@evident-ai/cli 3.4.1-dev.c03b461 → 3.4.1-dev.c138c09

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
@@ -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,7 +1192,7 @@ async function claudeUsage() {
1183
1192
  }
1184
1193
 
1185
1194
  // src/commands/run.ts
1186
- import { chmodSync as chmodSync3, existsSync as existsSync3, readFileSync as readFileSync6, writeFileSync as writeFileSync5 } 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
1197
  import { isAbsolute as isAbsolute3, join as join9, parse, resolve as resolvePath2 } from "path";
1189
1198
  import chalk6 from "chalk";
@@ -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;
@@ -1728,10 +1738,14 @@ function runSynchroniser(args, opts) {
1728
1738
  let stderr = "";
1729
1739
  let settled = false;
1730
1740
  const timer = {};
1741
+ let abortListener;
1742
+ let spawnListener;
1731
1743
  const finish = (result) => {
1732
1744
  if (settled) return;
1733
1745
  settled = true;
1734
1746
  if (timer.handle) clearTimeout(timer.handle);
1747
+ if (abortListener) opts.signal?.removeEventListener("abort", abortListener);
1748
+ if (spawnListener) child.removeListener("spawn", spawnListener);
1735
1749
  resolve4(result);
1736
1750
  };
1737
1751
  try {
@@ -1757,6 +1771,25 @@ function runSynchroniser(args, opts) {
1757
1771
  child.once("close", (code) => {
1758
1772
  finish({ code, stdout, stderr, timedOut: false });
1759
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
+ }
1760
1793
  timer.handle = setTimeout(
1761
1794
  () => {
1762
1795
  child.kill("SIGKILL");
@@ -3119,6 +3152,44 @@ function findLastAssistantReplyFor(messages, userMessageId) {
3119
3152
  }
3120
3153
  return lastOk ?? last;
3121
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
+ }
3122
3193
  function messageUsage(messages, userMessageId) {
3123
3194
  if (!messages || messages.length === 0) return null;
3124
3195
  const byParentAll = messages.filter(
@@ -3247,8 +3318,7 @@ function isAbortedTerminalReply(messages, userMessageId) {
3247
3318
  }
3248
3319
  return false;
3249
3320
  }
3250
- function messageFailure(messages, userMessageId) {
3251
- const reply = findLastAssistantReplyFor(messages, userMessageId);
3321
+ function classifyReplyAuthError(reply) {
3252
3322
  const error2 = errorOf(reply);
3253
3323
  if (error2 == null || typeof error2 !== "object") return null;
3254
3324
  const e = error2;
@@ -3273,6 +3343,32 @@ function messageFailure(messages, userMessageId) {
3273
3343
  }
3274
3344
  return null;
3275
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
+ }
3276
3372
  function applyZeroProviderFallback(classified, hasConfiguredProvider, replyProviderId, replyModelId = null) {
3277
3373
  if (classified != null) return classified;
3278
3374
  if (hasConfiguredProvider !== false) return null;
@@ -3603,7 +3699,6 @@ var StreamForwarder = class {
3603
3699
  handleFrame(frame) {
3604
3700
  switch (frame.type) {
3605
3701
  case "open":
3606
- this.callbacks.onOpen?.(frame.sid, frame.method, frame.path);
3607
3702
  void this.handleOpen(frame);
3608
3703
  break;
3609
3704
  case "req_data":
@@ -3639,12 +3734,21 @@ var StreamForwarder = class {
3639
3734
  const { sid, method, path, headers, has_body } = frame;
3640
3735
  const correlationId = headers?.[CORRELATION_ID_HEADER];
3641
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
+ }
3642
3740
  if (path === TUNNEL_DRAIN_PING_PATH) {
3643
3741
  this.callbacks.onDrainPing?.();
3644
3742
  this.send({ type: "head", sid, status: 204, headers: {} });
3645
3743
  this.send({ type: "res_end", sid });
3646
3744
  return;
3647
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
+ }
3648
3752
  if (process.env.DEBUG) {
3649
3753
  log("debug", "agent_request", {
3650
3754
  correlation_id: correlationId,
@@ -3789,7 +3893,8 @@ function connectTunnel(options) {
3789
3893
  onResponse,
3790
3894
  onInfo,
3791
3895
  onWarning,
3792
- onDrainPing
3896
+ onDrainPing,
3897
+ onUsageRearmPing
3793
3898
  } = options;
3794
3899
  const tunnelUrl = getTunnelUrlConfig();
3795
3900
  const url = `${tunnelUrl}/tunnel/${agentId}/connect`;
@@ -3801,7 +3906,8 @@ function connectTunnel(options) {
3801
3906
  });
3802
3907
  const forwarder = new StreamForwarder(ws, port, {
3803
3908
  onHead: () => onResponse?.(),
3804
- onDrainPing: () => onDrainPing?.()
3909
+ onDrainPing: () => onDrainPing?.(),
3910
+ onUsageRearmPing: () => onUsageRearmPing?.()
3805
3911
  });
3806
3912
  const connectionTimeout = setTimeout(() => {
3807
3913
  ws.close();
@@ -3844,8 +3950,8 @@ function connectTunnel(options) {
3844
3950
  try {
3845
3951
  message = JSON.parse(data.toString());
3846
3952
  } catch (error2) {
3847
- const errorMessage = error2 instanceof Error ? error2.message : "Unknown error";
3848
- onError?.(`Failed to handle message: ${errorMessage}`);
3953
+ const errorMessage2 = error2 instanceof Error ? error2.message : "Unknown error";
3954
+ onError?.(`Failed to handle message: ${errorMessage2}`);
3849
3955
  return;
3850
3956
  }
3851
3957
  if (isStreamFrame(message)) {
@@ -3962,6 +4068,7 @@ var RunnerConnection = class {
3962
4068
  onError: (error2) => events.onError?.(error2),
3963
4069
  onResponse: () => events.onResponse?.(),
3964
4070
  onDrainPing: () => events.onDrainPing?.(),
4071
+ onUsageRearmPing: () => events.onUsageRearmPing?.(),
3965
4072
  onInfo: (message) => events.onInfo?.(message),
3966
4073
  onWarning: (message) => events.onWarning?.(message)
3967
4074
  });
@@ -4080,6 +4187,23 @@ function readOpenCodeChatGptCredentials() {
4080
4187
  return null;
4081
4188
  }
4082
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
+ }
4083
4207
  function toWindow2(headers, name) {
4084
4208
  const utilizationHeader = headers.get(`x-codex-${name}-used-percent`);
4085
4209
  const windowMinutesHeader = headers.get(`x-codex-${name}-window-minutes`);
@@ -4155,6 +4279,7 @@ async function getOpenAiUsage(port) {
4155
4279
  "credentials_expired"
4156
4280
  );
4157
4281
  }
4282
+ const subscription = parseChatGptIdentity(credentials2.accessToken);
4158
4283
  const models = await resolveProbeModels(port);
4159
4284
  if (models.length === 0) {
4160
4285
  throw new OpenAiUsageError("No supported OpenAI probe model is available.", "no_probe_model");
@@ -4187,7 +4312,7 @@ async function getOpenAiUsage(port) {
4187
4312
  "no_usable_window"
4188
4313
  );
4189
4314
  }
4190
- return usage;
4315
+ return { ...usage, subscription };
4191
4316
  }
4192
4317
  if (res.status === 401) {
4193
4318
  throw new OpenAiUsageError(
@@ -4388,45 +4513,84 @@ function readDisk(homeDir) {
4388
4513
  };
4389
4514
  }
4390
4515
  }
4391
- function createResourceUsageCollector(homeDir) {
4392
- let previous = readCpuSample();
4393
- 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(() => {
4394
4526
  const current = readCpuSample();
4395
- const hostCpuPercent = cpuPercentBetween(previous, current);
4396
- const hostCpuCount = cpus().length;
4397
- previous = current;
4398
- const disk = readDisk(homeDir);
4399
- const opencodeDbBytes = statSessionDbBytes(homeDir);
4400
- const { limits, warning: ecsWarning } = await readEcsTaskLimits(process.env);
4401
- const warnings = [];
4402
- if (disk.warning) warnings.push(disk.warning);
4403
- if (ecsWarning) warnings.push(ecsWarning);
4404
- let cpuPercent = hostCpuPercent;
4405
- let cpuCount = hostCpuCount;
4406
- let memoryTotalBytes = totalmem();
4407
- let memoryAvailableBytes = freemem();
4408
- if (limits !== null) {
4409
- cpuCount = limits.cpuCount;
4410
- memoryTotalBytes = limits.memoryTotalBytes;
4411
- memoryAvailableBytes = clamp(
4412
- limits.memoryTotalBytes - (totalmem() - freemem()),
4413
- 0,
4414
- limits.memoryTotalBytes
4415
- );
4416
- 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
+ }
4417
4533
  }
4418
- return {
4419
- usage: {
4420
- cpuPercent,
4421
- cpuCount,
4422
- memoryTotalBytes,
4423
- memoryAvailableBytes,
4424
- diskTotalBytes: disk.totalBytes,
4425
- diskFreeBytes: disk.freeBytes,
4426
- opencodeDbBytes
4427
- },
4428
- warnings
4429
- };
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
4430
4594
  };
4431
4595
  }
4432
4596
 
@@ -5249,6 +5413,7 @@ var ChannelDriver = class _ChannelDriver {
5249
5413
  * and stops opencode.
5250
5414
  */
5251
5415
  stopped = false;
5416
+ recycleRequestedFlag = false;
5252
5417
  constructor(config) {
5253
5418
  this.agentId = config.agentId;
5254
5419
  this.port = config.port;
@@ -5354,6 +5519,9 @@ var ChannelDriver = class _ChannelDriver {
5354
5519
  let dispatched = 0;
5355
5520
  try {
5356
5521
  const conversations = await this.getPendingConversations();
5522
+ if (this.recycleRequestedFlag) {
5523
+ this.stop();
5524
+ }
5357
5525
  if (conversations.length > 0) {
5358
5526
  const total = conversations.reduce((sum, c) => sum + (c.pending_message_count ?? 0), 0);
5359
5527
  this.log({
@@ -5482,6 +5650,14 @@ var ChannelDriver = class _ChannelDriver {
5482
5650
  stop() {
5483
5651
  this.stopped = true;
5484
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
+ }
5485
5661
  /**
5486
5662
  * Wait (up to `timeoutMs`) for in-flight watcher work to settle during a
5487
5663
  * graceful shutdown, so a turn whose reply is ready — or completes within the
@@ -5619,7 +5795,7 @@ var ChannelDriver = class _ChannelDriver {
5619
5795
  this.signalDispatchNotStarted(conv, message, "session_existence_unknown");
5620
5796
  break;
5621
5797
  }
5622
- const errorMessage = err instanceof Error ? err.message : String(err);
5798
+ const errorMessage2 = err instanceof Error ? err.message : String(err);
5623
5799
  this.sessions.delete(conv.id);
5624
5800
  this.supersede(conv.id, sessionId);
5625
5801
  this.log({
@@ -5628,7 +5804,7 @@ var ChannelDriver = class _ChannelDriver {
5628
5804
  conversation_id: conv.id,
5629
5805
  message_id: message.id
5630
5806
  });
5631
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5807
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
5632
5808
  this.log({
5633
5809
  level: "warn",
5634
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)}`,
@@ -5639,7 +5815,7 @@ var ChannelDriver = class _ChannelDriver {
5639
5815
  });
5640
5816
  this.log({
5641
5817
  level: "error",
5642
- message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage}`,
5818
+ message: `Message ${message.id.slice(0, 8)} dispatch failed: ${errorMessage2}`,
5643
5819
  conversation_id: conv.id,
5644
5820
  message_id: message.id
5645
5821
  });
@@ -5660,14 +5836,14 @@ var ChannelDriver = class _ChannelDriver {
5660
5836
  this.unconfirmedDispatchFailures.delete(message.id);
5661
5837
  this.sessions.delete(conv.id);
5662
5838
  this.supersede(conv.id, sessionId);
5663
- 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.`;
5664
5840
  this.log({
5665
5841
  level: "error",
5666
- message: errorMessage,
5842
+ message: errorMessage2,
5667
5843
  conversation_id: conv.id,
5668
5844
  message_id: message.id
5669
5845
  });
5670
- await this.markFailed(conv.id, message.id, null, errorMessage).catch((markErr) => {
5846
+ await this.markFailed(conv.id, message.id, null, errorMessage2).catch((markErr) => {
5671
5847
  this.log({
5672
5848
  level: "warn",
5673
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)}`,
@@ -6001,6 +6177,9 @@ var ChannelDriver = class _ChannelDriver {
6001
6177
  });
6002
6178
  await this.markFailed(conv.id, message.id, sessionId, error2, usage, failure);
6003
6179
  }
6180
+ if (ocId !== null) {
6181
+ await this.reportSubagentAuthFailures(conv.id, ocId, message.id, messages);
6182
+ }
6004
6183
  } catch (err) {
6005
6184
  if (err instanceof ChannelAuthError) throw err;
6006
6185
  this.log({
@@ -6965,6 +7144,12 @@ var ChannelDriver = class _ChannelDriver {
6965
7144
  return;
6966
7145
  }
6967
7146
  inFlight.done = true;
7147
+ await this.reportSubagentAuthFailures(
7148
+ watcher.conv.id,
7149
+ inFlight.opencodeMessageId,
7150
+ inFlight.evidentMessageId,
7151
+ messages
7152
+ );
6968
7153
  }
6969
7154
  this.removeInFlight(watcher, inFlight.evidentMessageId);
6970
7155
  return;
@@ -7207,6 +7392,12 @@ var ChannelDriver = class _ChannelDriver {
7207
7392
  return;
7208
7393
  }
7209
7394
  inFlight.done = true;
7395
+ await this.reportSubagentAuthFailures(
7396
+ watcher.conv.id,
7397
+ inFlight.opencodeMessageId,
7398
+ inFlight.evidentMessageId,
7399
+ messages
7400
+ );
7210
7401
  }
7211
7402
  this.removeInFlight(watcher, inFlight.evidentMessageId);
7212
7403
  }
@@ -7377,6 +7568,7 @@ var ChannelDriver = class _ChannelDriver {
7377
7568
  });
7378
7569
  return;
7379
7570
  }
7571
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId ?? "", row.id, messages);
7380
7572
  this.dontRedispatch.delete(row.id);
7381
7573
  void this.postSignal(row.conversation_id, row.id, "readopt_failed");
7382
7574
  return;
@@ -7538,6 +7730,9 @@ var ChannelDriver = class _ChannelDriver {
7538
7730
  });
7539
7731
  return;
7540
7732
  }
7733
+ if (ocId !== null) {
7734
+ await this.reportSubagentAuthFailures(row.conversation_id, ocId, row.id, messages);
7735
+ }
7541
7736
  this.dontRedispatch.delete(row.id);
7542
7737
  void this.postSignal(row.conversation_id, row.id, "readopt_done");
7543
7738
  }
@@ -7631,14 +7826,14 @@ var ChannelDriver = class _ChannelDriver {
7631
7826
  this.unconfirmedDispatchFailures.delete(row.id);
7632
7827
  this.sessions.delete(readoptConv.id);
7633
7828
  this.supersede(readoptConv.id, sessionId);
7634
- 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.`;
7635
7830
  this.log({
7636
7831
  level: "error",
7637
- message: errorMessage,
7832
+ message: errorMessage2,
7638
7833
  conversation_id: row.conversation_id,
7639
7834
  message_id: row.id
7640
7835
  });
7641
- 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) => {
7642
7837
  this.log({
7643
7838
  level: "warn",
7644
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)}`,
@@ -8253,6 +8448,7 @@ var ChannelDriver = class _ChannelDriver {
8253
8448
  throw new Error(`Failed to get pending conversations: HTTP ${res.status}`);
8254
8449
  }
8255
8450
  const data = await res.json();
8451
+ this.recycleRequestedFlag = data.recycle_requested === true;
8256
8452
  let conversations = data.conversations;
8257
8453
  if (this.conversationFilter) {
8258
8454
  conversations = conversations.filter((c) => c.id === this.conversationFilter);
@@ -8488,6 +8684,111 @@ var ChannelDriver = class _ChannelDriver {
8488
8684
  reply?.info?.modelID ?? null
8489
8685
  );
8490
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
+ }
8491
8792
  /**
8492
8793
  * Best-effort, SINGLE-ATTEMPT runner→server telemetry ping
8493
8794
  * (queued-followup-redrive). `POST .../messages/:id/signal {signal, ...extra}`
@@ -9079,6 +9380,241 @@ function applyRunnerOpenCodeConfig({
9079
9380
  log3(`Applied runner OpenCode config ${source} to ${join8(cwd, target)}`);
9080
9381
  }
9081
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
+ };
9616
+ }
9617
+
9082
9618
  // src/commands/run.ts
9083
9619
  var MAX_ACTIVITY_LOG_ENTRIES = 10;
9084
9620
  var CHANNEL_POLL_INTERVAL_MS = Number(process.env.EVIDENT_CHANNEL_POLL_INTERVAL_MS) || 2e3;
@@ -9376,6 +9912,10 @@ async function driveChannels(state, driver) {
9376
9912
  consecutiveDrainFailures = 0;
9377
9913
  unreachableMs = 0;
9378
9914
  state.messageCount += processed;
9915
+ if (driver.recycleRequested) {
9916
+ await beginGracefulShutdown(state, "recycle");
9917
+ return;
9918
+ }
9379
9919
  const proxiedActivity = state.lastProxiedActivityAt !== lastSeenProxiedActivityAt;
9380
9920
  lastSeenProxiedActivityAt = state.lastProxiedActivityAt;
9381
9921
  const fileActivitySnapshot = driver.fileSyncActivity();
@@ -9418,8 +9958,8 @@ async function driveChannels(state, driver) {
9418
9958
  state.running = false;
9419
9959
  break;
9420
9960
  }
9421
- const errorMessage = error2 instanceof Error ? error2.message : String(error2);
9422
- 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}` });
9423
9963
  if (state.interactive) displayStatus(state);
9424
9964
  if (driver.hasInFlightWatchers()) {
9425
9965
  consecutiveDrainFailures = 0;
@@ -9779,7 +10319,8 @@ function scheduleResourceUsageReporting(state, options) {
9779
10319
  });
9780
10320
  return;
9781
10321
  }
9782
- const collect = createResourceUsageCollector(homedir5());
10322
+ const { collect, stop } = createResourceUsageCollector(homedir5());
10323
+ state.stopResourceUsageSampling = stop;
9783
10324
  let consecutiveFailures = 0;
9784
10325
  const tick = async () => {
9785
10326
  try {
@@ -9889,21 +10430,41 @@ async function cleanup(state, opts = {}) {
9889
10430
  clearTimeout(state.resourceUsageTimer);
9890
10431
  state.resourceUsageTimer = null;
9891
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;
9892
10448
  if (opts.graceful && state.channelDriver) {
9893
10449
  state.channelDriver.stop();
10450
+ }
10451
+ if (flushCredentials) {
10452
+ await flushCredentials("credential_flush", !(opts.graceful && state.channelDriver));
10453
+ }
10454
+ if (opts.graceful && state.channelDriver) {
9894
10455
  log2(state, "Draining in-flight channel work before shutdown...");
9895
10456
  if (state.interactive) {
9896
10457
  logActivity(state, { type: "info", message: "Draining in-flight work before shutdown..." });
9897
10458
  displayStatus(state);
9898
10459
  }
9899
10460
  const driver = state.channelDriver;
9900
- const settled = await timeShutdownPhase(
10461
+ drainSettled = await timeShutdownPhase(
9901
10462
  state,
9902
10463
  durations,
9903
10464
  "drain",
9904
10465
  () => driver.waitForInFlight(SHUTDOWN_DRAIN_TIMEOUT_MS)
9905
10466
  );
9906
- if (!settled) {
10467
+ if (!drainSettled) {
9907
10468
  logActivity(state, {
9908
10469
  type: "info",
9909
10470
  message: "Shutdown drain timed out with work still in flight \u2014 leaving it for restart recovery"
@@ -9911,6 +10472,9 @@ async function cleanup(state, opts = {}) {
9911
10472
  if (state.interactive) displayStatus(state);
9912
10473
  }
9913
10474
  }
10475
+ if (opts.graceful && state.channelDriver && flushCredentials && drainSettled) {
10476
+ await flushCredentials("credential_flush_final", true);
10477
+ }
9914
10478
  await timeShutdownPhase(state, durations, "offline_notify", () => notifyOffline(state));
9915
10479
  if (state.connection) {
9916
10480
  const connection = state.connection;
@@ -9946,6 +10510,44 @@ async function cleanup(state, opts = {}) {
9946
10510
  }
9947
10511
  return durations;
9948
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
+ }
9949
10551
  async function run(options) {
9950
10552
  const interactive = isInteractive(options.json);
9951
10553
  let logLevel;
@@ -9997,9 +10599,24 @@ async function run(options) {
9997
10599
  openaiUsageTimer: null,
9998
10600
  openaiUsageRearm: null,
9999
10601
  resourceUsageTimer: null,
10602
+ stopResourceUsageSampling: null,
10603
+ credentialSync: null,
10000
10604
  authHeader: ""
10001
10605
  };
10002
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
+ }
10003
10620
  if (fileSyncDirectories.length > 0) {
10004
10621
  log2(state, `File sync enabled for: ${fileSyncDirectories.join(", ")}`);
10005
10622
  } else {
@@ -10025,43 +10642,7 @@ async function run(options) {
10025
10642
  "warn"
10026
10643
  );
10027
10644
  }
10028
- const handleSignal = async () => {
10029
- if (state.shuttingDown) return;
10030
- state.shuttingDown = true;
10031
- const shutdownStartedAt = Date.now();
10032
- if (state.interactive) {
10033
- logActivity(state, { type: "info", message: "Shutting down..." });
10034
- displayStatus(state);
10035
- } else {
10036
- log2(state, "Shutting down...");
10037
- }
10038
- const durations = await cleanup(state, { graceful: true });
10039
- const telemetryBudgetMs = Number(process.env.EVIDENT_TELEMETRY_SHUTDOWN_MS) || TELEMETRY_SHUTDOWN_TIMEOUT_MS;
10040
- await timeShutdownPhase(state, durations, "telemetry_flush", async () => {
10041
- let timer;
10042
- const flushed = shutdownTelemetry().then(
10043
- () => true,
10044
- (error2) => {
10045
- log2(
10046
- state,
10047
- `Telemetry flush failed during shutdown: ${error2 instanceof Error ? error2.message : String(error2)}`,
10048
- "warn"
10049
- );
10050
- return true;
10051
- }
10052
- );
10053
- const timedOut = new Promise((resolve4) => {
10054
- timer = setTimeout(() => resolve4(false), telemetryBudgetMs);
10055
- });
10056
- if (!await Promise.race([flushed, timedOut])) {
10057
- log2(state, `Telemetry flush exceeded ${telemetryBudgetMs}ms \u2014 exiting anyway`, "warn");
10058
- }
10059
- clearTimeout(timer);
10060
- });
10061
- const breakdown = Object.entries(durations).map(([phase, ms]) => `${phase}=${ms}ms`).join(" ");
10062
- log2(state, `Shutdown complete in ${Date.now() - shutdownStartedAt}ms (${breakdown})`);
10063
- process.exit(0);
10064
- };
10645
+ const handleSignal = () => beginGracefulShutdown(state, "signal");
10065
10646
  process.on("SIGINT", handleSignal);
10066
10647
  process.on("SIGTERM", handleSignal);
10067
10648
  try {
@@ -10207,6 +10788,7 @@ async function run(options) {
10207
10788
  await restoreCredentialStores(credentialContext);
10208
10789
  if (githubTokenPopulated) await configureGitHubAccess(credentialContext);
10209
10790
  }
10791
+ state.credentialSync?.arm();
10210
10792
  let sessionDbVerifyFatal = false;
10211
10793
  if (!options.restoreSessionDb) {
10212
10794
  log2(state, "Skipping session-DB restore: --restore-session-db was not passed", "debug");
@@ -10276,7 +10858,7 @@ async function run(options) {
10276
10858
  state.opencodeVersion = oc.version;
10277
10859
  if (options.opencodePidFile && oc.process?.pid !== void 0) {
10278
10860
  try {
10279
- writeFileSync5(options.opencodePidFile, `${oc.process.pid}
10861
+ writeFileSync6(options.opencodePidFile, `${oc.process.pid}
10280
10862
  `, { mode: 384 });
10281
10863
  chmodSync3(options.opencodePidFile, 384);
10282
10864
  } catch (error2) {
@@ -10392,7 +10974,7 @@ async function run(options) {
10392
10974
  });
10393
10975
  try {
10394
10976
  if (litestreamProcess.pid !== void 0) {
10395
- writeFileSync5(options.litestreamPidFile, `${litestreamProcess.pid}
10977
+ writeFileSync6(options.litestreamPidFile, `${litestreamProcess.pid}
10396
10978
  `, {
10397
10979
  mode: 384
10398
10980
  });
@@ -10578,6 +11160,18 @@ async function run(options) {
10578
11160
  if (state.interactive) displayStatus(state);
10579
11161
  });
10580
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
+ },
10581
11175
  onInfo: (message) => logActivity(state, { type: "info", message })
10582
11176
  }
10583
11177
  });
@@ -10598,7 +11192,17 @@ async function run(options) {
10598
11192
  setTimer: (timer) => {
10599
11193
  state.openaiUsageTimer = timer;
10600
11194
  },
10601
- 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
+ },
10602
11206
  report: (usage) => reportOpenAiUsage(state.agentId, state.authHeader, usage),
10603
11207
  isLocalCredentialProblem: isLocalCredentialProblem2,
10604
11208
  forcedOnHint: "connect a ChatGPT account to this runner, or run `opencode auth login`",
@@ -10722,6 +11326,9 @@ program.command("run").description("Connect to Evident and process messages").op
10722
11326
  ).option(
10723
11327
  "--opencode-config-overlay <path>",
10724
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."
10725
11332
  ).action(
10726
11333
  (options) => {
10727
11334
  run({
@@ -10760,7 +11367,8 @@ program.command("run").description("Connect to Evident and process messages").op
10760
11367
  sessionDbNoReplicateMarker: options.sessionDbNoReplicateMarker,
10761
11368
  restoreSessionDb: options.restoreSessionDb,
10762
11369
  restoreRunnerCredentials: options.restoreRunnerCredentials,
10763
- opencodeConfigOverlay: options.opencodeConfigOverlay
11370
+ opencodeConfigOverlay: options.opencodeConfigOverlay,
11371
+ credentialSyncMarker: options.credentialSyncMarker
10764
11372
  });
10765
11373
  }
10766
11374
  );