@bitkyc08/opencodex 2.98.0-preview.20261107 → 2.99.0-preview.20261108

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.
Files changed (109) hide show
  1. package/bin/ocx.mjs +6 -19
  2. package/gui/dist/assets/{App-6TFtZsUF.css → App-C1hGwaN1.css} +1 -1
  3. package/gui/dist/assets/App-CWz0areF.js +59 -0
  4. package/gui/dist/assets/Tray-BosDoskm.js +1 -0
  5. package/gui/dist/assets/{brand-mark-VPhzz-WE.js → brand-mark-DpiBmFGI.js} +1 -1
  6. package/gui/dist/assets/{index-BWKBeK77.js → index-CAxjGWkm.js} +13 -13
  7. package/gui/dist/assets/index-bnUWypyz.css +2 -0
  8. package/gui/dist/assets/{segment-thumb-features-BQYzrze9.js → segment-thumb-features-JcszQ18T.js} +1 -1
  9. package/gui/dist/classic/assets/App-BOEsFmiN.js +58 -0
  10. package/gui/dist/classic/assets/Tray-5v86r9Az.js +1 -0
  11. package/gui/dist/classic/assets/{index-Dp2j8bSc.js → index-ClZ_zZ77.js} +13 -13
  12. package/gui/dist/classic/assets/{quota-summary-CvamtHjB.js → quota-summary-xwvDtvu7.js} +1 -1
  13. package/gui/dist/classic/index.html +1 -1
  14. package/gui/dist/index.html +2 -2
  15. package/package.json +1 -1
  16. package/src/adapters/coding-agent/protocol.ts +8 -3
  17. package/src/adapters/coding-agent/turn.ts +6 -2
  18. package/src/adapters/command-code.ts +8 -5
  19. package/src/adapters/devin.ts +11 -1
  20. package/src/adapters/kiro/stream.ts +5 -4
  21. package/src/adapters/openai-chat/destination-policy.ts +25 -0
  22. package/src/adapters/openai-chat/native-content.ts +32 -0
  23. package/src/adapters/openai-chat/passthrough.ts +14 -1
  24. package/src/adapters/openai-chat/reasoning-wire.ts +42 -1
  25. package/src/adapters/openai-chat.ts +2 -2
  26. package/src/adapters/qoder/adapter.ts +9 -0
  27. package/src/bridge/response-json.ts +13 -1
  28. package/src/bridge/sse.ts +112 -22
  29. package/src/cli/account-affinity-reset.ts +42 -0
  30. package/src/cli/account.ts +5 -0
  31. package/src/cli/capabilities-accounts.ts +8 -0
  32. package/src/cli/capabilities-agents-routing.ts +5 -4
  33. package/src/cli/message-command.ts +1 -1
  34. package/src/cli/message-mcp-command.ts +8 -5
  35. package/src/client/orphan-token-recovery.ts +1 -1
  36. package/src/codex/autostart-health.ts +4 -2
  37. package/src/generated/compatibility-version.json +128 -76
  38. package/src/lib/desktop-cli-handoff.mjs +5 -8
  39. package/src/lib/failure-detail.ts +5 -2
  40. package/src/lib/launcher-signal-forwarding.d.mts +8 -0
  41. package/src/lib/launcher-signal-forwarding.mjs +36 -0
  42. package/src/lib/privacy.ts +6 -0
  43. package/src/lib/request-execution-budget.ts +188 -33
  44. package/src/lib/upstream-validation-detail.ts +47 -0
  45. package/src/lib/workflow-budget.ts +35 -16
  46. package/src/messaging/mcp-queue-rpc.ts +7 -3
  47. package/src/messaging/mcp-queue-server.ts +6 -0
  48. package/src/messaging/mcp-reply-cursor.ts +34 -0
  49. package/src/messaging/mcp-reply-tools.ts +93 -0
  50. package/src/oauth/anthropic-routing.ts +46 -3
  51. package/src/oauth/anthropic-session-affinity-store.ts +23 -10
  52. package/src/protocols/codecs/ollama.ts +22 -12
  53. package/src/providers/registry/entries-extended.ts +14 -0
  54. package/src/remote/hub-usage.ts +7 -0
  55. package/src/responses/parser.ts +27 -0
  56. package/src/responses/reasoning-envelope.ts +4 -0
  57. package/src/responses/schema.ts +1 -0
  58. package/src/server/chat-completions.ts +3 -0
  59. package/src/server/chat-native-sse.ts +14 -10
  60. package/src/server/chat-native.ts +24 -11
  61. package/src/server/index/websocket-handler.ts +12 -1
  62. package/src/server/management/oauth-account-routes.ts +65 -13
  63. package/src/server/management/route-registry.ts +1 -0
  64. package/src/server/messages-native.ts +21 -5
  65. package/src/server/request-log-attempt-recovery.ts +17 -0
  66. package/src/server/request-log-failure-detail.ts +18 -1
  67. package/src/server/request-log.ts +5 -18
  68. package/src/server/responses/account-auto-pause-recovery.ts +29 -1
  69. package/src/server/responses/adapter-continuation.ts +12 -5
  70. package/src/server/responses/adapter-dispatch.ts +50 -51
  71. package/src/server/responses/antigravity-model-denial-recovery.ts +4 -2
  72. package/src/server/responses/codex-ws-exchange.ts +13 -4
  73. package/src/server/responses/combo-required-tool-contract.ts +22 -3
  74. package/src/server/responses/core-codex-account.ts +92 -86
  75. package/src/server/responses/core-combo-failure.ts +7 -0
  76. package/src/server/responses/core-combo.ts +11 -4
  77. package/src/server/responses/core-lifetime.ts +3 -0
  78. package/src/server/responses/deferred-send-reporter.ts +18 -1
  79. package/src/server/responses/fetch-helpers.ts +9 -3
  80. package/src/server/responses/native-continuation-refusal.ts +8 -0
  81. package/src/server/responses/native-continuation-send.ts +34 -0
  82. package/src/server/responses/passthrough-delivery.ts +6 -2
  83. package/src/server/responses/passthrough-dispatch.ts +48 -33
  84. package/src/server/responses/passthrough-execution.ts +1 -0
  85. package/src/server/responses/policy-fallback.ts +32 -5
  86. package/src/server/responses/policy-request-scope.ts +26 -0
  87. package/src/server/responses/request-prepare.ts +11 -2
  88. package/src/server/responses/request-send-budget.ts +96 -14
  89. package/src/server/responses/request-spend-target.ts +19 -0
  90. package/src/server/responses/request-spend.ts +80 -28
  91. package/src/server/responses/request-transport.ts +106 -53
  92. package/src/server/responses/run-turn-execution.ts +3 -0
  93. package/src/server/responses/sidecar-execution.ts +2 -1
  94. package/src/server/responses/ws-upstream.ts +2 -1
  95. package/src/server/workflow-refusal.ts +38 -4
  96. package/src/server/ws-bridge.ts +10 -3
  97. package/src/service/desktop-startup.ts +76 -13
  98. package/src/types/request.ts +2 -1
  99. package/src/usage/app-breakdown.ts +23 -0
  100. package/src/usage/summary.ts +34 -1
  101. package/src/web-search/anthropic-executor.ts +25 -9
  102. package/src/web-search/loop.ts +3 -1
  103. package/src/web-search/passthrough-bridge.ts +3 -0
  104. package/src/web-search/run-turn-loop.ts +3 -1
  105. package/gui/dist/assets/App-B8_sOxoz.js +0 -59
  106. package/gui/dist/assets/Tray-DpWEp6ee.js +0 -1
  107. package/gui/dist/assets/index-5REODpG3.css +0 -2
  108. package/gui/dist/classic/assets/App-Bz65x1J6.js +0 -58
  109. package/gui/dist/classic/assets/Tray-kk7mdO6I.js +0 -1
