@evident-ai/cli 3.4.1-dev.dbc0a1f → 3.4.1-dev.f9c90e1

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
@@ -763,6 +763,13 @@ function toReportedOpenAiWindow(window) {
763
763
  resets_at: window.resetsAt
764
764
  };
765
765
  }
766
+ function toReportedOpenAiSubscription(snapshot) {
767
+ if (!snapshot.subscription) return null;
768
+ return {
769
+ owner_email: snapshot.subscription.ownerEmail,
770
+ plan_type: snapshot.subscription.planType
771
+ };
772
+ }
766
773
  async function reportOpenAiUsage(agentId, authHeader, snapshot) {
767
774
  try {
768
775
  const apiUrl = getApiUrlConfig();
@@ -773,7 +780,8 @@ async function reportOpenAiUsage(agentId, authHeader, snapshot) {
773
780
  primary: toReportedOpenAiWindow(snapshot.primary),
774
781
  secondary: toReportedOpenAiWindow(snapshot.secondary),
775
782
  has_credits: snapshot.hasCredits,
776
- credits_unlimited: snapshot.creditsUnlimited
783
+ credits_unlimited: snapshot.creditsUnlimited,
784
+ subscription: toReportedOpenAiSubscription(snapshot)
777
785
  }),
778
786
  signal: AbortSignal.timeout(BEST_EFFORT_NOTIFY_TIMEOUT_MS)
779
787
  });