@@ -63,6 +63,7 @@ const systemMessageItemSchema = z.object({
63
63
  });
64
64
  const assistantMessageItemSchema = z.object({
65
65
  type: z.literal("message").optional(),
66
+ id: z.string().optional(),
66
67
  role: z.literal("assistant"),
67
68
  content: z.union([z.string(), z.array(outputContentBlockSchema)]).optional(),
68
69
  phase: z.enum(["commentary", "final_answer"]).optional(),
@@ -1,4 +1,5 @@
1
1
  import { projectLocalModelRefusalLog } from "./request-log-failure-detail";
2
+ import { carryWorkflowRefusal } from "./workflow-refusal";
2
3
  import { clientFacingModelLabel, clientFacingModelText, type ClientFacingModelLabel } from "../lib/error-destination";
3
4
  import { wasAccountAutoPaused } from "../lib/account-auto-pause-response";
4
5
  import { ownedChatErrorPayload, isProxyError, LOCAL_CHAT_ERROR_MESSAGES } from "../lib/client-error-body";
@@ -587,6 +588,7 @@ async function handleChatCompletionsWithBudget(
587
588
  ...(envelope && nativeChatCombos ? {
588
589
  protocolSource: createNativeChatComboSource({
589
590
  req, config, envelope, requestedModel, requestedStream: stream, translatorBudget,
591
+ workflowRefusalContext: logCtx,
590
592
  // Children run without logIds; the request's admission rides the source so their
591
593
  // replay scope resolves the same caller principal as every other surface.
592
594
  ...(admission ? { admission } : {}),
@@ -736,6 +738,7 @@ async function handleChatCompletionsWithBudget(
736
738
  },
737
739
  });
738
740
  if (replayRefusal) retainReplayRefusal(rewritten);
741
+ carryWorkflowRefusal(upstream, rewritten);
739
742
  return logIds
740
743
  // Deferred logging re-wraps this response and carries the verdict with it.
741
744
  ? responseWithDeferredRequestLog(rewritten, logIds.requestId, logIds.start, logCtx, undefined, legacyEnvelope)
@@ -1,3 +1,5 @@
1
+ import { normalizeNativeChatContent } from "../adapters/openai-chat/native-content";
2
+ import type { OcxProviderConfig } from "../types";
1
3
  import { ownedChatErrorPayload, canonicalSseErrorBlock } from "../lib/client-error-body";
2
4
  import { legacyFailureVerdict, type FailureVerdictSink } from "../chat/failure-verdict";
3
5
  import { chatCompletionsErrorBody, type ChatCompletionsCollectorSource } from "../chat/outbound";
@@ -177,6 +179,7 @@ export function jsonCompletionSse(value: Rec, requestedModel: string, budget?: T
177
179
  }
178
180
 
179
181
  interface NativeChatSseOptions {
182
+ provider?: Pick<OcxProviderConfig, "adapter" | "authMode" | "baseUrl">;
180
183
  requestedModel: string;
181
184
  failureVerdictSink?: FailureVerdictSink;
182
185
  collectorSource?: ChatCompletionsCollectorSource;
@@ -370,7 +373,10 @@ export function nativeChatSse(
370
373
  fail(controller, "upstream returned an invalid SSE event", "upstream_sse_invalid");
371
374
  return;
372
375
  }
373
- const error = structuredError(parsed);
376
+ const usage = usageFromChat(parsed.usage);
377
+ if (usage) options.onUsage(usage);
378
+ const chunk = normalizeNativeChatContent(parsed, options.provider);
379
+ const error = structuredError(chunk);
374
380
  if (error) {
375
381
  const status = error.status ?? 502;
376
382
  const classified = classifyError(status, error.type ?? "upstream_error", error.message);
@@ -390,14 +396,12 @@ export function nativeChatSse(
390
396
  controller.close();
391
397
  return;
392
398
  }
393
- const usage = usageFromChat(parsed.usage);
394
- options.onChunk?.(parsed);
395
- if (usage) options.onUsage(usage);
396
- if (options.clientDeclaredEmptyToolCatalog && !inspectToolCalls(parsed)) {
399
+ options.onChunk?.(chunk);
400
+ if (options.clientDeclaredEmptyToolCatalog && !inspectToolCalls(chunk)) {
397
401
  fail(controller, UNDECLARED_CHAT_TOOL_MESSAGE, UNDECLARED_TOOL_CALL_ERROR_CODE);
398
402
  return;
399
403
  }
400
- const choices = Array.isArray(parsed.choices) ? parsed.choices : [];
404
+ const choices = Array.isArray(chunk.choices) ? chunk.choices : [];
401
405
  const finished = choices.some(choice => isRec(choice) && typeof choice.finish_reason === "string" && choice.finish_reason.length > 0);
402
406
  if (hasPendingToolName() && choices.some((choice, position) => isRec(choice)
403
407
  && typeof choice.finish_reason === "string" && choice.finish_reason.length > 0
@@ -436,16 +440,16 @@ export function nativeChatSse(
436
440
  firstOutput = true;
437
441
  options.onFirstOutput?.();
438
442
  }
439
- if (eventName === "error" || parsed.type === "error") {
443
+ if (eventName === "error" || chunk.type === "error") {
440
444
  // These frames keep the ordinary relay's control flow; only client bytes change.
441
- const flat = structuredError({ error: parsed })!;
445
+ const flat = structuredError({ error: chunk })!;
442
446
  const status = flat.status ?? 502;
443
447
  const payload = ownedChatErrorPayload({ status, type: flat.type, code: flat.code,
444
448
  evidence: { ...flat, status, code: flat.code ?? undefined } }).error;
445
- options.collectorSource?.retain(JSON.stringify(payload), parsed);
449
+ options.collectorSource?.retain(JSON.stringify(payload), chunk);
446
450
  relay(controller, (eventName === "error" ? "event: error\n" : "") + canonicalSseErrorBlock(payload));
447
451
  } else {
448
- const normalized = normalizedChunk(parsed, options.requestedModel);
452
+ const normalized = normalizedChunk(chunk, options.requestedModel);
449
453
  const choiceError = choices.some(choice => isRec(choice) && choice.error !== undefined && choice.error !== null);
450
454
  relay(controller, choiceError ? canonicalSseErrorBlock(normalizeChatChoiceErrors(normalized))
451
455
  : replaceSseDataPayload(block, JSON.stringify(normalized)) + delimiter);
@@ -1,3 +1,5 @@
1
+ import { recordUpstreamValidationDetail } from "./request-log-failure-detail";
2
+ import { normalizeNativeChatContent } from "../adapters/openai-chat/native-content";
1
3
  import { attachComboFailureEvidence, ownedChatErrorPayload, type ClientErrorContract } from "../lib/client-error-body";
2
4
  import type { FailureVerdictSink } from "../chat/failure-verdict";
3
5
  import { normalizeChatChoiceErrors } from "./chat-native-sse";
@@ -87,7 +89,7 @@ import { beginInferenceAttempt, type InferenceAttempt } from "./inference/attemp
87
89
  import { createFinalRequestLog } from "./inference/final-log";
88
90
  import { registerTurn, unregisterTurn } from "./lifecycle";
89
91
  import { attachRequestSpendTracker } from "./responses/request-spend";
90
- import { workflowRefusalResponse } from "./workflow-refusal";
92
+ import { workflowRefusalResponse, workflowSendCeilingRefusalResponse } from "./workflow-refusal";
91
93
  import type { ComboProtocolSource } from "./responses/core-combo-native";
92
94
  import type { ProtocolEnvelope } from "../protocols/envelope";
93
95
  import { protocolTraceSuppressed } from "../protocols/trace";
@@ -212,6 +214,8 @@ export interface NativeChatExecution extends HandleNativeChatOptions {
212
214
  */
213
215
  sendBudget?: RequestExecutionBudget;
214
216
  comboDispatchPermit?: SingleUseDispatchPermit;
217
+ /** The shared observer records a Combo child's refusal on its parent's context. */
218
+ workflowRefusalContext?: Pick<RequestLogContext, "localTerminalReason">;
215
219
  /** Replaces the request-relative first-output mark; a combo child records its own. */
216
220
  onFirstOutput?: () => void;
217
221
  /** The lease a streamed body holds; defaults to `logIds.turnAdmissionLease`. */
@@ -259,6 +263,7 @@ export function createNativeChatComboSource(input: {
259
263
  requestedModel: string;
260
264
  requestedStream: boolean;
261
265
  translatorBudget: TranslatorBudget;
266
+ workflowRefusalContext?: Pick<RequestLogContext, "localTerminalReason">;
262
267
  /** The request's data-plane admission; native children inherit it for replay scope. */
263
268
  admission?: DataPlaneAdmission;
264
269
  }): ComboProtocolSource {
@@ -277,6 +282,7 @@ export function createNativeChatComboSource(input: {
277
282
  finishLog: child.finishLog,
278
283
  sendBudget: child.sendBudget,
279
284
  comboDispatchPermit: child.comboDispatchPermit,
285
+ workflowRefusalContext: input.workflowRefusalContext,
280
286
  onFirstOutput: child.onFirstOutput,
281
287
  ...(child.turnAdmissionLease ? { turnAdmissionLease: child.turnAdmissionLease } : {}),
282
288
  ...(input.admission ? { admission: input.admission } : {}),
@@ -471,9 +477,9 @@ export async function runNativeChatAttempt(
471
477
  throw new SendBudgetExhaustedError(errorDestinationLabel(url));
472
478
  }
473
479
  physicalSends += 1;
474
- reportDispatchSends(sendBudget, 1, execution.comboDispatchPermit);
480
+ reportDispatchSends(sendBudget, 1, execution.comboDispatchPermit, true);
475
481
  initialSendStarted = true;
476
- } else if (!spendTracker?.charge()) throw new NativeChatSpendRefusal();
482
+ } else if (!spendTracker?.charge({ workflowAlreadyCounted: true })) throw new NativeChatSpendRefusal();
477
483
  };
478
484
 
479
485
  let responseOwnsDistributedKey = true;
@@ -700,11 +706,15 @@ export async function runNativeChatAttempt(
700
706
  return refusal;
701
707
  }
702
708
  if (sendError instanceof SendBudgetExhaustedError) {
709
+ const refusal = workflowSendCeilingRefusalResponse(logCtx, req.headers.get("x-codex-parent-thread-id")?.trim() || undefined,
710
+ execution.workflowRefusalContext);
711
+ if (refusal) { finishLog(429); return refusal; }
703
712
  // A decision this process made, answered as the Responses path answers it: 429, not 502.
704
713
  return failLocal(429, sendError.message, SEND_BUDGET_EXHAUSTED_CODE, SEND_BUDGET_EXHAUSTED_CODE);
705
714
  }
706
715
  if (sendError instanceof NativeChatSpendRefusal) {
707
- const refusal = workflowRefusalResponse("workflow-spend-exhausted", logCtx);
716
+ const refusal = workflowRefusalResponse(logCtx.errorCode === "workflow_sends_exhausted"
717
+ ? "workflow-sends-exhausted" : "workflow-spend-exhausted", logCtx);
708
718
  finishLog(429);
709
719
  return refusal;
710
720
  }
@@ -735,6 +745,7 @@ export async function runNativeChatAttempt(
735
745
  let upstreamMessage: string | undefined;
736
746
  try {
737
747
  const parsedError = JSON.parse(bodyText) as Rec;
748
+ recordUpstreamValidationDetail(logCtx, response.status, parsedError);
738
749
  const nested = isRec(parsedError.error) ? parsedError.error : undefined;
739
750
  const details = nested ?? parsedError;
740
751
  if (typeof details.type === "string") upstreamType = details.type;
@@ -838,6 +849,7 @@ export async function runNativeChatAttempt(
838
849
  const failureVerdictSink: FailureVerdictSink = {};
839
850
  const collectorSource = requestedStream ? undefined : new ChatCompletionsCollectorSource(translatorBudget);
840
851
  const stream = nativeChatSse(resilientBody, {
852
+ provider: activeProvider,
841
853
  failureVerdictSink,
842
854
  collectorSource,
843
855
  requestedModel,
@@ -940,19 +952,20 @@ export async function runNativeChatAttempt(
940
952
  } catch {
941
953
  return failUpstream(502, "upstream returned malformed Chat Completions JSON", "upstream_error");
942
954
  }
943
- const error = structuredError(parsedJson);
944
- if (error) return failUpstream(error.status ?? 502, error.message, error.type, error.code);
945
- const originalCompletion = chatCompletionJson(parsedJson);
946
- const completion = originalCompletion ? normalizeChatChoiceErrors(originalCompletion) : null;
947
- if (!completion) return failUpstream(502, "upstream response contained no choices", "upstream_error");
948
- reasoningCapture.json(completion);
949
- const usage = usageFromChat(completion.usage);
955
+ const usage = isRec(parsedJson) ? usageFromChat(parsedJson.usage) : undefined;
950
956
  if (usage) {
951
957
  if (!recordKeyWireAttemptUsage(logCtx, usage)) {
952
958
  logCtx.usage = usage;
953
959
  attempt.usage = usage;
954
960
  }
955
961
  }
962
+ if (isRec(parsedJson)) parsedJson = normalizeNativeChatContent(parsedJson, activeProvider);
963
+ const error = structuredError(parsedJson);
964
+ if (error) return failUpstream(error.status ?? 502, error.message, error.type, error.code);
965
+ const originalCompletion = chatCompletionJson(parsedJson);
966
+ const completion = originalCompletion ? normalizeChatChoiceErrors(originalCompletion) : null;
967
+ if (!completion) return failUpstream(502, "upstream response contained no choices", "upstream_error");
968
+ reasoningCapture.json(completion);
956
969
  if (clientDeclaredEmptyToolCatalog && chatResponseHasUndeclaredToolCalls(completion, declaredLegacyFunctionNames)) {
957
970
  return failUpstream(502, UNDECLARED_CHAT_TOOL_MESSAGE, "upstream_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
958
971
  }
@@ -94,6 +94,7 @@ import type { RequestMetricsRecorder } from "../request-metrics";
94
94
  import { resolveInboundBodyLimitBytes } from "../request-decompress";
95
95
  import { responseToolOwnership } from "../responses/response-tool-ownership";
96
96
  import { runTurnStateCallerScope } from "../turn-state-handles";
97
+ import { admitHttpWorkflowTurn, workflowDecisionRefusalResponse } from "../workflow-refusal";
97
98
 
98
99
  /**
99
100
  * The WebSocket half of the Bun.serve options, split out of serve-options.ts to keep that file
@@ -307,7 +308,6 @@ export function createWebsocketHandler(
307
308
  // Only a genuinely admitted turn may receive steering or continuations.
308
309
  const advisorAdmissionPolicy = captureAdmissionPolicy(config, ws.data.admission);
309
310
  const finishActivity = ws.data.responsesActivity?.beginTurn(frame.input);
310
- ws.data.nativeControl = nativeControl;
311
311
  const payload: Record<string, unknown> = { ...frame };
312
312
  delete payload.type;
313
313
  turnAdmissionLease.bindAbortController(turnAbort);
@@ -346,6 +346,17 @@ export function createWebsocketHandler(
346
346
  body: JSON.stringify({ ...payload, stream: true }),
347
347
  });
348
348
  try {
349
+ // A socket can be the first ingress for a root. Admit every inference frame,
350
+ // then keep its workflow lease with the turn until terminal output or cancellation.
351
+ const workflow = admitHttpWorkflowTurn(req.headers);
352
+ if (workflow && !workflow.admitted) {
353
+ const refusal = workflowDecisionRefusalResponse(workflow, logCtx);
354
+ await sendResponseToWebSocket(ws, refusal, isCurrent);
355
+ finalizeLog(refusal.status);
356
+ return;
357
+ }
358
+ if (workflow?.admitted) turnAdmissionLease.attach(workflow.lease);
359
+ ws.data.nativeControl = nativeControl;
349
360
  let terminalRecorder: ((status: ResponsesTerminalStatus, httpStatusOverride?: number) => void) | undefined;
350
361
  const response = await handleResponses(req, config, logCtx, {
351
362
  advisorAdmissionPolicy,
@@ -40,7 +40,7 @@ import { OAuthMutationBusyError, removeCredential } from "../../oauth/store";
40
40
  import { cancelKiroDeviceLogin, kiroDeviceConfigBaseline, selectKiroDeviceProfile, startKiroDeviceLogin, statusKiroDeviceLogin, type KiroDeviceMethod } from "../../oauth/kiro-device-login";
41
41
  import { parseKiroIamOptions } from "../../oauth/kiro-iam-identity-center";
42
42
  import { providerDestinationResolvedError } from "../../lib/destination-policy";
43
- import { emailMaskingEnabled } from "../../lib/privacy";
43
+ import { emailMaskingEnabled, maskedAccountLabel } from "../../lib/privacy";
44
44
  import { reconcileLiveStateStores } from "../../lib/state-store-registrations";
45
45
  import { enrichProviderFromCatalog, listKeyLoginProviders } from "../../oauth/key-providers";
46
46
  import { deriveProviderPresets } from "../../providers/derive";
@@ -527,7 +527,8 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
527
527
  needsReauth: summary.needsReauth === true,
528
528
  reauthReason: summary.needsReauth === true ? "refresh_failed" : undefined,
529
529
  });
530
- return { ...summary, ...oauthAccountHealthFields(provider, summary.id, health), quotaMode,
530
+ const maskedLabel = maskedAccountLabel(full?.credential.email);
531
+ return { ...summary, ...(maskedLabel ? { maskedLabel } : {}), ...oauthAccountHealthFields(provider, summary.id, health), quotaMode,
531
532
  ...(supportsPause ? { paused: full?.paused === true, ...(full?.pauseDetails ? { pauseDetails: full.pauseDetails } : {}) } : {}),
532
533
  ...(isAnthropicOAuthInstance(provider) && supportsPause ? { autoSwitchThresholdOverride: full?.autoSwitchThresholdOverride ?? null,
533
534
  effectiveAutoSwitchThreshold: effectiveAnthropicAccountThresholdForInstance(provider as AnthropicInstanceId, config, full),
@@ -572,6 +573,19 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
572
573
  }),
573
574
  });
574
575
  }
576
+ if (url.pathname === "/api/oauth/accounts/affinity-reset" && req.method === "PUT") {
577
+ const body = await readManagementJsonBodyOr(req, {});
578
+ if (!isPlainRecord(body)) return jsonResponse({ error: "body must be an object" }, 400);
579
+ const provider = typeof body.provider === "string" ? body.provider.trim().toLowerCase() : "";
580
+ if (!isPublicOAuthProvider(provider) || !isAnthropicOAuthInstance(provider)) {
581
+ return jsonResponse({ error: "session-affinity reset is only supported for Anthropic OAuth instances" }, 400);
582
+ }
583
+ const { anthropicRoutingFor } = await import("../../oauth/anthropic-routing");
584
+ if (!await anthropicRoutingFor(provider).resetAnthropicSessionAffinityConfirmed()) {
585
+ return jsonResponse({ code: "affinity_reset_not_persisted", provider, retryable: true }, 503);
586
+ }
587
+ return jsonResponse({ ok: true, provider, affinityResetConfirmed: true });
588
+ }
575
589
  if (url.pathname === "/api/oauth/accounts/active" && req.method === "PUT") {
576
590
  const body = await readManagementJsonBodyOr(req, {}) as { provider?: string; accountId?: string };
577
591
  const provider = (body.provider ?? "").trim().toLowerCase();
@@ -595,15 +609,20 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
595
609
  // side carries resetCodexRoutingForManualSelection for.
596
610
  const { genericPoolKey, seedPoolRotationAccount } = await import("../../oauth/pool-kernel");
597
611
  seedPoolRotationAccount(genericPoolKey(provider), body.accountId);
598
- if (isAnthropicOAuthInstance(provider)) {
599
- const { anthropicRoutingFor } = await import("../../oauth/anthropic-routing");
600
- anthropicRoutingFor(provider).resetAnthropicRoutingForManualSelection(body.accountId);
601
- }
612
+ const anthropicRouting = isAnthropicOAuthInstance(provider)
613
+ ? (await import("../../oauth/anthropic-routing")).anthropicRoutingFor(provider) : undefined;
614
+ anthropicRouting?.resetAnthropicRoutingForManualSelection(body.accountId);
615
+ const affinityReset = anthropicRouting?.flushAnthropicManualSelectionReset();
602
616
  const { clearModelCache } = await import("../../codex/model-cache");
603
617
  const { clearGatherRoutedModelsInflight } = await import("../../codex/catalog");
604
618
  clearModelCache(provider);
605
619
  clearGatherRoutedModelsInflight();
606
620
  clearQuotaCachesAfterAccountMutation(provider, false);
621
+ if (affinityReset && !await affinityReset) {
622
+ return jsonResponse({ error: "Account selection was saved, but its session-affinity reset was not persisted. Retry the selection.",
623
+ code: "affinity_reset_not_persisted", provider, activeAccountId: body.accountId,
624
+ selectionCommitted: true, retryable: true }, 503);
625
+ }
607
626
  return jsonResponse({ ok: true, provider, activeAccountId: body.accountId });
608
627
  }
609
628
 
@@ -617,6 +636,9 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
617
636
  return jsonResponse({ error: "missing accountId" }, 400);
618
637
  }
619
638
  if (typeof body.paused !== "boolean") return jsonResponse({ error: "paused must be a boolean" }, 400);
639
+ if (body.retryAffinityReset !== undefined && typeof body.retryAffinityReset !== "boolean") {
640
+ return jsonResponse({ error: "retryAffinityReset must be a boolean" }, 400);
641
+ }
620
642
 
621
643
  const { isGenericFailoverProvider } = await import("../../oauth/generic-account-failover");
622
644
  const effectiveProvider = genericOAuthProviderConfig(provider, config);
@@ -629,11 +651,18 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
629
651
  const result = await setAccountPaused(provider, body.accountId, body.paused);
630
652
  if (result.status === "not-found") return jsonResponse({ error: "account not found" }, 404);
631
653
 
654
+ const anthropicRouting = isAnthropicOAuthInstance(provider)
655
+ ? (await import("../../oauth/anthropic-routing")).anthropicRoutingFor(provider) : undefined;
656
+ let affinityReset: Promise<boolean> | undefined;
657
+ // Explicit retries rebuild reset intent after a server restart lost the in-memory marker.
658
+ if (anthropicRouting && result.activeAccountChanged) {
659
+ anthropicRouting.resetAnthropicRoutingForManualSelection(result.activeAccountId);
660
+ affinityReset = anthropicRouting.flushAnthropicManualSelectionReset();
661
+ } else if (anthropicRouting && body.retryAffinityReset === true) {
662
+ affinityReset = anthropicRouting.resetAnthropicSessionAffinityConfirmed();
663
+ }
632
664
  if (result.activeAccountChanged) {
633
- if (isAnthropicOAuthInstance(provider)) {
634
- const { anthropicRoutingFor } = await import("../../oauth/anthropic-routing");
635
- anthropicRoutingFor(provider).resetAnthropicRoutingForManualSelection(result.activeAccountId);
636
- } else {
665
+ if (!anthropicRouting) {
637
666
  const { genericPoolKey, seedPoolRotationAccount } = await import("../../oauth/pool-kernel");
638
667
  seedPoolRotationAccount(genericPoolKey(provider), result.activeAccountId);
639
668
  }
@@ -644,6 +673,14 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
644
673
  clearQuotaCachesAfterAccountMutation(provider, false);
645
674
  }
646
675
 
676
+ // Inactive pauses also publish invalidation before success; an unchanged retry drains it.
677
+ affinityReset ??= anthropicRouting?.flushAnthropicAccountPause();
678
+ if (affinityReset && !await affinityReset) {
679
+ return jsonResponse({ error: "Account pause was saved, but its session-affinity reset was not persisted. Retry with the same pause state and retryAffinityReset: true.",
680
+ code: "affinity_reset_not_persisted", provider, accountId: body.accountId, paused: body.paused,
681
+ activeAccountId: result.activeAccountId, activeAccountChanged: result.activeAccountChanged,
682
+ pauseCommitted: true, retryable: true }, 503);
683
+ }
647
684
  return jsonResponse({
648
685
  ok: true,
649
686
  provider,
@@ -1034,13 +1071,24 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
1034
1071
  const id = url.searchParams.get("id") ?? "";
1035
1072
  if (!isPublicOAuthProvider(provider)) return jsonResponse({ error: "unknown oauth provider" }, 400);
1036
1073
  if (!id) return jsonResponse({ error: "missing id" }, 400);
1074
+ const retryReset = url.searchParams.get("retryAffinityReset");
1075
+ if (retryReset !== null && retryReset !== "true" && retryReset !== "false") {
1076
+ return jsonResponse({ error: "retryAffinityReset must be true or false" }, 400);
1077
+ }
1078
+ if (retryReset === "true" && !isAnthropicOAuthInstance(provider)) {
1079
+ return jsonResponse({ error: "session-affinity reset is only supported for Anthropic OAuth instances" }, 400);
1080
+ }
1037
1081
  const { removeAccount, getAccountSet } = await import("../../oauth/store");
1038
1082
  if (!(await removeAccount(provider, id))) return jsonResponse({ error: "account not found" }, 404);
1039
1083
  reconcileLiveStateStores();
1084
+ let affinityReset: Promise<boolean> | undefined;
1040
1085
  if (isAnthropicOAuthInstance(provider)) {
1041
1086
  const { anthropicRoutingFor } = await import("../../oauth/anthropic-routing");
1042
- anthropicRoutingFor(provider).clearAnthropicAccountCooldown(id);
1043
- anthropicRoutingFor(provider).clearAnthropicSessionAffinityForAccount(id);
1087
+ const routing = anthropicRoutingFor(provider);
1088
+ routing.clearAnthropicAccountCooldown(id);
1089
+ routing.clearAnthropicSessionAffinityForAccount(id);
1090
+ affinityReset = retryReset === "true" ? routing.resetAnthropicSessionAffinityConfirmed()
1091
+ : routing.flushAnthropicManualSelectionReset();
1044
1092
  }
1045
1093
  if (!getAccountSet(provider)) clearLoginState(provider);
1046
1094
  const { clearModelCache } = await import("../../codex/model-cache");
@@ -1052,7 +1100,11 @@ export async function handleOauthAccountRoutes(ctx: ManagementContext): Promise<
1052
1100
  // leave the JWT and catalog in memory, because only the logout route cleared
1053
1101
  // them.
1054
1102
  if (isDevinCloudDirectProvider(provider)) await clearDevinCloudDirectCaches();
1055
- return jsonResponse({ ok: true });
1103
+ if (affinityReset && !await affinityReset) {
1104
+ return jsonResponse({ code: "affinity_reset_not_persisted", provider, accountId: id,
1105
+ deletionCommitted: true, retryable: true }, 503);
1106
+ }
1107
+ return jsonResponse({ ok: true, ...(retryReset === "true" ? { affinityResetConfirmed: true } : {}) });
1056
1108
  }
1057
1109
 
1058
1110
  // Multi-key pool for API-key providers (same GUI dropdown as OAuth multiauth): list masked
@@ -368,6 +368,7 @@ export const MANAGEMENT_ROUTES: readonly ManagementRoute[] = [
368
368
  { method: "POST", path: "/api/oauth/logout", module: "server/management/oauth-account-routes", mutates: true },
369
369
  { method: "POST", path: "/api/providers/keys", module: "server/management/oauth-account-routes", mutates: true },
370
370
  { method: "PUT", path: "/api/oauth/accounts/active", module: "server/management/oauth-account-routes", mutates: true },
371
+ { method: "PUT", path: "/api/oauth/accounts/affinity-reset", module: "server/management/oauth-account-routes", mutates: true },
371
372
  { method: "PUT", path: "/api/oauth/accounts/alias", module: "server/management/oauth-account-routes", mutates: true },
372
373
  { method: "PUT", path: "/api/oauth/accounts/pause", module: "server/management/oauth-account-routes", mutates: true },
373
374
  { method: "PUT", path: "/api/oauth/accounts/auto-switch", module: "server/management/oauth-account-routes", mutates: true },
@@ -60,6 +60,7 @@ import {
60
60
  fetchWithResetRetry,
61
61
  fetchWithTransientRetry,
62
62
  isNonReplayableResponse,
63
+ markResponseNonReplayable,
63
64
  isReplayRefusalResponse,
64
65
  isTransientUpstreamStatus,
65
66
  prepareSameTarget429Wait,
@@ -134,7 +135,7 @@ import { anthropicBillingPhysicalFetch } from "../providers/anthropic-billing-ow
134
135
  import { rotateAnthropicBillingKeyOnResponse } from "../providers/anthropic-billing-recovery";
135
136
  import { linkAbortSignal } from "./responses/core-lifetime";
136
137
  import { attachRequestSpendTracker } from "./responses/request-spend";
137
- import { workflowRefusalResponse } from "./workflow-refusal";
138
+ import { carryWorkflowRefusal, workflowRefusalResponse, workflowSendCeilingRefusalResponse } from "./workflow-refusal";
138
139
  import { sseFieldValue } from "../lib/sse-decoder";
139
140
  import { admissionModelDeniedResponse, projectAdmissionModelDeniedLog, type AdmissionModelScope } from "./admission-model-scope";
140
141
  import { nativeMessagesToolScopeDenial } from "./messages-native-scope";
@@ -153,6 +154,16 @@ const MAX_NATIVE_MESSAGES_ERROR_BYTES = 64 * 1024;
153
154
 
154
155
  class NativeMessagesSpendRefusal extends Error {}
155
156
 
157
+ /** Project only proxy-owned workflow refusals onto the native Messages error wire. */
158
+ async function nativeWorkflowRefusalResponse(refusal: Response): Promise<Response> {
159
+ const { error } = await refusal.json() as { error: { message: string; type: string; code?: string } };
160
+ const projected = new Response(JSON.stringify(anthropicErrorBody(refusal.status, error.message, error.type, error.code)), {
161
+ status: refusal.status, headers: refusal.headers,
162
+ });
163
+ if (isNonReplayableResponse(refusal)) markResponseNonReplayable(projected);
164
+ return carryWorkflowRefusal(refusal, projected);
165
+ }
166
+
156
167
  /** A rebuild for a destination that cannot carry the body's opaque state, under `reject`. */
157
168
  class NativeOpaqueStateRefusal extends Error {
158
169
  constructor() {
@@ -667,7 +678,7 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
667
678
  if (spendTracker.refusals) throw new NativeMessagesSpendRefusal();
668
679
  throw new SendBudgetExhaustedError();
669
680
  }
670
- if (!physicalBudget.spendEnforced && !spendTracker.charge()) throw new NativeMessagesSpendRefusal();
681
+ if (!physicalBudget.spendEnforced && !spendTracker.charge({ workflowAlreadyCounted: true })) throw new NativeMessagesSpendRefusal();
671
682
  noteProviderAttemptSend(logCtx, route.providerName, activeProvider, logCtx.usageLogInputTokens, transportRecovery ?? recovery);
672
683
  // Last synchronous ownership reservation before the physical fetch. Retain this
673
684
  // exact incarnation across its await; a returned response can never reserve one.
@@ -870,11 +881,16 @@ export async function handleNativeMessages(options: HandleNativeMessagesOptions)
870
881
  if (advice !== undefined) refusal.headers.set("Retry-After", advice);
871
882
  return refusal;
872
883
  }
873
- if (sendError instanceof SendBudgetExhaustedError) return fail(429, sendError.message, "send_budget_exhausted", "send_budget_exhausted");
884
+ if (sendError instanceof SendBudgetExhaustedError) {
885
+ const refusal = workflowSendCeilingRefusalResponse(logCtx, req.headers.get("x-codex-parent-thread-id")?.trim() || undefined);
886
+ if (refusal) { finishLog(429); return nativeWorkflowRefusalResponse(refusal); }
887
+ return fail(429, sendError.message, "send_budget_exhausted", "send_budget_exhausted");
888
+ }
874
889
  if (sendError instanceof NativeMessagesSpendRefusal) {
875
- const refusal = workflowRefusalResponse("workflow-spend-exhausted", logCtx, undefined, undefined, logCtx.spendRefusalDetail);
890
+ const refusal = workflowRefusalResponse(logCtx.errorCode === "workflow_sends_exhausted"
891
+ ? "workflow-sends-exhausted" : "workflow-spend-exhausted", logCtx, undefined, undefined, logCtx.spendRefusalDetail);
876
892
  finishLog(429);
877
- return refusal;
893
+ return nativeWorkflowRefusalResponse(refusal);
878
894
  }
879
895
  const localRefusal = localOAuthFailure(sendError);
880
896
  if (localRefusal) return localRefusal;
@@ -0,0 +1,17 @@
1
+ import type { AttemptRecoveryWithheld, PersistedUsageAttempt } from "../usage/log";
2
+
3
+ /**
4
+ * Record that a recovery this attempt was eligible for did not happen.
5
+ *
6
+ * Deliberately NOT `noteAttemptSend`: nothing was sent, so `sendCount` must not move. The two
7
+ * together are what make a one-send log readable — no kind and no withheld reason means nothing
8
+ * was eligible, a withheld reason means something was and the budget refused it (#5044).
9
+ */
10
+ export function noteAttemptRecoveryWithheld(
11
+ attempt: PersistedUsageAttempt | undefined,
12
+ reason: AttemptRecoveryWithheld,
13
+ ): void {
14
+ if (!attempt) return;
15
+ if (!attempt.recoveryWithheld) attempt.recoveryWithheld = [];
16
+ if (!attempt.recoveryWithheld.includes(reason)) attempt.recoveryWithheld.push(reason);
17
+ }
@@ -1,3 +1,4 @@
1
+ import { upstreamValidationIssues } from "../lib/upstream-validation-detail";
1
2
  import { clientFacingModelText, errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
2
3
  import { knownModelIdentity } from "../lib/model-identity";
3
4
  /**
@@ -9,7 +10,7 @@ import { knownModelIdentity } from "../lib/model-identity";
9
10
  * upstreamError, which internal classifiers and status heuristics read and which never reaches a row.
10
11
  * Only proxy-composed text may be passed here.
11
12
  */
12
- import type { FailureDetail, FailureDetailCode } from "../lib/failure-detail";
13
+ import { boundedFailureDetail, type FailureDetail, type FailureDetailCode } from "../lib/failure-detail";
13
14
 
14
15
  export type { FailureDetail };
15
16
 
@@ -78,3 +79,19 @@ export function projectLocalModelRefusalLog(
78
79
  || decision.candidates.some(candidate => !known(candidate.model) || !providerKnown(candidate.provider))
79
80
  || decision.selected && (!known(decision.selected.model) || !providerKnown(decision.selected.provider)))) logCtx.routeDecision = undefined;
80
81
  }
82
+
83
+ /** Capture bounded schema facts before a 4xx is replaced by the client-facing error policy. */
84
+ export function recordUpstreamValidationDetail(logCtx: { failureDetail?: FailureDetail }, status: number, payload: unknown): void {
85
+ if (status < 400 || status >= 500) return;
86
+ let parsed = payload;
87
+ if (typeof payload === "string") {
88
+ if (payload.length > 256 * 1024) return;
89
+ try { parsed = JSON.parse(payload); } catch { return; }
90
+ }
91
+ const validation = upstreamValidationIssues(parsed);
92
+ if (!validation) return;
93
+ const record = parsed as Record<string, unknown>;
94
+ const nested = record.error !== null && typeof record.error === "object" && !Array.isArray(record.error)
95
+ ? record.error as Record<string, unknown> : undefined;
96
+ logCtx.failureDetail = boundedFailureDetail({ code: "provider_error", type: nested?.type ?? record.type, validation });
97
+ }
@@ -5,6 +5,7 @@ import { stampApiKeyAccountLabel, usesApiKeyAccount } from "../providers/label";
5
5
  import { KEY_ACCOUNT_LOG_LABEL_RE } from "../codex/account-label";
6
6
  import { attemptAccountChanged, sealRequestAttemptIdentity } from "./request-log-account-rotation";
7
7
  export { sealRequestAttemptIdentity };
8
+ export { noteAttemptRecoveryWithheld } from "./request-log-attempt-recovery";
8
9
  import { captureUpstreamTerminalDiagnostics, upstreamDiagnosticLogFields, type UpstreamErrorDiagnostics, type UpstreamErrorPayload } from "./request-log-terminal-status";
9
10
  export { noteUpstreamRequestId } from "./request-log-terminal-status";
10
11
  import { readBoundedResponseBody } from "../lib/bounded-body";
@@ -58,7 +59,6 @@ import {
58
59
  usageStatusForFinalLog,
59
60
  usageTotalTokens,
60
61
  type AttemptRecoveryKind,
61
- type AttemptRecoveryWithheld,
62
62
  type CacheTelemetryProvenance,
63
63
  type PersistedRequestSpend,
64
64
  type PersistedUsageAttempt,
@@ -1789,13 +1789,15 @@ export function noteProviderAttemptSend(
1789
1789
  provider: OcxProviderConfig,
1790
1790
  inputTokenEstimate: number | undefined,
1791
1791
  recovery?: AttemptRecoveryKind,
1792
+ resolved?: { model: string; adapter: string },
1792
1793
  ): void {
1793
1794
  const attempt = logCtx.activeAttempt;
1794
1795
  const previous = attempt?.accountLogLabel;
1795
1796
  stampApiKeyAccountLabel(logCtx, providerName, provider);
1796
1797
  const next = logCtx.accountLogLabel;
1797
1798
  if (attempt && usesApiKeyAccount(provider)) keyUsageOwners.add(attempt);
1798
- if (attempt && attempt.sendCount > 0 && attemptAccountChanged(previous, next, attempt.provider, logCtx.provider)) {
1799
+ if (attempt && attempt.sendCount > 0 && (attemptAccountChanged(previous, next, attempt.provider, logCtx.provider)
1800
+ || resolved && (attempt.model !== resolved.model || attempt.adapter !== resolved.adapter))) {
1799
1801
  // An input estimate is not evidence that a failed send used that many tokens.
1800
1802
  delete attempt.inputTokenEstimate;
1801
1803
  // An account-scoped recovery kind names the status the sealed account answered.
@@ -1827,6 +1829,7 @@ export function noteProviderAttemptSend(
1827
1829
  logCtx.activeAttemptStartedAt = Date.now();
1828
1830
  }
1829
1831
  if (attempt) {
1832
+ if (resolved) { attempt.model = resolved.model; attempt.adapter = resolved.adapter; }
1830
1833
  sealRequestAttemptIdentity(attempt, logCtx.provider, attempt.adapter, next);
1831
1834
  recordAttemptCredentialSource(attempt, providerName, provider, attempt.adapter);
1832
1835
  }
@@ -1885,22 +1888,6 @@ export function noteAttemptSend(
1885
1888
  }
1886
1889
  }
1887
1890
 
1888
- /**
1889
- * Record that a recovery this attempt was eligible for did not happen.
1890
- *
1891
- * Deliberately NOT `noteAttemptSend`: nothing was sent, so `sendCount` must not move. The two
1892
- * together are what make a one-send log readable — no kind and no withheld reason means nothing
1893
- * was eligible, a withheld reason means something was and the budget refused it (#5044).
1894
- */
1895
- export function noteAttemptRecoveryWithheld(
1896
- attempt: PersistedUsageAttempt | undefined,
1897
- reason: AttemptRecoveryWithheld,
1898
- ): void {
1899
- if (!attempt) return;
1900
- if (!attempt.recoveryWithheld) attempt.recoveryWithheld = [];
1901
- if (!attempt.recoveryWithheld.includes(reason)) attempt.recoveryWithheld.push(reason);
1902
- }
1903
-
1904
1891
  export function finishRequestAttempt(
1905
1892
  attempt: PersistedUsageAttempt,
1906
1893
  status: number,
@@ -1,5 +1,24 @@
1
1
  import type { ResponsesSendBudget } from "./request-send-budget";
2
2
 
3
+ /** Dispose the failed exchange before projecting a refresh refusal or rethrowing its original error. */
4
+ export function createAutoPauseRefreshErrorMapper(
5
+ response: Response,
6
+ upstream: { abort(): void },
7
+ cleanup: () => void,
8
+ refusal: (error: unknown) => Response | undefined,
9
+ cancelled: () => boolean,
10
+ cancelledResponse: () => Response,
11
+ ): (error: unknown) => { failed: Response } | undefined {
12
+ return error => {
13
+ const clientCancelled = cancelled();
14
+ try { void response.body?.cancel().catch(() => {}); } catch { /* closed */ }
15
+ cleanup();
16
+ upstream.abort();
17
+ const failed = clientCancelled ? cancelledResponse() : refusal(error);
18
+ return failed ? { failed } : undefined;
19
+ };
20
+ }
21
+
3
22
  const moves = new WeakMap<object, number>();
4
23
  /** One same-provider credential move, funded by the existing recovery owner. */
5
24
  export async function recoverAutoPausedAccount<T>(options: {
@@ -7,6 +26,8 @@ export async function recoverAutoPausedAccount<T>(options: {
7
26
  remaining?: number;
8
27
  reserve: ResponsesSendBudget["reserveCredentialHop"];
9
28
  refresh: () => Promise<boolean>;
29
+ /** Map a known refresh refusal; unknown errors still propagate after unused-hop cleanup. */
30
+ mapRefreshError?: (error: unknown) => T | undefined;
10
31
  refetch: () => Promise<T>;
11
32
  setPermit: (permit: ResponsesSendBudget["pendingHopPermit"]) => void;
12
33
  }): Promise<T | undefined> {
@@ -15,7 +36,14 @@ export async function recoverAutoPausedAccount<T>(options: {
15
36
  const hop = options.reserve("auth-recovery", "account-auto-pause", true);
16
37
  if (!hop.allowed) return;
17
38
  try {
18
- if (!await options.refresh()) return;
39
+ let refreshed: boolean;
40
+ try { refreshed = await options.refresh(); }
41
+ catch (error) {
42
+ const failure = options.mapRefreshError?.(error);
43
+ if (failure !== undefined) return failure;
44
+ throw error;
45
+ }
46
+ if (!refreshed) return;
19
47
  moves.set(options.requestKey, count + 1);
20
48
  options.setPermit(hop.permit);
21
49
  return await options.refetch();