@@ -797,6 +805,7 @@ async function reportResourceUsage(agentId, authHeader, usage) {
797
805
  headers: { Authorization: authHeader, "Content-Type": "application/json" },
798
806
  body: JSON.stringify({
799
807
  cpu_percent: usage.cpuPercent,
808
+ cpu_peak_percent: usage.cpuPeakPercent,
800
809
  cpu_count: usage.cpuCount,
801
810
  memory_total_bytes: usage.memoryTotalBytes,
802
811
  memory_available_bytes: usage.memoryAvailableBytes,
@@ -1183,9 +1192,9 @@ async function claudeUsage() {
1183
1192
  }
1184
1193
 
1185
1194
  // src/commands/run.ts
1186
- import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync5, writeFileSync as writeFileSync4 } from "fs";
1195
+ import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync6 } from "fs";
1187
1196
  import { homedir as homedir5 } from "os";
1188
- import { isAbsolute as isAbsolute3, join as join8, parse, resolve as resolvePath2 } from "path";
1197
+ import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "path";
1189
1198
  import chalk6 from "chalk";
1190
1199
 
1191
1200
  // ../../packages/types/src/agents/index.ts
@@ -1207,6 +1216,7 @@ var TelemetryEventTypes = {
1207
1216
  // ../../packages/types/src/tunnel/index.ts
1208
1217
  var MAX_FRAME_BYTES = 256 * 1024;
1209
1218
  var TUNNEL_DRAIN_PING_PATH = "/__evident/drain";
1219
+ var TUNNEL_USAGE_REARM_PING_PATH = "/__evident/usage-rearm";
1210
1220
 
1211
1221
  // ../../packages/types/src/runner-files.ts
1212
1222
  var MAX_FILE_PUSH_BYTES = 64 * 1024;
@@ -1525,7 +1535,14 @@ function drainSessionDbRecoveryReport({
1525
1535
  skippedLines++;
1526
1536
  return [];
1527
1537
  }
1528
- return [{ ...value, replication_suspended: value.replication_suspended ?? false }];
1538
+ return [
1539
+ {
1540
+ ...value,
1541
+ provenance_reason: value.provenance_reason ?? null,
1542
+ provenance_migration_delta: value.provenance_migration_delta ?? null,
1543
+ replication_suspended: value.replication_suspended ?? false
1544
+ }
1545
+ ];
1529
1546
  } catch (error2) {
1530
1547
  skippedLines++;
1531
1548
  console.error(
@@ -1628,6 +1645,12 @@ function buildSessionDbRecoveryActivity(record) {
1628
1645
  metadata: withoutContractFields(record),
1629
1646
  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.`
1630
1647
  };
1648
+ case "schema_provenance_mismatch":
1649
+ return {
1650
+ level,
1651
+ metadata: withoutContractFields(record),
1652
+ 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.`
1653
+ };
1631
1654
  default:
1632
1655
  return null;
1633
1656
  }
@@ -1642,7 +1665,8 @@ var OUTCOMES = /* @__PURE__ */ new Set([
1642
1665
  "fresh_session_db",
1643
1666
  "history_rolled_back",
1644
1667
  "restore_misconfigured",
1645
- "session_db_boot_refused"
1668
+ "session_db_boot_refused",
1669
+ "schema_provenance_mismatch"
1646
1670
  ]);
1647
1671
  var STAGES = /* @__PURE__ */ new Set(["restore", "verify"]);
1648
1672
  var SEVERITIES = /* @__PURE__ */ new Set(["warning", "error"]);
@@ -1660,7 +1684,7 @@ var STRING_OR_NULL_FIELDS = ["quarantine_destination", "verified_restore_point"]
1660
1684
  function isSessionDbRecoveryRecord(value) {
1661
1685
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
1662
1686
  const record = value;
1663
- 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") && NUMBER_FIELDS.every((field) => record[field] === null || Number.isInteger(record[field])) && STRING_OR_NULL_FIELDS.every(
1687
+ 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(
1664
1688
  (field) => record[field] === null || typeof record[field] === "string"
1665
1689
  );
1666
1690
  }
@@ -1714,10 +1738,14 @@ function runSynchroniser(args, opts) {
1714
1738
  let stderr = "";
1715
1739
  let settled = false;
1716
1740
  const timer = {};
1741
+ let abortListener;
1742
+ let spawnListener;
1717
1743
  const finish = (result) => {
1718
1744
  if (settled) return;
1719
1745
  settled = true;
1720
1746
  if (timer.handle) clearTimeout(timer.handle);
1747
+ if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
1748
+ if (spawnListener) child.removeListener("spawn", spawnListener);
1721
1749
  resolve4(result);
1722
1750
  };
1723
1751
  try {
@@ -1743,6 +1771,25 @@ function runSynchroniser(args, opts) {
1743
1771
  child.once("close", (code) => {
1744
1772
  finish({ code, stdout, stderr, timedOut: false });
1745
1773
  });
1774
+ if (opts.signal) {
1775
+ const killChild = () => {
1776
+ if (child.pid === void 0) {
1777
+ if (!spawnListener) {
1778
+ spawnListener = killChild;
1779
+ child.once("spawn", spawnListener);
1780
+ }
1781
+ return;
1782
+ }
1783
+ child.kill("SIGKILL");
1784
+ };
1785
+ abortListener = killChild;
1786
+ if (opts.signal.aborted) {
1787
+ abortListener();
1788
+ } else {
1789
+ opts.signal.addEventListener("abort", abortListener, { once: true });
1790
+ if (opts.signal.aborted) abortListener();
1791
+ }
1792
+ }
1746
1793
  timer.handle = setTimeout(
1747
1794
  () => {
1748
1795
  child.kill("SIGKILL");
@@ -1780,6 +1827,8 @@ function reportRecord(stage, outcome, reason, litestreamExitCode, options) {
1780
1827
  quarantined_bytes: null,
1781
1828
  verified_restore_point: null,
1782
1829
  restore_points_tried: null,
1830
+ provenance_reason: null,
1831
+ provenance_migration_delta: null,
1783
1832
  replication_suspended: stage === "restore"
1784
1833
  });
1785
1834
  }
@@ -2200,6 +2249,131 @@ async function restoreAndVerifySessionDb(options) {
2200
2249
  return { verifyFatal: await verifySessionDb(options, configPath, env) };
2201
2250
  }
2202
2251
 
2252
+ // src/lib/opencode/session-db-provenance.ts
2253
+ import { createRequire } from "module";
2254
+ import { mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
2255
+ import { dirname as dirname3, join as join3 } from "path";
2256
+ var require2 = createRequire(import.meta.url);
2257
+ function readSessionDbMigrationIds(dbPath) {
2258
+ let db;
2259
+ try {
2260
+ const { DatabaseSync } = require2("node:sqlite");
2261
+ db = new DatabaseSync(dbPath, { readOnly: true });
2262
+ const columns = db.prepare("PRAGMA table_info(migration)").all();
2263
+ const hasExpectedShape = columns.length === 2 && columns.some(
2264
+ (column) => column.name === "id" && typeof column.type === "string" && column.type.toUpperCase() === "TEXT" && column.pk === 1
2265
+ ) && columns.some(
2266
+ (column) => column.name === "time_completed" && typeof column.type === "string" && column.type.toUpperCase() === "INTEGER" && column.notnull === 1 && column.pk === 0
2267
+ );
2268
+ if (!hasExpectedShape) {
2269
+ console.warn(`[readSessionDbMigrationIds] unsupported migration table shape in ${dbPath}`);
2270
+ return null;
2271
+ }
2272
+ const rows = db.prepare("SELECT id FROM migration ORDER BY id").all();
2273
+ if (rows.some((row) => typeof row.id !== "string")) return null;
2274
+ return rows.map((row) => row.id);
2275
+ } catch (error2) {
2276
+ console.warn(
2277
+ `[readSessionDbMigrationIds] could not read migration history from ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2278
+ );
2279
+ return null;
2280
+ } finally {
2281
+ try {
2282
+ db?.close();
2283
+ } catch (error2) {
2284
+ console.warn(
2285
+ `[readSessionDbMigrationIds] could not close ${dbPath}: ${error2 instanceof Error ? error2.message : String(error2)}`
2286
+ );
2287
+ }
2288
+ }
2289
+ }
2290
+ function sessionDbProvenanceStatePath(homeDir, env) {
2291
+ const override = env.EVIDENT_SESSION_DB_PROVENANCE_STATE?.trim();
2292
+ return override || join3(homeDir, ".local", "state", "evident", "session-db-provenance.json");
2293
+ }
2294
+ function loadSessionDbProvenanceState(path) {
2295
+ let value;
2296
+ try {
2297
+ value = JSON.parse(readFileSync3(path, "utf8"));
2298
+ } catch (error2) {
2299
+ if (error2 instanceof Error && "code" in error2 && error2.code === "ENOENT") return {};
2300
+ console.error(
2301
+ `[session-db-provenance] could not read ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2302
+ );
2303
+ return {};
2304
+ }
2305
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
2306
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2307
+ return {};
2308
+ }
2309
+ const state = {};
2310
+ for (const [dbPath, record] of Object.entries(value)) {
2311
+ if (!isSessionDbProvenanceRecord(record)) {
2312
+ console.error(`[session-db-provenance] ignored malformed state in ${path}`);
2313
+ return {};
2314
+ }
2315
+ state[dbPath] = record;
2316
+ }
2317
+ return state;
2318
+ }
2319
+ function saveSessionDbProvenanceState(path, state) {
2320
+ try {
2321
+ mkdirSync2(dirname3(path), { recursive: true });
2322
+ writeFileSync2(path, `${JSON.stringify(state, null, 2)}
2323
+ `, "utf8");
2324
+ } catch (error2) {
2325
+ console.error(
2326
+ `[session-db-provenance] could not write ${path}: ${error2 instanceof Error ? error2.message : String(error2)}`
2327
+ );
2328
+ }
2329
+ }
2330
+ function evaluateSessionDbProvenance(input) {
2331
+ const { currentVersion, currentIds, previous } = input;
2332
+ if (!previous) return { anomaly: false, reason: null };
2333
+ const current = new Set(currentIds);
2334
+ const prior = new Set(previous.migrationIds);
2335
+ for (const id of prior) {
2336
+ if (!current.has(id)) return { anomaly: true, reason: "migration-history-regressed" };
2337
+ }
2338
+ if (current.size > prior.size && previous.opencodeVersion === currentVersion) {
2339
+ return { anomaly: true, reason: "foreign-version-migrations" };
2340
+ }
2341
+ return { anomaly: false, reason: null };
2342
+ }
2343
+ function checkSessionDbProvenance(input) {
2344
+ const { dbPath, currentVersion, homeDir, env } = input;
2345
+ const path = sessionDbProvenanceStatePath(homeDir, env);
2346
+ const state = loadSessionDbProvenanceState(path);
2347
+ const previous = state[dbPath];
2348
+ const currentIds = readSessionDbMigrationIds(dbPath);
2349
+ if (currentIds === null) {
2350
+ return {
2351
+ anomaly: false,
2352
+ reason: null,
2353
+ recordedVersion: previous?.opencodeVersion ?? null,
2354
+ migrationDelta: null
2355
+ };
2356
+ }
2357
+ const decision = evaluateSessionDbProvenance({ currentVersion, currentIds, previous });
2358
+ const migrationDelta = previous ? new Set(currentIds).size - new Set(previous.migrationIds).size : null;
2359
+ state[dbPath] = {
2360
+ opencodeVersion: currentVersion,
2361
+ migrationIds: currentIds,
2362
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2363
+ };
2364
+ saveSessionDbProvenanceState(path, state);
2365
+ return {
2366
+ ...decision,
2367
+ recordedVersion: previous?.opencodeVersion ?? null,
2368
+ migrationDelta
2369
+ };
2370
+ }
2371
+ function isSessionDbProvenanceRecord(value) {
2372
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
2373
+ const record = value;
2374
+ return typeof record.opencodeVersion === "string" && Array.isArray(record.migrationIds) && record.migrationIds.every((id) => typeof id === "string") && typeof record.updatedAt === "string";
2375
+ }
2376
+
2203
2377
  // src/lib/opencode/opencode-version-gate.ts
2204
2378
  var QUEUE_VALIDATED_OPENCODE_VERSIONS = ["1.17.11", "1.18.3"];
2205
2379
  function isQueueValidatedVersion(version2) {
@@ -2978,6 +3152,44 @@ function findLastAssistantReplyFor(messages, userMessageId) {
2978
3152
  }
2979
3153
  return lastOk ?? last;
2980
3154
  }
3155
+ function collectSubagentSessions(messages, userMessageId) {
3156
+ if (!messages || messages.length === 0) return [];
3157
+ const byParent = messages.filter(
3158
+ (message) => roleOf(message) === "assistant" && parentIdOf(message) === userMessageId
3159
+ );
3160
+ const assistants = byParent.length > 0 ? byParent : [];
3161
+ if (assistants.length === 0) {
3162
+ const userIndex = messages.findIndex((message) => idOf(message) === userMessageId);
3163
+ if (userIndex === -1) return [];
3164
+ for (let i = userIndex + 1; i < messages.length; i++) {
3165
+ const message = messages[i];
3166
+ if (roleOf(message) === "user") break;
3167
+ if (roleOf(message) === "assistant") assistants.push(message);
3168
+ }
3169
+ }
3170
+ const refs = [];
3171
+ const seen = /* @__PURE__ */ new Set();
3172
+ for (const message of assistants) {
3173
+ const parts = Array.isArray(message.parts) ? message.parts : [];
3174
+ for (const part of parts) {
3175
+ if (!part || typeof part !== "object" || part.type !== "tool" || part.tool !== "task")
3176
+ continue;
3177
+ const state = part.state;
3178
+ if (!state || typeof state !== "object") continue;
3179
+ const metadata = state.metadata;
3180
+ if (!metadata || typeof metadata !== "object") continue;
3181
+ const sessionId = metadata.sessionId;
3182
+ if (typeof sessionId !== "string" || sessionId.length === 0 || seen.has(sessionId)) continue;
3183
+ seen.add(sessionId);
3184
+ const start = state.time?.start;
3185
+ refs.push({
3186
+ sessionId,
3187
+ startedAtMs: typeof start === "number" && Number.isFinite(start) ? start : null
3188
+ });
3189
+ }
3190
+ }
3191
+ return refs;
3192
+ }
2981
3193
  function messageUsage(messages, userMessageId) {
2982
3194
  if (!messages || messages.length === 0) return null;
2983
3195
  const byParentAll = messages.filter(
@@ -3106,8 +3318,7 @@ function isAbortedTerminalReply(messages, userMessageId) {
3106
3318
  }
3107
3319
  return false;
3108
3320
  }
3109
- function messageFailure(messages, userMessageId) {
3110
- const reply = findLastAssistantReplyFor(messages, userMessageId);
3321
+ function classifyReplyAuthError(reply) {
3111
3322
  const error2 = errorOf(reply);
3112
3323
  if (error2 == null || typeof error2 !== "object") return null;
3113
3324
  const e = error2;
@@ -3132,6 +3343,32 @@ function messageFailure(messages, userMessageId) {
3132
3343
  }
3133
3344
  return null;
3134
3345
  }
3346
+ function messageFailure(messages, userMessageId) {
3347
+ return classifyReplyAuthError(findLastAssistantReplyFor(messages, userMessageId));
3348
+ }
3349
+ function findLatestSubagentAuthOutcome(messages, sinceMs) {
3350
+ if (!messages || messages.length === 0) return null;
3351
+ for (let i = messages.length - 1; i >= 0; i--) {
3352
+ const message = messages[i];
3353
+ if (roleOf(message) !== "assistant") continue;
3354
+ const created = createdOf(message);
3355
+ if (sinceMs !== null && typeof created === "number" && created < sinceMs) continue;
3356
+ const failure = classifyReplyAuthError(message);
3357
+ if (failure) {
3358
+ if (!failure.providerId) return null;
3359
+ return { providerId: failure.providerId, outcome: "failed", failure };
3360
+ }
3361
+ const providerId = message.info?.providerID;
3362
+ if (errorOf(message) == null && typeof providerId === "string" && providerId.length > 0) {
3363
+ return { providerId, outcome: "succeeded" };
3364
+ }
3365
+ return null;
3366
+ }
3367
+ return null;
3368
+ }
3369
+ function findSubagentAuthOutcome(messages, sinceMs) {
3370
+ return findLatestSubagentAuthOutcome(messages, sinceMs);
3371
+ }
3135
3372
  function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
3136
3373
  if (classified != null) return classified;
3137
3374
  if (hasConfiguredProvider !== false) return null;
@@ -3286,10 +3523,10 @@ function resolveSessionCleanupConfig(flags, env = process.env) {
3286
3523
 
3287
3524
  // src/lib/opencode/session-db-size.ts
3288
3525
  import { statSync as statSync3 } from "fs";
3289
- import { join as join3 } from "path";
3526
+ import { join as join4 } from "path";
3290
3527
  var LARGE_DB_THRESHOLD_BYTES = 268435456;
3291
3528
  function statSessionDbBytes(homeDir) {
3292
- const dbPath = join3(homeDir, ".local", "share", "opencode", "opencode.db");
3529
+ const dbPath = join4(homeDir, ".local", "share", "opencode", "opencode.db");
3293
3530
  try {
3294
3531
  return statSync3(dbPath).size;
3295
3532
  } catch (err) {
@@ -3318,10 +3555,10 @@ function buildSessionStoreSizeWarning(input) {
3318
3555
 
3319
3556
  // src/lib/opencode/session-db-reclaim.ts
3320
3557
  import { statSync as statSync4, statfsSync } from "fs";
3321
- import { dirname as dirname3 } from "path";
3558
+ import { dirname as dirname4 } from "path";
3322
3559
  function insufficientSpaceReason(dbPath, requiredBytes) {
3323
3560
  try {
3324
- const fsStats = statfsSync(dirname3(dbPath));
3561
+ const fsStats = statfsSync(dirname4(dbPath));
3325
3562
  const availableBytes = fsStats.bavail * fsStats.bsize;
3326
3563
  if (availableBytes < requiredBytes) {
3327
3564
  return `only ${availableBytes} bytes free, need ${requiredBytes} for a second copy`;
@@ -3462,7 +3699,6 @@ var StreamForwarder = class {
3462
3699
  handleFrame(frame) {
3463
3700
  switch (frame.type) {
3464
3701
  case "open":
3465
- this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
3466
3702
  void this.handleOpen(frame);
3467
3703
  break;
3468
3704
  case "req_data":
@@ -3498,12 +3734,21 @@ var StreamForwarder = class {
3498
3734
  const { sid, method, path, headers, has_body } = frame;
3499
3735
  const correlationId = headers?.[CORRELATION_ID_HEADER];
3500
3736
  const startedAt = Date.now();
3737
+ if (path !== TUNNEL_DRAIN_PING_PATH && path !== TUNNEL_USAGE_REARM_PING_PATH) {
3738
+ this.callbacks.onOpen?.(sid, method, path);
3739
+ }
3501
3740
  if (path === TUNNEL_DRAIN_PING_PATH) {
3502
3741
  this.callbacks.onDrainPing?.();
3503
3742
  this.send({ type: "head", sid, status: 204, headers: {} });
3504
3743
  this.send({ type: "res_end", sid });
3505
3744
  return;
3506
3745
  }
3746
+ if (path === TUNNEL_USAGE_REARM_PING_PATH) {
3747
+ this.callbacks.onUsageRearmPing?.();
3748
+ this.send({ type: "head", sid, status: 204, headers: {} });
3749
+ this.send({ type: "res_end", sid });
3750
+ return;
3751
+ }
3507
3752
  if (process.env.DEBUG) {
3508
3753
  log("debug", "agent_request", {
3509
3754
  correlation_id: correlationId,
@@ -3648,7 +3893,8 @@ function connectTunnel(options) {
3648
3893
  onResponse,
3649
3894
  onInfo,
3650
3895
  onWarning,
3651
- onDrainPing
3896
+ onDrainPing,
3897
+ onUsageRearmPing
3652
3898
  } = options;
3653
3899
  const tunnelUrl = getTunnelUrlConfig();
3654
3900
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
@@ -3660,7 +3906,8 @@ function connectTunnel(options) {
3660
3906
  });
3661
3907
  const forwarder = new StreamForwarder(ws, port, {
3662
3908
  onHead: () => onResponse?.(),
3663
- onDrainPing: () => onDrainPing?.()
3909
+ onDrainPing: () => onDrainPing?.(),
3910
+ onUsageRearmPing: () => onUsageRearmPing?.()
3664
3911
  });
3665
3912
  const connectionTimeout = setTimeout(() => {
3666
3913
  ws.close();
@@ -3703,8 +3950,8 @@ function connectTunnel(options) {
3703
3950
  try {
3704
3951
  message = JSON.parse(data.toString());
3705
3952
  } catch (error2) {
3706
- const errorMessage = error2 instanceof Error ? error2.message : "Unknown error";
3707
- onError?.(`Failed to handle message: ${errorMessage}`);
3953
+ const errorMessage2 = error2 instanceof Error ? error2.message : "Unknown error";
3954
+ onError?.(`Failed to handle message: ${errorMessage2}`);
3708
3955
  return;
3709
3956
  }
3710
3957
  if (isStreamFrame(message)) {
@@ -3821,6 +4068,7 @@ var RunnerConnection = class {
3821
4068
  onError: (error2) => events.onError?.(error2),
3822
4069
  onResponse: () => events.onResponse?.(),
3823
4070
  onDrainPing: () => events.onDrainPing?.(),
4071
+ onUsageRearmPing: () => events.onUsageRearmPing?.(),
3824
4072
  onInfo: (message) => events.onInfo?.(message),
3825
4073
  onWarning: (message) => events.onWarning?.(message)
3826
4074
  });
@@ -3847,10 +4095,10 @@ var RunnerConnection = class {
3847
4095
  };
3848
4096
 
3849
4097
  // src/lib/tunnel/ready-marker.ts
3850
- import { writeFileSync as writeFileSync2 } from "fs";
4098
+ import { writeFileSync as writeFileSync3 } from "fs";
3851
4099
  function writeTunnelReadyMarker(path, agentId) {
3852
4100
  try {
3853
- writeFileSync2(path, `${agentId}
4101
+ writeFileSync3(path, `${agentId}
3854
4102
  `);
3855
4103
  return { ok: true };
3856
4104
  } catch (error2) {
@@ -3875,7 +4123,7 @@ async function stopSessionDbReplication(child, timeoutMs) {
3875
4123
  }
3876
4124
 
3877
4125
  // src/lib/process-liveness.ts
3878
- import { readFileSync as readFileSync3 } from "fs";
4126
+ import { readFileSync as readFileSync4 } from "fs";
3879
4127
  function isProcessAlive(pid) {
3880
4128
  try {
3881
4129
  process.kill(pid, 0);
@@ -3890,7 +4138,7 @@ function isProcessAlive(pid) {
3890
4138
  }
3891
4139
  if (process.platform !== "linux") return true;
3892
4140
  try {
3893
- const status2 = readFileSync3(`/proc/${pid}/status`, "utf8");
4141
+ const status2 = readFileSync4(`/proc/${pid}/status`, "utf8");
3894
4142
  return !/^State:\s+Z(?:\s|$)/m.test(status2);
3895
4143
  } catch (error2) {
3896
4144
  console.error(
@@ -3901,9 +4149,9 @@ function isProcessAlive(pid) {
3901
4149
  }
3902
4150
 
3903
4151
  // src/lib/openai-usage.ts
3904
- import { readFileSync as readFileSync4 } from "fs";
4152
+ import { readFileSync as readFileSync5 } from "fs";
3905
4153
  import { homedir as homedir3 } from "os";
3906
- import { join as join4 } from "path";
4154
+ import { join as join5 } from "path";
3907
4155
  var CODEX_RESPONSES_URL = "https://chatgpt.com/backend-api/codex/responses";
3908
4156
  var OPENCODE_AUTH_SEGMENTS = [".local", "share", "opencode", "auth.json"];
3909
4157
  var OpenAiUsageError = class extends Error {
@@ -3917,7 +4165,7 @@ function isLocalCredentialProblem2(err) {
3917
4165
  }
3918
4166
  function readOpenCodeChatGptCredentials() {
3919
4167
  try {
3920
- const raw = readFileSync4(join4(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
4168
+ const raw = readFileSync5(join5(homedir3(), ...OPENCODE_AUTH_SEGMENTS), "utf-8");
3921
4169
  let parsed;
3922
4170
  try {
3923
4171
  parsed = JSON.parse(raw);
@@ -3939,6 +4187,23 @@ function readOpenCodeChatGptCredentials() {
3939
4187
  return null;
3940
4188
  }
3941
4189
  }
4190
+ function parseChatGptIdentity(accessToken) {
4191
+ const segments = accessToken.split(".");
4192
+ if (segments.length !== 3) return null;
4193
+ let payload;
4194
+ try {
4195
+ const parsed = JSON.parse(Buffer.from(segments[1], "base64url").toString("utf8"));
4196
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
4197
+ payload = parsed;
4198
+ } catch {
4199
+ return null;
4200
+ }
4201
+ const profile = payload["https://api.openai.com/profile"];
4202
+ const auth = payload["https://api.openai.com/auth"];
4203
+ const ownerEmail = typeof profile?.email === "string" ? profile.email.trim() || null : null;
4204
+ const planType = typeof auth?.chatgpt_plan_type === "string" ? auth.chatgpt_plan_type.trim() || null : null;
4205
+ return ownerEmail === null && planType === null ? null : { ownerEmail, planType };
4206
+ }
3942
4207
  function toWindow2(headers, name) {
3943
4208
  const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
3944
4209
  const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
@@ -4014,6 +4279,7 @@ async function getOpenAiUsage(port) {
4014
4279
  "credentials_expired"
4015
4280
  );
4016
4281
  }
4282
+ const subscription = parseChatGptIdentity(credentials2.accessToken);
4017
4283
  const models = await resolveProbeModels(port);
4018
4284
  if (models.length === 0) {
4019
4285
  throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
@@ -4046,7 +4312,7 @@ async function getOpenAiUsage(port) {
4046
4312
  "no_usable_window"
4047
4313
  );
4048
4314
  }
4049
- return usage;
4315
+ return { ...usage, subscription };
4050
4316
  }
4051
4317
  if (res.status === 401) {
4052
4318
  throw new OpenAiUsageError(
@@ -4247,45 +4513,84 @@ function readDisk(homeDir) {
4247
4513
  };
4248
4514
  }
4249
4515
  }
4250
- function createResourceUsageCollector(homeDir) {
4251
- let previous = readCpuSample();
4252
- return async () => {
4516
+ var CPU_PEAK_WINDOW_MS = 6e4;
4517
+ var CPU_PEAK_WINDOW_SAMPLE_COUNT = 4;
4518
+ var CPU_PEAK_SAMPLE_INTERVAL_MS = CPU_PEAK_WINDOW_MS / CPU_PEAK_WINDOW_SAMPLE_COUNT;
4519
+ function createCpuPeakSampler() {
4520
+ const sampleHistory = new Array(CPU_PEAK_WINDOW_SAMPLE_COUNT);
4521
+ sampleHistory[0] = readCpuSample();
4522
+ let nextSampleIndex = 1;
4523
+ let sampleCount = 1;
4524
+ let peak = null;
4525
+ const timer = setInterval(() => {
4253
4526
  const current = readCpuSample();
4254
- const hostCpuPercent = cpuPercentBetween(previous, current);
4255
- const hostCpuCount = cpus().length;
4256
- previous = current;
4257
- const disk = readDisk(homeDir);
4258
- const opencodeDbBytes = statSessionDbBytes(homeDir);
4259
- const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
4260
- const warnings = [];
4261
- if (disk.warning) warnings.push(disk.warning);
4262
- if (ecsWarning) warnings.push(ecsWarning);
4263
- let cpuPercent = hostCpuPercent;
4264
- let cpuCount = hostCpuCount;
4265
- let memoryTotalBytes = totalmem();
4266
- let memoryAvailableBytes = freemem();
4267
- if (limits !== null) {
4268
- cpuCount = limits.cpuCount;
4269
- memoryTotalBytes = limits.memoryTotalBytes;
4270
- memoryAvailableBytes = clamp(
4271
- limits.memoryTotalBytes - (totalmem() - freemem()),
4272
- 0,
4273
- limits.memoryTotalBytes
4274
- );
4275
- cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
4527
+ const sampleFromWindowAgo = sampleCount === CPU_PEAK_WINDOW_SAMPLE_COUNT ? sampleHistory[nextSampleIndex] : void 0;
4528
+ if (sampleFromWindowAgo !== void 0) {
4529
+ const percentage = cpuPercentBetween(sampleFromWindowAgo, current);
4530
+ if (percentage !== null) {
4531
+ peak = peak === null ? percentage : Math.max(peak, percentage);
4532
+ }
4276
4533
  }
4277
- return {
4278
- usage: {
4279
- cpuPercent,
4280
- cpuCount,
4281
- memoryTotalBytes,
4282
- memoryAvailableBytes,
4283
- diskTotalBytes: disk.totalBytes,
4284
- diskFreeBytes: disk.freeBytes,
4285
- opencodeDbBytes
4286
- },
4287
- warnings
4288
- };
4534
+ sampleHistory[nextSampleIndex] = current;
4535
+ nextSampleIndex = (nextSampleIndex + 1) % CPU_PEAK_WINDOW_SAMPLE_COUNT;
4536
+ sampleCount = Math.min(sampleCount + 1, CPU_PEAK_WINDOW_SAMPLE_COUNT);
4537
+ }, CPU_PEAK_SAMPLE_INTERVAL_MS);
4538
+ return {
4539
+ takeAndReset: () => {
4540
+ const currentPeak = peak;
4541
+ peak = null;
4542
+ return currentPeak;
4543
+ },
4544
+ stop: () => clearInterval(timer)
4545
+ };
4546
+ }
4547
+ function createResourceUsageCollector(homeDir) {
4548
+ let previous = readCpuSample();
4549
+ const cpuPeakSampler = createCpuPeakSampler();
4550
+ return {
4551
+ collect: async () => {
4552
+ const current = readCpuSample();
4553
+ const hostCpuPercent = cpuPercentBetween(previous, current);
4554
+ const hostCpuPeakPercent = cpuPeakSampler.takeAndReset();
4555
+ const hostCpuCount = cpus().length;
4556
+ previous = current;
4557
+ const disk = readDisk(homeDir);
4558
+ const opencodeDbBytes = statSessionDbBytes(homeDir);
4559
+ const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
4560
+ const warnings = [];
4561
+ if (disk.warning) warnings.push(disk.warning);
4562
+ if (ecsWarning) warnings.push(ecsWarning);
4563
+ let cpuPercent = hostCpuPercent;
4564
+ let cpuPeakPercent = hostCpuPeakPercent;
4565
+ let cpuCount = hostCpuCount;
4566
+ let memoryTotalBytes = totalmem();
4567
+ let memoryAvailableBytes = freemem();
4568
+ if (limits !== null) {
4569
+ cpuCount = limits.cpuCount;
4570
+ memoryTotalBytes = limits.memoryTotalBytes;
4571
+ memoryAvailableBytes = clamp(
4572
+ limits.memoryTotalBytes - (totalmem() - freemem()),
4573
+ 0,
4574
+ limits.memoryTotalBytes
4575
+ );
4576
+ cpuPercent = hostCpuPercent === null ? null : clamp(round2(hostCpuPercent * hostCpuCount / limits.cpuCount), 0, 100);
4577
+ cpuPeakPercent = hostCpuPeakPercent === null ? null : clamp(round2(hostCpuPeakPercent * hostCpuCount / limits.cpuCount), 0, 100);
4578
+ }
4579
+ return {
4580
+ usage: {
4581
+ cpuPercent,
4582
+ cpuPeakPercent,
4583
+ cpuCount,
4584
+ memoryTotalBytes,
4585
+ memoryAvailableBytes,
4586
+ diskTotalBytes: disk.totalBytes,
4587
+ diskFreeBytes: disk.freeBytes,
4588
+ opencodeDbBytes
4589
+ },
4590
+ warnings
4591
+ };
4592
+ },
4593
+ stop: cpuPeakSampler.stop
4289
4594
  };
4290
4595
  }
4291
4596
 
@@ -4293,12 +4598,12 @@ function createResourceUsageCollector(homeDir) {
4293
4598
  import { homedir as homedir4 } from "os";
4294
4599
 
4295
4600
  // src/lib/runner-file-sync.ts
4296
- import { join as join6 } from "path";
4601
+ import { join as join7 } from "path";
4297
4602
 
4298
4603
  // src/lib/file-push.ts
4299
4604
  import { randomUUID } from "crypto";
4300
4605
  import { chmod, mkdir, open as open2, realpath, rename, unlink } from "fs/promises";
4301
- import { basename, dirname as dirname4, isAbsolute, join as join5, relative, resolve as resolve2, sep } from "path";
4606
+ import { basename, dirname as dirname5, isAbsolute, join as join6, relative, resolve as resolve2, sep } from "path";
4302
4607
  var FILE_MODE = 384;
4303
4608
  var DIRECTORY_MODE = 448;
4304
4609
  async function writePushedFile(request) {
@@ -4329,9 +4634,9 @@ async function writePushedFile(request) {
4329
4634
  }
4330
4635
  try {
4331
4636
  const { existingAncestor, missingSegments } = await resolveNearestExistingAncestor(
4332
- dirname4(candidate)
4637
+ dirname5(candidate)
4333
4638
  );
4334
- const realTarget = join5(existingAncestor, ...missingSegments, basename(candidate));
4639
+ const realTarget = join6(existingAncestor, ...missingSegments, basename(candidate));
4335
4640
  const allowedDirectory = await findContainingAllowedDirectory(allowedDirectories, realTarget);
4336
4641
  if (allowedDirectory === null) {
4337
4642
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
@@ -4341,8 +4646,8 @@ async function writePushedFile(request) {
4341
4646
  }
4342
4647
  if (missingSegments.length > 0) {
4343
4648
  await createMissingDirectories(existingAncestor, missingSegments);
4344
- const realParent = await realpath(dirname4(realTarget));
4345
- if (realParent !== dirname4(realTarget) || !contains(allowedDirectory, realTarget)) {
4649
+ const realParent = await realpath(dirname5(realTarget));
4650
+ if (realParent !== dirname5(realTarget) || !contains(allowedDirectory, realTarget)) {
4346
4651
  return refuse("path_not_allowed", "The runner does not allow writing to that location.", {
4347
4652
  path: realTarget,
4348
4653
  bytes,
@@ -4367,7 +4672,7 @@ function expandAndValidate(requestedPath, homeDir) {
4367
4672
  if (requestedPath.trim() === "" || requestedPath.includes("\0")) {
4368
4673
  return null;
4369
4674
  }
4370
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join5(homeDir, requestedPath.slice(2)) : requestedPath;
4675
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
4371
4676
  if (expanded.split(/[/\\]/).includes("..")) {
4372
4677
  return null;
4373
4678
  }
@@ -4385,7 +4690,7 @@ async function resolveNearestExistingAncestor(directory) {
4385
4690
  try {
4386
4691
  return { existingAncestor: await realpath(current), missingSegments };
4387
4692
  } catch (err) {
4388
- const parent = dirname4(current);
4693
+ const parent = dirname5(current);
4389
4694
  if (err.code !== "ENOENT" || parent === current) {
4390
4695
  throw err;
4391
4696
  }
@@ -4440,13 +4745,13 @@ function contains(realDirectory, realTarget) {
4440
4745
  async function createMissingDirectories(existingAncestor, missingSegments) {
4441
4746
  let current = existingAncestor;
4442
4747
  for (const segment of missingSegments) {
4443
- current = join5(current, segment);
4748
+ current = join6(current, segment);
4444
4749
  await mkdir(current, { recursive: true, mode: DIRECTORY_MODE });
4445
4750
  await chmod(current, DIRECTORY_MODE);
4446
4751
  }
4447
4752
  }
4448
4753
  async function writeAtomically(realTarget, content) {
4449
- const temporaryPath = join5(dirname4(realTarget), `.evident-push-${randomUUID()}.tmp`);
4754
+ const temporaryPath = join6(dirname5(realTarget), `.evident-push-${randomUUID()}.tmp`);
4450
4755
  let handle;
4451
4756
  try {
4452
4757
  handle = await open2(temporaryPath, "wx", FILE_MODE);
@@ -4576,12 +4881,12 @@ var NOT_APPLIED = {
4576
4881
  opencodeAuthApplied: false
4577
4882
  };
4578
4883
  function isClaudeCredentialPath(requestedPath, homeDir) {
4579
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
4580
- return expanded === join6(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
4884
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4885
+ return expanded === join7(homeDir, ...CLAUDE_CREDENTIALS_SEGMENTS);
4581
4886
  }
4582
4887
  function isOpenCodeAuthPath(requestedPath, homeDir) {
4583
- const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join6(homeDir, requestedPath.slice(2)) : requestedPath;
4584
- return expanded === join6(homeDir, ...OPENCODE_AUTH_SEGMENTS);
4888
+ const expanded = requestedPath === "~" ? homeDir : requestedPath.startsWith("~/") ? join7(homeDir, requestedPath.slice(2)) : requestedPath;
4889
+ return expanded === join7(homeDir, ...OPENCODE_AUTH_SEGMENTS);
4585
4890
  }
4586
4891
  async function applyOne(options, file) {
4587
4892
  const label = `${file.id.slice(0, 8)} (${file.path})`;
@@ -5108,6 +5413,7 @@ var ChannelDriver = class _ChannelDriver {
5108
5413
  * and stops opencode.
5109
5414
  */
5110
5415
  stopped = false;
5416
+ recycleRequestedFlag = false;
5111
5417
  constructor(config) {
5112
5418
  this.agentId = config.agentId;
5113
5419
  this.port = config.port;
@@ -5213,6 +5519,9 @@ var ChannelDriver = class _ChannelDriver {
5213
5519
  let dispatched = 0;
5214
5520
  try {
5215
5521
  const conversations = await this.getPendingConversations();
5522
+ if (this.recycleRequestedFlag) {
5523
+ this.stop();
5524
+ }
5216
5525
  if (conversations.length > 0) {
5217
5526
  const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
5218
5527
  this.log({
@@ -5341,6 +5650,14 @@ var ChannelDriver = class _ChannelDriver {
5341
5650
  stop() {
5342
5651
  this.stopped = true;
5343
5652
  }
5653
+ /**
5654
+ * The server clears this request when a new MicroVM identity is recorded, so a
5655
+ * same-VM tunnel reconnect does not consume it. This is a plain read rather
5656
+ * than a consume; `run.ts` guards the action once-only.
5657
+ */
5658
+ get recycleRequested() {
5659
+ return this.recycleRequestedFlag;
5660
+ }
5344
5661
  /**
5345
5662
  * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
5346
5663
  * graceful shutdown, so a turn whose reply is ready — or completes within the
@@ -5478,7 +5795,7 @@ var ChannelDriver = class _ChannelDriver {
5478
5795
  this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
5479
5796
  break;
5480
5797
  }
5481
- const errorMessage = err instanceof Error ? err.message : String(err);
5798
+ const errorMessage2 = err instanceof Error ? err.message : String(err);
5482
5799
  this.sessions.delete(conv.id);
5483
5800
  this.supersede(conv.id, sessionId);
5484
5801
  this.log({
@@ -5487,7 +5804,7 @@ var ChannelDriver = class _ChannelDriver {
5487
5804
  conversation_id: conv.id,
5488
5805
  message_id: message.id
5489
5806
  });
5490
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5807
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
5491
5808
  this.log({
5492
5809
  level: "warn",
5493
5810
  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)}`,
@@ -5498,7 +5815,7 @@ var ChannelDriver = class _ChannelDriver {
5498
5815
  });
5499
5816
  this.log({
5500
5817
  level: "error",
5501
- message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
5818
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage2}`,
5502
5819
  conversation_id: conv.id,
5503
5820
  message_id: message.id
5504
5821
  });
@@ -5519,14 +5836,14 @@ var ChannelDriver = class _ChannelDriver {
5519
5836
  this.unconfirmedDispatchFailures.delete(message.id);
5520
5837
  this.sessions.delete(conv.id);
5521
5838
  this.supersede(conv.id, sessionId);
5522
- 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.`;
5839
+ const errorMessage2 = `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.`;
5523
5840
  this.log({
5524
5841
  level: "error",
5525
- message: errorMessage,
5842
+ message: errorMessage2,
5526
5843
  conversation_id: conv.id,
5527
5844
  message_id: message.id
5528
5845
  });
5529
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5846
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
5530
5847
  this.log({
5531
5848
  level: "warn",
5532
5849
  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)}`,
@@ -5860,6 +6177,9 @@ var ChannelDriver = class _ChannelDriver {
5860
6177
  });
5861
6178
  await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
5862
6179
  }
6180
+ if (ocId !== null) {
6181
+ await this.reportSubagentAuthFailures(conv.id, ocId, message.id, messages);
6182
+ }
5863
6183
  } catch (err) {
5864
6184
  if (err instanceof ChannelAuthError) throw err;
5865
6185
  this.log({
@@ -6824,6 +7144,12 @@ var ChannelDriver = class _ChannelDriver {
6824
7144
  return;
6825
7145
  }
6826
7146
  inFlight.done = true;
7147
+ await this.reportSubagentAuthFailures(
7148
+ watcher.conv.id,
7149
+ inFlight.opencodeMessageId,
7150
+ inFlight.evidentMessageId,
7151
+ messages
7152
+ );
6827
7153
  }
6828
7154
  this.removeInFlight(watcher, inFlight.evidentMessageId);
6829
7155
  return;
@@ -7066,6 +7392,12 @@ var ChannelDriver = class _ChannelDriver {
7066
7392
  return;
7067
7393
  }
7068
7394
  inFlight.done = true;
7395
+ await this.reportSubagentAuthFailures(
7396
+ watcher.conv.id,
7397
+ inFlight.opencodeMessageId,
7398
+ inFlight.evidentMessageId,
7399
+ messages
7400
+ );
7069
7401
  }
7070
7402
  this.removeInFlight(watcher, inFlight.evidentMessageId);
7071
7403
  }
@@ -7236,6 +7568,7 @@ var ChannelDriver = class _ChannelDriver {
7236
7568
  });
7237
7569
  return;
7238
7570
  }
7571
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
7239
7572
  this.dontRedispatch.delete(row.id);
7240
7573
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
7241
7574
  return;
@@ -7397,6 +7730,9 @@ var ChannelDriver = class _ChannelDriver {
7397
7730
  });
7398
7731
  return;
7399
7732
  }
7733
+ if (ocId !== null) {
7734
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId, row.id, messages);
7735
+ }
7400
7736
  this.dontRedispatch.delete(row.id);
7401
7737
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
7402
7738
  }
@@ -7490,14 +7826,14 @@ var ChannelDriver = class _ChannelDriver {
7490
7826
  this.unconfirmedDispatchFailures.delete(row.id);
7491
7827
  this.sessions.delete(readoptConv.id);
7492
7828
  this.supersede(readoptConv.id, sessionId);
7493
- 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.`;
7829
+ const errorMessage2 = `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.`;
7494
7830
  this.log({
7495
7831
  level: "error",
7496
- message: errorMessage,
7832
+ message: errorMessage2,
7497
7833
  conversation_id: row.conversation_id,
7498
7834
  message_id: row.id
7499
7835
  });
7500
- await this.markFailed(row.conversation_id, row.id, null, errorMessage).catch((markErr) => {
7836
+ await this.markFailed(row.conversation_id, row.id, null, errorMessage2).catch((markErr) => {
7501
7837
  this.log({
7502
7838
  level: "warn",
7503
7839
  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)}`,
@@ -8112,6 +8448,7 @@ var ChannelDriver = class _ChannelDriver {
8112
8448
  throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
8113
8449
  }
8114
8450
  const data = await res.json();
8451
+ this.recycleRequestedFlag = data.recycle_requested === true;
8115
8452
  let conversations = data.conversations;
8116
8453
  if (this.conversationFilter) {
8117
8454
  conversations = conversations.filter((c) => c.id === this.conversationFilter);
@@ -8347,6 +8684,111 @@ var ChannelDriver = class _ChannelDriver {
8347
8684
  reply?.info?.modelID ?? null
8348
8685
  );
8349
8686
  }
8687
+ async recordSubagentModelAuthFailure(failure, conversationId, messageId) {
8688
+ const providerId = failure.providerId ?? "(unknown)";
8689
+ try {
8690
+ const res = await this.fetchImpl(
8691
+ `${this.apiUrl}/runners/${this.agentId}/model-auth-failures`,
8692
+ {
8693
+ method: "POST",
8694
+ headers: { Authorization: this.getAuthHeader(), "Content-Type": "application/json" },
8695
+ body: JSON.stringify({
8696
+ provider_id: failure.providerId,
8697
+ model_id: failure.modelId,
8698
+ reason: failure.reason
8699
+ })
8700
+ }
8701
+ );
8702
+ if (!res.ok) {
8703
+ this.log({
8704
+ level: "warn",
8705
+ 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)})`,
8706
+ conversation_id: conversationId,
8707
+ message_id: messageId
8708
+ });
8709
+ }
8710
+ } catch (err) {
8711
+ this.log({
8712
+ level: "warn",
8713
+ 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)}`,
8714
+ conversation_id: conversationId,
8715
+ message_id: messageId
8716
+ });
8717
+ }
8718
+ }
8719
+ async clearSubagentModelAuthFailure(providerId, conversationId, messageId) {
8720
+ try {
8721
+ const res = await this.fetchImpl(
8722
+ `${this.apiUrl}/runners/${this.agentId}/model-auth-failures/${encodeURIComponent(providerId)}`,
8723
+ {
8724
+ method: "DELETE",
8725
+ headers: { Authorization: this.getAuthHeader() }
8726
+ }
8727
+ );
8728
+ if (!res.ok) {
8729
+ this.log({
8730
+ level: "warn",
8731
+ message: `Sub-agent model-auth clear for provider ${providerId} returned HTTP ${res.status} (conversation ${conversationId.slice(0, 8)}, message ${messageId.slice(0, 8)})`,
8732
+ conversation_id: conversationId,
8733
+ message_id: messageId
8734
+ });
8735
+ }
8736
+ } catch (err) {
8737
+ this.log({
8738
+ level: "warn",
8739
+ 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)}`,
8740
+ conversation_id: conversationId,
8741
+ message_id: messageId
8742
+ });
8743
+ }
8744
+ }
8745
+ async reportSubagentAuthFailures(conversationId, opencodeMessageId, evidentMessageId, messages) {
8746
+ const refs = collectSubagentSessions(messages, opencodeMessageId);
8747
+ if (refs.length === 0) return;
8748
+ const failedProviders = /* @__PURE__ */ new Map();
8749
+ const succeededProviders = /* @__PURE__ */ new Set();
8750
+ for (const ref of refs) {
8751
+ try {
8752
+ const childMessages = await getSessionMessages(this.port, ref.sessionId);
8753
+ if (childMessages === null) {
8754
+ this.log({
8755
+ level: "debug",
8756
+ message: `Could not read sub-agent session ${ref.sessionId} while checking credential failures \u2014 skipping it`,
8757
+ conversation_id: conversationId,
8758
+ message_id: evidentMessageId
8759
+ });
8760
+ continue;
8761
+ }
8762
+ const outcome = findSubagentAuthOutcome(childMessages, ref.startedAtMs);
8763
+ if (!outcome) continue;
8764
+ if (outcome.outcome === "failed") {
8765
+ failedProviders.set(outcome.providerId, outcome.failure);
8766
+ } else {
8767
+ succeededProviders.add(outcome.providerId);
8768
+ }
8769
+ } catch (err) {
8770
+ this.log({
8771
+ level: "warn",
8772
+ 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)}`,
8773
+ conversation_id: conversationId,
8774
+ message_id: evidentMessageId
8775
+ });
8776
+ }
8777
+ }
8778
+ for (const [providerId, failure] of failedProviders) {
8779
+ this.log({
8780
+ level: "warn",
8781
+ message: `Sub-agent turn failed on provider ${providerId} (${failure.reason}) \u2014 recording credential evidence`,
8782
+ conversation_id: conversationId,
8783
+ message_id: evidentMessageId
8784
+ });
8785
+ await this.recordSubagentModelAuthFailure(failure, conversationId, evidentMessageId);
8786
+ }
8787
+ for (const providerId of succeededProviders) {
8788
+ if (failedProviders.has(providerId)) continue;
8789
+ await this.clearSubagentModelAuthFailure(providerId, conversationId, evidentMessageId);
8790
+ }
8791
+ }
8350
8792
  /**
8351
8793
  * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
8352
8794
  * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
@@ -8629,7 +9071,7 @@ Port ${port} is already in use.`));
8629
9071
  }
8630
9072
 
8631
9073
  // src/lib/runner-credentials.ts
8632
- import { chmodSync as chmodSync2, writeFileSync as writeFileSync3 } from "fs";
9074
+ import { chmodSync as chmodSync2, writeFileSync as writeFileSync4 } from "fs";
8633
9075
  import { spawn as spawn5 } from "child_process";
8634
9076
  var RUNNER_SECRET_FETCH_TIMEOUT_MS = 6e4;
8635
9077
  var CREDENTIAL_RESTORE_TIMEOUT_MS = 6e4;
@@ -8870,8 +9312,8 @@ async function configureGitHubAccess({ env, log: log3 }) {
8870
9312
  }
8871
9313
  try {
8872
9314
  env.GIT_CONFIG_GLOBAL = GIT_CONFIG_GLOBAL;
8873
- writeFileSync3(GIT_CONFIG_GLOBAL, "");
8874
- writeFileSync3(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
9315
+ writeFileSync4(GIT_CONFIG_GLOBAL, "");
9316
+ writeFileSync4(GIT_CREDENTIAL_HELPER, GIT_CREDENTIAL_HELPER_CONTENT, { mode: 448 });
8875
9317
  chmodSync2(GIT_CREDENTIAL_HELPER, 448);
8876
9318
  const config = [
8877
9319
  ["user.name", env.GIT_USER_NAME ?? "evident-bot"],
@@ -8904,7 +9346,7 @@ async function configureGitHubAccess({ env, log: log3 }) {
8904
9346
  // src/lib/opencode/config-overlay.ts
8905
9347
  import { execFileSync as execFileSync2 } from "child_process";
8906
9348
  import { copyFileSync, existsSync as existsSync2, statSync as statSync5 } from "fs";
8907
- import { isAbsolute as isAbsolute2, join as join7, resolve as resolve3 } from "path";
9349
+ import { isAbsolute as isAbsolute2, join as join8, resolve as resolve3 } from "path";
8908
9350
  function isFile(filePath) {
8909
9351
  return existsSync2(filePath) && statSync5(filePath).isFile();
8910
9352
  }
@@ -8918,7 +9360,7 @@ function applyRunnerOpenCodeConfig({
8918
9360
  return;
8919
9361
  }
8920
9362
  const source = isAbsolute2(overlayPath) ? overlayPath : resolve3(cwd, overlayPath);
8921
- const target = isFile(join7(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
9363
+ const target = isFile(join8(cwd, "opencode.jsonc")) ? "opencode.jsonc" : "opencode.json";
8922
9364
  if (!isFile(source)) {
8923
9365
  log3(
8924
9366
  `RUNNER-OPENCODE-CONFIG-MISSING: ${source} is not a file; headless turns will wedge on the first external-directory permission prompt (#563)`,
@@ -8926,7 +9368,7 @@ function applyRunnerOpenCodeConfig({
8926
9368
  );
8927
9369
  return;
8928
9370
  }
8929
- copyFileSync(source, join7(cwd, target));
9371
+ copyFileSync(source, join8(cwd, target));
8930
9372
  try {
8931
9373
  execFileSync2("git", ["-C", cwd, "update-index", "--skip-worktree", target], {
8932
9374
  stdio: "ignore"
@@ -8935,7 +9377,242 @@ function applyRunnerOpenCodeConfig({
8935
9377
  const detail = error2 instanceof Error ? error2.message : String(error2);
8936
9378
  log3(`could not mark ${target} skip-worktree: ${detail}; it may show as a local change`, "warn");
8937
9379
  }
8938
- log3(`Applied runner OpenCode config ${source} to ${join7(cwd, target)}`);
9380
+ log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
9381
+ }
9382
+
9383
+ // src/lib/credential-sync.ts
9384
+ import { renameSync, writeFileSync as writeFileSync5 } from "fs";
9385
+ var CREDENTIAL_SYNC_TICK_TIMEOUT_MS = 3e4;
9386
+ var CREDENTIAL_FLUSH_DEADLINE_MS = 8e3;
9387
+ var CREDENTIAL_FLUSH_ABORT_GRACE_MS = 500;
9388
+ var DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS = 60;
9389
+ var STORES = ["claude", "opencode"];
9390
+ var MAX_FLUSH_PASSES = 2;
9391
+ function outcomesWith(outcome) {
9392
+ return { claude: outcome, opencode: outcome };
9393
+ }
9394
+ function errorMessage(error2) {
9395
+ return error2 instanceof Error ? error2.message : String(error2);
9396
+ }
9397
+ function waitForSettlement(promise, timeoutMs) {
9398
+ return new Promise((resolve4) => {
9399
+ let settled = false;
9400
+ const timer = setTimeout(() => finish(false), Math.max(0, timeoutMs));
9401
+ const finish = (value) => {
9402
+ if (settled) return;
9403
+ settled = true;
9404
+ clearTimeout(timer);
9405
+ resolve4(value);
9406
+ };
9407
+ promise.then(
9408
+ () => finish(true),
9409
+ () => finish(true)
9410
+ );
9411
+ });
9412
+ }
9413
+ function writeMarker(markerPath, outcomes, log3) {
9414
+ const body = `${STORES.map((store) => `${store}=${outcomes[store]}`).join("\n")}
9415
+ `;
9416
+ const temporaryPath = `${markerPath}.tmp`;
9417
+ try {
9418
+ writeFileSync5(temporaryPath, body, { mode: 384 });
9419
+ renameSync(temporaryPath, markerPath);
9420
+ } catch (error2) {
9421
+ log3(`CREDENTIAL-FLUSH-MARKER-UNWRITABLE: ${markerPath}: ${errorMessage(error2)}`, "warn");
9422
+ }
9423
+ }
9424
+ function intervalSeconds(env, log3) {
9425
+ const raw = env.CREDS_SYNC_INTERVAL;
9426
+ if (raw === void 0 || /^[1-9][0-9]*$/.test(raw) && Number.isSafeInteger(Number(raw))) {
9427
+ return raw === void 0 ? DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS : Number(raw);
9428
+ }
9429
+ log3(
9430
+ `CREDENTIAL-SYNC-INTERVAL-INVALID: CREDS_SYNC_INTERVAL='${raw}' is not a positive integer; using ${DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS}s`,
9431
+ "warn"
9432
+ );
9433
+ return DEFAULT_CREDENTIAL_SYNC_INTERVAL_SECONDS;
9434
+ }
9435
+ async function runFlushStore(store, env, synchroniserRunner, log3, deadlineAt) {
9436
+ const remainingMs = deadlineAt - Date.now();
9437
+ if (remainingMs <= 0) return { outcome: "timeout", orphaned: false };
9438
+ const controller = new AbortController();
9439
+ let result;
9440
+ let failed = false;
9441
+ const completion = Promise.resolve().then(
9442
+ () => synchroniserRunner(["sync-once", store], {
9443
+ timeoutMs: remainingMs,
9444
+ env,
9445
+ signal: controller.signal
9446
+ })
9447
+ ).then(
9448
+ (value) => {
9449
+ result = value;
9450
+ },
9451
+ (error2) => {
9452
+ failed = true;
9453
+ log3(`CREDENTIAL-FLUSH-ERROR: ${store}: ${errorMessage(error2)}`, "warn");
9454
+ }
9455
+ );
9456
+ const abortTimer = setTimeout(() => controller.abort(), remainingMs);
9457
+ const settledBeforeDeadline = await waitForSettlement(completion, remainingMs);
9458
+ clearTimeout(abortTimer);
9459
+ if (!settledBeforeDeadline) {
9460
+ controller.abort();
9461
+ const settledAfterAbort = await waitForSettlement(completion, CREDENTIAL_FLUSH_ABORT_GRACE_MS);
9462
+ if (!settledAfterAbort) return { outcome: "timeout", orphaned: true };
9463
+ return { outcome: "timeout", orphaned: false };
9464
+ }
9465
+ if (failed || !result) return { outcome: "failed", orphaned: false };
9466
+ if (result.timedOut || Date.now() >= deadlineAt) {
9467
+ return { outcome: "timeout", orphaned: false };
9468
+ }
9469
+ return { outcome: result.code === 0 ? "ok" : "failed", orphaned: false };
9470
+ }
9471
+ function createCredentialSync({
9472
+ markerPath,
9473
+ env,
9474
+ log: log3,
9475
+ synchroniserRunner = runSynchroniser
9476
+ }) {
9477
+ const persistenceDisabled = !env.PERSISTENCE_BUCKET;
9478
+ let disabled = persistenceDisabled;
9479
+ let armed = false;
9480
+ let stopped = false;
9481
+ let timer;
9482
+ let inFlight;
9483
+ let activeTickAbort;
9484
+ let lastTickFailed;
9485
+ let flushPromise;
9486
+ const scheduleTick = (intervalMs, startTick2) => {
9487
+ if (stopped) return;
9488
+ timer = setTimeout(() => {
9489
+ timer = void 0;
9490
+ startTick2();
9491
+ }, intervalMs);
9492
+ };
9493
+ const startTick = (intervalMs) => {
9494
+ if (stopped) return;
9495
+ const controller = new AbortController();
9496
+ activeTickAbort = controller;
9497
+ const tick = (async () => {
9498
+ const outcomes = {
9499
+ claude: "failed",
9500
+ opencode: "failed"
9501
+ };
9502
+ for (const store of STORES) {
9503
+ if (controller.signal.aborted) break;
9504
+ try {
9505
+ const result = await synchroniserRunner(["sync-once", store], {
9506
+ timeoutMs: CREDENTIAL_SYNC_TICK_TIMEOUT_MS,
9507
+ env,
9508
+ signal: controller.signal
9509
+ });
9510
+ outcomes[store] = !result.timedOut && result.code === 0 ? "ok" : "failed";
9511
+ } catch (error2) {
9512
+ outcomes[store] = "failed";
9513
+ log3(`CREDENTIAL-SYNC-TICK-ERROR: ${store}: ${errorMessage(error2)}`, "debug");
9514
+ }
9515
+ }
9516
+ const failed = STORES.some((store) => outcomes[store] === "failed");
9517
+ log3(
9518
+ `CREDENTIAL-SYNC-TICK: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
9519
+ "debug"
9520
+ );
9521
+ if (failed && lastTickFailed !== true) {
9522
+ log3(
9523
+ "CREDENTIAL-SYNC-FAILED: an interval credential sync failed; retrying on the next tick",
9524
+ "warn"
9525
+ );
9526
+ } else if (!failed && lastTickFailed === true) {
9527
+ log3("CREDENTIAL-SYNC-RECOVERED: interval credential sync succeeded again", "warn");
9528
+ }
9529
+ lastTickFailed = failed;
9530
+ })().finally(() => {
9531
+ if (activeTickAbort === controller) activeTickAbort = void 0;
9532
+ if (inFlight === tick) inFlight = void 0;
9533
+ scheduleTick(intervalMs, () => startTick(intervalMs));
9534
+ });
9535
+ inFlight = tick;
9536
+ };
9537
+ const performFlush = async () => {
9538
+ stopped = true;
9539
+ if (timer) {
9540
+ clearTimeout(timer);
9541
+ timer = void 0;
9542
+ }
9543
+ const deadlineAt = Date.now() + CREDENTIAL_FLUSH_DEADLINE_MS;
9544
+ if (inFlight) {
9545
+ const settled = await waitForSettlement(inFlight, CREDENTIAL_FLUSH_DEADLINE_MS);
9546
+ if (!settled) {
9547
+ activeTickAbort?.abort();
9548
+ const settledAfterAbort = await waitForSettlement(
9549
+ inFlight,
9550
+ CREDENTIAL_FLUSH_ABORT_GRACE_MS
9551
+ );
9552
+ if (!settledAfterAbort) {
9553
+ log3(
9554
+ "CREDENTIAL-FLUSH-ORPHANED-TICK: a sync-once child could not be proven settled; skipping the boundary flush rather than becoming a second writer",
9555
+ "warn"
9556
+ );
9557
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
9558
+ }
9559
+ }
9560
+ }
9561
+ if (disabled) return { outcomes: outcomesWith("skipped"), orphaned: false };
9562
+ const outcomes = outcomesWith("timeout");
9563
+ for (const store of STORES) {
9564
+ const result = await runFlushStore(store, env, synchroniserRunner, log3, deadlineAt);
9565
+ if (result.orphaned) {
9566
+ log3(
9567
+ "CREDENTIAL-FLUSH-ORPHANED-CHILD: a flush sync-once child could not be proven settled; refusing another flush pass rather than becoming a second writer",
9568
+ "warn"
9569
+ );
9570
+ return { outcomes: outcomesWith("timeout"), orphaned: true };
9571
+ }
9572
+ outcomes[store] = result.outcome;
9573
+ }
9574
+ return { outcomes, orphaned: false };
9575
+ };
9576
+ let flushPasses = 0;
9577
+ let lastFlush;
9578
+ return {
9579
+ arm() {
9580
+ if (stopped || armed) return;
9581
+ armed = true;
9582
+ if (persistenceDisabled) {
9583
+ disabled = true;
9584
+ log3(
9585
+ "CREDENTIAL-SYNC-DISABLED: LITESTREAM_BUCKET/LITESTREAM_PREFIX are not both set; no interval credential sync this boot",
9586
+ "warn"
9587
+ );
9588
+ return;
9589
+ }
9590
+ disabled = false;
9591
+ const intervalMs = intervalSeconds(env, log3) * 1e3;
9592
+ scheduleTick(intervalMs, () => startTick(intervalMs));
9593
+ },
9594
+ async stopAndFlush(publish) {
9595
+ let result;
9596
+ const runningFlush = flushPromise;
9597
+ if (runningFlush) {
9598
+ result = await runningFlush;
9599
+ } else if (flushPasses >= MAX_FLUSH_PASSES || lastFlush?.orphaned) {
9600
+ result = lastFlush ?? { outcomes: outcomesWith("timeout"), orphaned: true };
9601
+ } else {
9602
+ flushPasses++;
9603
+ const currentFlush = performFlush();
9604
+ flushPromise = currentFlush;
9605
+ try {
9606
+ result = await currentFlush;
9607
+ lastFlush = result;
9608
+ } finally {
9609
+ if (flushPromise === currentFlush) flushPromise = void 0;
9610
+ }
9611
+ }
9612
+ if (publish) writeMarker(markerPath, result.outcomes, log3);
9613
+ return result.outcomes;
9614
+ }
9615
+ };
8939
9616
  }
8940
9617
 
8941
9618
  // src/commands/run.ts
@@ -8975,7 +9652,7 @@ function resolveFileSyncDirectories(raw, homeDir) {
8975
9652
  if (trimmed === "") {
8976
9653
  throw new Error("--enable-file-sync-to requires a directory path (got an empty value)");
8977
9654
  }
8978
- const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join8(homeDir, trimmed.slice(2)) : trimmed;
9655
+ const expanded = trimmed === "~" ? homeDir : trimmed.startsWith("~/") ? join9(homeDir, trimmed.slice(2)) : trimmed;
8979
9656
  if (!isAbsolute3(expanded)) {
8980
9657
  throw new Error(`--enable-file-sync-to requires an absolute directory path; got "${entry}"`);
8981
9658
  }
@@ -9235,6 +9912,10 @@ async function driveChannels(state, driver) {
9235
9912
  consecutiveDrainFailures = 0;
9236
9913
  unreachableMs = 0;
9237
9914
  state.messageCount += processed;
9915
+ if (driver.recycleRequested) {
9916
+ await beginGracefulShutdown(state, "recycle");
9917
+ return;
9918
+ }
9238
9919
  const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
9239
9920
  lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
9240
9921
  const fileActivitySnapshot = driver.fileSyncActivity();
@@ -9277,8 +9958,8 @@ async function driveChannels(state, driver) {
9277
9958
  state.running = false;
9278
9959
  break;
9279
9960
  }
9280
- const errorMessage = error2 instanceof Error ? error2.message : String(error2);
9281
- logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage}` });
9961
+ const errorMessage2 = error2 instanceof Error ? error2.message : String(error2);
9962
+ logActivity(state, { type: "error", error: `Channel processing error: ${errorMessage2}` });
9282
9963
  if (state.interactive) displayStatus(state);
9283
9964
  if (driver.hasInFlightWatchers()) {
9284
9965
  consecutiveDrainFailures = 0;
@@ -9318,7 +9999,43 @@ async function driveChannels(state, driver) {
9318
9999
  var SESSION_CLEANUP_FIRST_SWEEP_MS = 1e4;
9319
10000
  var SESSION_DB_RECLAIM_MAX_PAGES = 2e3;
9320
10001
  function sessionDbPath() {
9321
- return join8(homedir5(), ".local", "share", "opencode", "opencode.db");
10002
+ return join9(homedir5(), ".local", "share", "opencode", "opencode.db");
10003
+ }
10004
+ function logSessionDbProvenanceMismatch(state, provenance, currentVersion, preBootMigrationCount) {
10005
+ const record = {
10006
+ v: 1,
10007
+ event: "session_db_recovery",
10008
+ at: (/* @__PURE__ */ new Date()).toISOString(),
10009
+ stage: "verify",
10010
+ outcome: "schema_provenance_mismatch",
10011
+ severity: "error",
10012
+ reason: provenance.reason ?? "schema-provenance-mismatch",
10013
+ litestream_exit_code: null,
10014
+ attempt: null,
10015
+ replica_objects: null,
10016
+ replica_bytes: null,
10017
+ quarantine_destination: null,
10018
+ quarantined_objects: null,
10019
+ quarantine_failed_objects: null,
10020
+ quarantined_bytes: null,
10021
+ verified_restore_point: null,
10022
+ restore_points_tried: null,
10023
+ provenance_reason: provenance.reason,
10024
+ provenance_migration_delta: provenance.migrationDelta,
10025
+ replication_suspended: false,
10026
+ dbPath: sessionDbPath(),
10027
+ recorded_version: provenance.recordedVersion,
10028
+ current_version: currentVersion,
10029
+ provenance_pre_boot_migration_count: preBootMigrationCount
10030
+ };
10031
+ const activity = buildSessionDbRecoveryActivity(record);
10032
+ if (!activity) throw new Error("could not map session-DB provenance activity");
10033
+ logActivity(state, {
10034
+ type: activity.level === "error" ? "error" : "info",
10035
+ level: activity.level,
10036
+ ...activity.level === "error" ? { error: activity.message } : { message: activity.message },
10037
+ metadata: activity.metadata
10038
+ });
9322
10039
  }
9323
10040
  async function runSweep(state, driver, config) {
9324
10041
  const mode = `age=${config.maxAgeMs ?? "\u2014"} count=${config.maxCount ?? "\u2014"}`;
@@ -9365,7 +10082,7 @@ async function runSweep(state, driver, config) {
9365
10082
  const reclaimResult = await reclaimSessionDbSpace({
9366
10083
  dbPath: sessionDbPath(),
9367
10084
  maxPages: SESSION_DB_RECLAIM_MAX_PAGES,
9368
- allowFullVacuum: protectedNow.size === 0
10085
+ allowFullVacuum: protectedNow.size === 0 && !state.sessionDbProvenanceAnomaly
9369
10086
  });
9370
10087
  if (reclaimResult.ok) {
9371
10088
  const beforeMib = (reclaimResult.beforeBytes / 1024 / 1024).toFixed(1);
@@ -9602,7 +10319,8 @@ function scheduleResourceUsageReporting(state, options) {
9602
10319
  });
9603
10320
  return;
9604
10321
  }
9605
- const collect = createResourceUsageCollector(homedir5());
10322
+ const { collect, stop } = createResourceUsageCollector(homedir5());
10323
+ state.stopResourceUsageSampling = stop;
9606
10324
  let consecutiveFailures = 0;
9607
10325
  const tick = async () => {
9608
10326
  try {
@@ -9712,21 +10430,41 @@ async function cleanup(state, opts = {}) {
9712
10430
  clearTimeout(state.resourceUsageTimer);
9713
10431
  state.resourceUsageTimer = null;
9714
10432
  }
10433
+ state.stopResourceUsageSampling?.();
10434
+ state.stopResourceUsageSampling = null;
10435
+ const credentialSync = state.credentialSync;
10436
+ const flushCredentials = credentialSync ? async (phase, publish) => {
10437
+ await timeShutdownPhase(state, durations, phase, async () => {
10438
+ const outcomes = await credentialSync.stopAndFlush(publish);
10439
+ const level = Object.values(outcomes).every((outcome) => outcome === "ok") ? "info" : "warn";
10440
+ log2(
10441
+ state,
10442
+ `Credential flush: claude=${outcomes.claude}, opencode=${outcomes.opencode}`,
10443
+ level
10444
+ );
10445
+ });
10446
+ } : void 0;
10447
+ let drainSettled = true;
9715
10448
  if (opts.graceful && state.channelDriver) {
9716
10449
  state.channelDriver.stop();
10450
+ }
10451
+ if (flushCredentials) {
10452
+ await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
10453
+ }
10454
+ if (opts.graceful && state.channelDriver) {
9717
10455
  log2(state, "Draining in-flight channel work before shutdown...");
9718
10456
  if (state.interactive) {
9719
10457
  logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
9720
10458
  displayStatus(state);
9721
10459
  }
9722
10460
  const driver = state.channelDriver;
9723
- const settled = await timeShutdownPhase(
10461
+ drainSettled = await timeShutdownPhase(
9724
10462
  state,
9725
10463
  durations,
9726
10464
  "drain",
9727
10465
  () => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
9728
10466
  );
9729
- if (!settled) {
10467
+ if (!drainSettled) {
9730
10468
  logActivity(state, {
9731
10469
  type: "info",
9732
10470
  message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
@@ -9734,6 +10472,9 @@ async function cleanup(state, opts = {}) {
9734
10472
  if (state.interactive) displayStatus(state);
9735
10473
  }
9736
10474
  }
10475
+ if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
10476
+ await flushCredentials("credential_flush_final", true);
10477
+ }
9737
10478
  await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
9738
10479
  if (state.connection) {
9739
10480
  const connection = state.connection;
@@ -9769,6 +10510,44 @@ async function cleanup(state, opts = {}) {
9769
10510
  }
9770
10511
  return durations;
9771
10512
  }
10513
+ async function beginGracefulShutdown(state, trigger) {
10514
+ if (state.shuttingDown) return;
10515
+ state.shuttingDown = true;
10516
+ const shutdownStartedAt = Date.now();
10517
+ const shutdownMessage = trigger === "recycle" ? "Recycle requested \u2014 finishing in-flight work, then exiting" : "Shutting down...";
10518
+ if (state.interactive) {
10519
+ logActivity(state, { type: "info", message: shutdownMessage });
10520
+ displayStatus(state);
10521
+ } else {
10522
+ log2(state, shutdownMessage);
10523
+ }
10524
+ const durations = await cleanup(state, { graceful: true });
10525
+ const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
10526
+ await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
10527
+ let timer;
10528
+ const flushed = shutdownTelemetry().then(
10529
+ () => true,
10530
+ (error2) => {
10531
+ log2(
10532
+ state,
10533
+ `Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
10534
+ "warn"
10535
+ );
10536
+ return true;
10537
+ }
10538
+ );
10539
+ const timedOut = new Promise((resolve4) => {
10540
+ timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
10541
+ });
10542
+ if (!await Promise.race([flushed, timedOut])) {
10543
+ log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
10544
+ }
10545
+ clearTimeout(timer);
10546
+ });
10547
+ const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
10548
+ log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
10549
+ process.exit(0);
10550
+ }
9772
10551
  async function run(options) {
9773
10552
  const interactive = isInteractive(options.json);
9774
10553
  let logLevel;
@@ -9804,6 +10583,7 @@ async function run(options) {
9804
10583
  connected: false,
9805
10584
  opencodeConnected: false,
9806
10585
  opencodeVersion: null,
10586
+ sessionDbProvenanceAnomaly: false,
9807
10587
  opencodeProcess: null,
9808
10588
  litestreamProcess: null,
9809
10589
  connection: null,
@@ -9819,9 +10599,24 @@ async function run(options) {
9819
10599
  openaiUsageTimer: null,
9820
10600
  openaiUsageRearm: null,
9821
10601
  resourceUsageTimer: null,
10602
+ stopResourceUsageSampling: null,
10603
+ credentialSync: null,
9822
10604
  authHeader: ""
9823
10605
  };
9824
10606
  setTelemetryAuthProvider(() => ({ authHeader: state.authHeader, agentId: state.agentId }));
10607
+ if (options.credentialSyncMarker) {
10608
+ state.credentialSync = createCredentialSync({
10609
+ markerPath: options.credentialSyncMarker,
10610
+ env: process.env,
10611
+ log: (message, level = "info") => {
10612
+ if (level === "error") {
10613
+ logActivity(state, { type: "error", error: message });
10614
+ } else {
10615
+ logActivity(state, { type: "info", level, message });
10616
+ }
10617
+ }
10618
+ });
10619
+ }
9825
10620
  if (fileSyncDirectories.length > 0) {
9826
10621
  log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
9827
10622
  } else {
@@ -9847,43 +10642,7 @@ async function run(options) {
9847
10642
  "warn"
9848
10643
  );
9849
10644
  }
9850
- const handleSignal = async () => {
9851
- if (state.shuttingDown) return;
9852
- state.shuttingDown = true;
9853
- const shutdownStartedAt = Date.now();
9854
- if (state.interactive) {
9855
- logActivity(state, { type: "info", message: "Shutting down..." });
9856
- displayStatus(state);
9857
- } else {
9858
- log2(state, "Shutting down...");
9859
- }
9860
- const durations = await cleanup(state, { graceful: true });
9861
- const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
9862
- await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
9863
- let timer;
9864
- const flushed = shutdownTelemetry().then(
9865
- () => true,
9866
- (error2) => {
9867
- log2(
9868
- state,
9869
- `Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
9870
- "warn"
9871
- );
9872
- return true;
9873
- }
9874
- );
9875
- const timedOut = new Promise((resolve4) => {
9876
- timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
9877
- });
9878
- if (!await Promise.race([flushed, timedOut])) {
9879
- log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
9880
- }
9881
- clearTimeout(timer);
9882
- });
9883
- const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
9884
- log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
9885
- process.exit(0);
9886
- };
10645
+ const handleSignal = () => beginGracefulShutdown(state, "signal");
9887
10646
  process.on("SIGINT", handleSignal);
9888
10647
  process.on("SIGTERM", handleSignal);
9889
10648
  try {
@@ -10029,6 +10788,7 @@ async function run(options) {
10029
10788
  await restoreCredentialStores(credentialContext);
10030
10789
  if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
10031
10790
  }
10791
+ state.credentialSync?.arm();
10032
10792
  let sessionDbVerifyFatal = false;
10033
10793
  if (!options.restoreSessionDb) {
10034
10794
  log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
@@ -10082,6 +10842,7 @@ async function run(options) {
10082
10842
  for (const warning2 of maxActiveSessionsWarnings) {
10083
10843
  logActivity(state, { type: "info", level: "warn", message: warning2 });
10084
10844
  }
10845
+ const preBootMigrationIds = readSessionDbMigrationIds(sessionDbPath());
10085
10846
  const ocSpinner = interactive && !state.json ? ora3("Checking OpenCode...").start() : null;
10086
10847
  try {
10087
10848
  const oc = await ensureOpenCodeRunning({
@@ -10097,7 +10858,7 @@ async function run(options) {
10097
10858
  state.opencodeVersion = oc.version;
10098
10859
  if (options.opencodePidFile && oc.process?.pid !== void 0) {
10099
10860
  try {
10100
- writeFileSync4(options.opencodePidFile, `${oc.process.pid}
10861
+ writeFileSync6(options.opencodePidFile, `${oc.process.pid}
10101
10862
  `, { mode: 384 });
10102
10863
  chmodSync3(options.opencodePidFile, 384);
10103
10864
  } catch (error2) {
@@ -10107,6 +10868,23 @@ async function run(options) {
10107
10868
  });
10108
10869
  }
10109
10870
  }
10871
+ if (state.opencodeVersion !== null) {
10872
+ const provenance = checkSessionDbProvenance({
10873
+ dbPath: sessionDbPath(),
10874
+ currentVersion: state.opencodeVersion,
10875
+ homeDir: homedir5(),
10876
+ env: process.env
10877
+ });
10878
+ if (provenance.anomaly) {
10879
+ state.sessionDbProvenanceAnomaly = true;
10880
+ logSessionDbProvenanceMismatch(
10881
+ state,
10882
+ provenance,
10883
+ state.opencodeVersion,
10884
+ preBootMigrationIds?.length ?? null
10885
+ );
10886
+ }
10887
+ }
10110
10888
  state.opencodeConnected = oc.notReadyReason === null;
10111
10889
  const version2 = state.opencodeVersion ? ` (v${state.opencodeVersion})` : "";
10112
10890
  ocSpinner?.succeed(`OpenCode running on port ${state.port}${version2}`);
@@ -10159,7 +10937,7 @@ async function run(options) {
10159
10937
  let existingPid;
10160
10938
  if (existsSync3(options.litestreamPidFile)) {
10161
10939
  try {
10162
- const rawPid = readFileSync5(options.litestreamPidFile, "utf8").trim();
10940
+ const rawPid = readFileSync6(options.litestreamPidFile, "utf8").trim();
10163
10941
  const parsedPid = Number(rawPid);
10164
10942
  if (/^\d+$/.test(rawPid) && Number.isSafeInteger(parsedPid) && parsedPid > 0) {
10165
10943
  existingPid = parsedPid;
@@ -10196,7 +10974,7 @@ async function run(options) {
10196
10974
  });
10197
10975
  try {
10198
10976
  if (litestreamProcess.pid !== void 0) {
10199
- writeFileSync4(options.litestreamPidFile, `${litestreamProcess.pid}
10977
+ writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
10200
10978
  `, {
10201
10979
  mode: 384
10202
10980
  });
@@ -10382,6 +11160,18 @@ async function run(options) {
10382
11160
  if (state.interactive) displayStatus(state);
10383
11161
  });
10384
11162
  },
11163
+ // Both loops are rearmed because `rearm()` is idempotent for the
11164
+ // provider that did not just connect, and is a no-op when reporting is off.
11165
+ onUsageRearmPing: () => {
11166
+ if (!state.running) return;
11167
+ logActivity(state, {
11168
+ type: "info",
11169
+ level: "debug",
11170
+ message: "Usage rearm ping received"
11171
+ });
11172
+ state.claudeUsageRearm?.();
11173
+ state.openaiUsageRearm?.();
11174
+ },
10385
11175
  onInfo: (message) => logActivity(state, { type: "info", message })
10386
11176
  }
10387
11177
  });
@@ -10402,7 +11192,17 @@ async function run(options) {
10402
11192
  setTimer: (timer) => {
10403
11193
  state.openaiUsageTimer = timer;
10404
11194
  },
10405
- fetchUsage: () => getOpenAiUsage(state.port),
11195
+ fetchUsage: async () => {
11196
+ const usage = await getOpenAiUsage(state.port);
11197
+ if (usage.subscription === null) {
11198
+ logActivity(state, {
11199
+ type: "info",
11200
+ level: "debug",
11201
+ message: "OpenAI usage subscription could not be identified from the local credential"
11202
+ });
11203
+ }
11204
+ return usage;
11205
+ },
10406
11206
  report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
10407
11207
  isLocalCredentialProblem: isLocalCredentialProblem2,
10408
11208
  forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
@@ -10448,7 +11248,7 @@ async function run(options) {
10448
11248
  }
10449
11249
 
10450
11250
  // src/index.ts
10451
- var { version } = createRequire(import.meta.url)("../package.json");
11251
+ var { version } = createRequire2(import.meta.url)("../package.json");
10452
11252
  var program = new Command();
10453
11253
  program.name("evident").description("Run OpenCode locally and connect it to Evident").version(version).option(
10454
11254
  "--endpoint <url>",
@@ -10526,6 +11326,9 @@ program.command("run").description("Connect to Evident and process messages").op
10526
11326
  ).option(
10527
11327
  "--opencode-config-overlay <path>",
10528
11328
  "Apply this runner-provided OpenCode config before starting OpenCode."
11329
+ ).option(
11330
+ "--credential-sync-marker <path>",
11331
+ "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."
10529
11332
  ).action(
10530
11333
  (options) => {
10531
11334
  run({
@@ -10564,7 +11367,8 @@ program.command("run").description("Connect to Evident and process messages").op
10564
11367
  sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
10565
11368
  restoreSessionDb: options.restoreSessionDb,
10566
11369
  restoreRunnerCredentials: options.restoreRunnerCredentials,
10567
- opencodeConfigOverlay: options.opencodeConfigOverlay
11370
+ opencodeConfigOverlay: options.opencodeConfigOverlay,
11371
+ credentialSyncMarker: options.credentialSyncMarker
10568
11372
  });
10569
11373
  }
10570
11374
  );