johns-harness 2026.10.22 → 2026.10.27

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 (44) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/anthropic-auth-runtime.js +62 -0
  3. package/dist/auth-profiles-5CHn7vq1.js +6 -3
  4. package/dist/channels/plugins/actions/telegram.js +1 -1
  5. package/dist/compact-B247y5Qt.js +13 -5
  6. package/dist/config-koj5M_EO.js +1 -1
  7. package/dist/daemon-cli.js +1 -1
  8. package/dist/model-selection-BGlGpPgM.js +6 -3
  9. package/dist/model-selection-BU6wl1le.js +6 -3
  10. package/dist/model-selection-L7RMwsG-.js +6 -3
  11. package/dist/pi-embedded-C6ITuRXf.js +13 -5
  12. package/dist/pi-embedded-DoQsYfIY.js +13 -5
  13. package/dist/plugin-sdk/config-4Fgm-yEH.js +6 -3
  14. package/dist/plugin-sdk/config-Di-lyCdh.js +6 -3
  15. package/dist/plugin-sdk/discord.js +1 -1
  16. package/dist/plugin-sdk/dispatch-BP0viZiL.js +13 -5
  17. package/dist/plugin-sdk/dispatch-CQsjmw7g.js +13 -5
  18. package/dist/plugin-sdk/dispatch-Cerq29sy.js +13 -5
  19. package/dist/plugin-sdk/dispatch-Cndjtt0g.js +13 -5
  20. package/dist/plugin-sdk/dispatch-UogiJYul.js +13 -5
  21. package/dist/plugin-sdk/mattermost.js +1 -1
  22. package/dist/plugin-sdk/model-auth-CX9cPHdC.js +6 -3
  23. package/dist/plugin-sdk/model-auth-Ciehz0x5.js +6 -3
  24. package/dist/plugin-sdk/model-auth-DVyo6JSX.js +6 -3
  25. package/dist/plugin-sdk/model-auth-kiHYHGrs.js +6 -3
  26. package/dist/plugin-sdk/model-selection-Dbj4jDYM.js +5 -2
  27. package/dist/plugin-sdk/reply-DbZnH8-h.js +13 -5
  28. package/dist/plugin-sdk/thinking-B-rIrY_n.js +1 -1
  29. package/dist/plugin-sdk/thinking-CopbP_Cn.js +1 -1
  30. package/dist/plugin-sdk/thinking-WQ4Zq8Nl.js +1 -1
  31. package/dist/plugin-sdk/thinking-WXqkGljb.js +1 -1
  32. package/dist/plugin-sdk/thinking-_uXqFaFO.js +1 -1
  33. package/dist/plugin-sdk/thinking-pTdxk2dr.js +1 -1
  34. package/dist/reply-C5LKjXcC.js +13 -5
  35. package/dist/thinking-B5B36ffe.js +1 -1
  36. package/dist/thinking-BYwvlJ3S.js +1 -1
  37. package/dist/thinking-CAzdgmNV.js +1 -1
  38. package/dist/thinking-DykY2Fzj.js +1 -1
  39. package/docs/PATCHES.md +46 -0
  40. package/docs/providers/anthropic.md +15 -0
  41. package/models/models.json +6 -3
  42. package/node_modules/@mariozechner/pi-ai/dist/models.generated.js +3 -1
  43. package/package.json +1 -1
  44. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.generated.js +3 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 2026.10.27
4
+
5
+ - **GPT-6.1 Terra** (`gpt-6.1-terra`) is available on native OpenAI Responses and the Codex subscription. Native: 1,050,000 context, 922,000 maximum input, 128,000 output; text/image in, text out, June 1, 2026 cutoff. Codex: 500,000 context from the exact official slug (optional max 872,000 is not the default), 128,000 output, zero subscription costs.
6
+ - **Calling rules:** none/low/medium/high/xhigh/max effort, API default medium (Codex UI default low); minimal maps to low. Temperature/top_p are kept only at explicit none and dropped at reasoning/default efforts. Tool calling uses Responses; plain-text Chat Completions remains supported by OpenAI. Existing master-driven adapters need no new patch. The official Codex UI separately lists low/medium/high/xhigh/max/ultra; ultra is not exposed as a harness effort. Only low was live-tested on the subscription; the remaining effort/sampling paths have offline adapter coverage, not a claim of hosted acceptance.
7
+ - **Standard API USD/MTok, <=272K input:** $1 input / $5 output / $0.05 cache read / $1.25 cache write (30m), $2.50 write (12h). Above 272K input, full-request rates are $2 / $7.50 / $0.10 / $2.50 (30m) / $5 (12h). Batch/Flex are 50% Standard; Fast is 2x; regional processing adds 10%. Flat catalog costs cover base Standard/30m only; other tiers and retention are documented, not represented by the flat estimator.
8
+ - **Aliases:** `terra` selects Codex GPT-6.1 Terra; `terra56` preserves GPT-5.6 Terra. All prior IDs and unrelated aliases/defaults are unchanged, including Sol/GPT and the subscription-only Sunburst -> GPT Image 2 fallback.
9
+ - **Live proof (2026-10-27):** one actual repository Codex adapter request, HTTP 200, low effort, sampling dropped, `terra_check({marker:"TERRA61_OK"})`, 71 input / 22 output tokens, $0. Paid OpenAI API inference was deliberately not tested. Credentials were read only; no auth refresh, live config, global install or gateway restart.
10
+ - **Sources checked 2026-10-27:** [model card](https://developers.openai.com/api/docs/models/gpt-6.1-terra), [pricing](https://developers.openai.com/api/docs/pricing), [parameter guide](https://developers.openai.com/api/docs/guides/latest-model), [announcement](https://openai.com/index/introducing-gpt-6-1-terra/), [exact Codex catalog](https://github.com/openai/codex/blob/b6ad8ea1fcd5562a7d3003887930be7d84824307/codex-rs/models-manager/models.json). The subscription models-list GET omits Terra, but direct exact-ID inference succeeds.
11
+ - **Rollback:** `npm i -g johns-harness@2026.10.23`.
12
+
13
+ ## 2026.10.23
14
+
15
+ ### Fixed
16
+ - Claude subscription credentials stay current after OAuth token rotation. A long-running gateway no longer retries a spent token from its startup snapshot and silently serves the request with the paid API key. Existing sessions use the current subscription credential without a reload or a new session.
17
+ - `/status` and `session_status` explain subscription-to-API fallback, including model unavailable, token refresh failure, authentication, quota and cooldown. The actual served profile stays visible, and the fallback annotation clears after subscription recovery. Explicit API-key selections remain explicit.
18
+ - Anthropic credential refresh rereads the auth store under its lock. Local-agent account precedence, resolved API SecretRefs, Codex ownership, subscription-zero accounting and the existing fallback policy are preserved.
19
+
3
20
  ## 2026.10.22
4
21
 
5
22
  ### Fixed
@@ -0,0 +1,62 @@
1
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
2
+ // OAuth generations rotate independently of SecretRef activation. Never log credential material.
3
+ import path from 'node:path';
4
+ import { readAuthStore } from './auth-store-transactions.js';
5
+
6
+ const isClaudeOAuth = c => c?.provider === 'anthropic' && c.type === 'oauth';
7
+ export function readCurrentAnthropicAuth({ store, mainAgentDir, agentDir }) {
8
+ const ids = Object.keys(store.profiles ?? {}).filter(id => isClaudeOAuth(store.profiles[id]));
9
+ if (!ids.length) return store;
10
+ const mainPath = path.join(mainAgentDir, 'auth-profiles.json');
11
+ const localPath = path.join(agentDir ?? mainAgentDir, 'auth-profiles.json');
12
+ const main = readAuthStore(mainPath);
13
+ const local = localPath === mainPath ? main : readAuthStore(localPath);
14
+ for (const id of ids) {
15
+ // Local accounts stay local. Deletion, logout or a type change must not resurrect a snapshot.
16
+ const owner = Object.hasOwn(local.profiles, id) ? local : main;
17
+ const current = owner.profiles[id];
18
+ if (isClaudeOAuth(current)) store.profiles[id] = structuredClone(current);
19
+ else delete store.profiles[id];
20
+ store.usageStats ??= {};
21
+ if (owner.usageStats?.[id]) store.usageStats[id] = structuredClone(owner.usageStats[id]);
22
+ else delete store.usageStats[id];
23
+ }
24
+ // New logins still activate through Family 89. API SecretRefs and Codex ownership are untouched.
25
+ return store;
26
+ }
27
+
28
+ const REASONS = Object.freeze({
29
+ auth: 'subscription authentication rejected',
30
+ billing: 'subscription quota or billing rejected',
31
+ rate_limit: 'subscription rate limited',
32
+ overloaded: 'subscription endpoint overloaded',
33
+ timeout: 'subscription request timed out',
34
+ model_not_found: 'model unavailable on subscription',
35
+ cooldown: 'subscription in cooldown',
36
+ credential_unavailable: 'subscription credential unavailable',
37
+ oauth_refresh_failed: 'subscription token refresh failed',
38
+ unknown: 'subscription request failed'
39
+ });
40
+ export function createSubscriptionAuthTracker({ provider, model, store, preferredProfileId, lockedProfileId }) {
41
+ const requested = isClaudeOAuth(store.profiles?.[preferredProfileId]) ? preferredProfileId : 'anthropic:subscription';
42
+ const enabled = provider === 'anthropic' && isClaudeOAuth(store.profiles?.[requested]) && (!lockedProfileId || lockedProfileId === requested);
43
+ let reason;
44
+ return {
45
+ failed(profileId, value) {
46
+ if (!enabled || profileId !== requested) return;
47
+ // Whitelisted categories only: endpoint bodies can contain secrets or untrusted text.
48
+ reason = Object.hasOwn(REASONS, value) ? value : 'unknown';
49
+ },
50
+ result(profileId) {
51
+ if (!enabled || profileId === requested || store.profiles?.[profileId]?.type !== 'api_key') return null;
52
+ const stats = store.usageStats?.[requested];
53
+ const cooled = Math.max(stats?.cooldownUntil ?? 0, stats?.disabledUntil ?? 0) > Date.now();
54
+ return { fromProfileId: requested, toProfileId: profileId, model, reason: reason ?? (cooled ? 'cooldown' : 'credential_unavailable') };
55
+ }
56
+ };
57
+ }
58
+ export function formatAuthProfileFallback(entry, profileId) {
59
+ const fallback = entry?.authProfileFallback;
60
+ if (!fallback || fallback.toProfileId !== profileId || !Object.hasOwn(REASONS, fallback.reason)) return '';
61
+ return ` [API fallback: ${REASONS[fallback.reason]}]`;
62
+ }
@@ -1,3 +1,4 @@
1
+ import { readCurrentAnthropicAuth } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { claudeSubscriptionOrder } from "./subscription-login.js"; // JOHNNESS_PATCH_SUBSCRIPTION_LOGIN_86
2
3
  // JOHNNESS_PATCH_CATALOG_REFRESH_65
3
4
  import { mergeShippedCatalog65 } from "./catalog-refresh.js";
@@ -4235,8 +4236,10 @@ function codexOwnershipContext(agentDir) {
4235
4236
  log: (event) => log$8.info("Codex OAuth ownership", event)
4236
4237
  };
4237
4238
  }
4239
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
4238
4240
  function readCodexOwnedAuthStore(agentDir, store) {
4239
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
4241
+ const context = codexOwnershipContext(agentDir);
4242
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
4240
4243
  }
4241
4244
  function sanitizeAuthProfileCredential(credential) {
4242
4245
  const sanitized = { ...credential };
@@ -5158,7 +5161,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
5158
5161
  grok46: "xai/grok-4.6"
5159
5162
  });
5160
5163
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
5161
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
5164
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
5162
5165
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
5163
5166
  /* JOHNNESS_MODELS_MASTER:END */
5164
5167
  const DEFAULT_MODEL_COST = {
@@ -16462,7 +16465,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
16462
16465
  const authPath = resolveAuthStorePath(params.agentDir);
16463
16466
  fs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
16464
16467
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
16465
- const store = ensureAuthProfileStore(params.agentDir);
16468
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
16466
16469
  const cred = store.profiles[params.profileId];
16467
16470
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
16468
16471
  if (!cred || cred.type !== "oauth") return null;
@@ -2132,7 +2132,7 @@ const XHIGH_MODEL_REFS = [
2132
2132
  "github-copilot/gpt-5.2"
2133
2133
  ];
2134
2134
  /* JOHNNESS_MODELS_MASTER:BEGIN xhigh refs (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
2135
- XHIGH_MODEL_REFS.splice(0, XHIGH_MODEL_REFS.length, ...['deepseek/deepseek-flash','deepseek/deepseek-v4-pro','meta/muse-spark-1.3','meta/muse-spark-1.2','anthropic/claude-fable-5-1','anthropic/claude-sonnet-5-5','anthropic/claude-opus-5-5','anthropic/claude-haiku-5-5','anthropic/claude-opus-5','anthropic/claude-sonnet-5','anthropic/claude-fable-5','anthropic/claude-opus-4-6','azure-openai-responses/gpt-5.2','azure-openai-responses/gpt-5.2-chat-latest','azure-openai-responses/gpt-5.2-codex','azure-openai-responses/gpt-5.2-pro','azure-openai-responses/gpt-5.3-codex','azure-openai-responses/gpt-5.3-codex-spark','github-copilot/claude-opus-4.6','github-copilot/gpt-5.2','github-copilot/gpt-5.2-codex','openai/gpt-5.2','openai/gpt-5.2-chat-latest','openai/gpt-5.2-codex','openai/gpt-5.2-pro','openai/gpt-5.3-codex','openai/gpt-5.3-codex-spark','openai/gpt-6-astra','openai/gpt-6.1-sol','openai/gpt-6-sol','openai/gpt-6-luna','openai/gpt-5.6-sol','openai/gpt-5.6-terra','openai/gpt-5.6-luna','openai-codex/gpt-5.2','openai-codex/gpt-5.2-codex','openai-codex/gpt-5.3-codex','openai-codex/gpt-5.3-codex-spark','openai-codex/gpt-6.1-sol','openai-codex/gpt-6-sol','openai-codex/gpt-6-luna','openai-codex/gpt-6-astra','openai-codex/gpt-5.6-sol','openai-codex/gpt-5.6-terra','openai-codex/gpt-5.6-luna','opencode/claude-opus-4-6','opencode/gpt-5.2','opencode/gpt-5.2-codex','opencode/gpt-5.3-codex','openrouter/openai/gpt-5.2','openrouter/openai/gpt-5.2-chat','openrouter/openai/gpt-5.2-codex','openrouter/openai/gpt-5.2-pro','openrouter/openai/gpt-5.3-codex','vercel-ai-gateway/anthropic/claude-opus-4.6','vercel-ai-gateway/openai/gpt-5.2','vercel-ai-gateway/openai/gpt-5.2-chat','vercel-ai-gateway/openai/gpt-5.2-codex','vercel-ai-gateway/openai/gpt-5.2-pro','vercel-ai-gateway/openai/gpt-5.3-codex','xai/grok-4.7','xai/grok-4.6','openai/gpt-5.4','openai/gpt-5.4-pro','openai/gpt-5.6','openai-codex/gpt-5.6','openai-codex/gpt-5.4','openai-codex/gpt-5.1-codex']);
2135
+ XHIGH_MODEL_REFS.splice(0, XHIGH_MODEL_REFS.length, ...['deepseek/deepseek-flash','deepseek/deepseek-v4-pro','meta/muse-spark-1.3','meta/muse-spark-1.2','anthropic/claude-fable-5-1','anthropic/claude-sonnet-5-5','anthropic/claude-opus-5-5','anthropic/claude-haiku-5-5','anthropic/claude-opus-5','anthropic/claude-sonnet-5','anthropic/claude-fable-5','anthropic/claude-opus-4-6','azure-openai-responses/gpt-5.2','azure-openai-responses/gpt-5.2-chat-latest','azure-openai-responses/gpt-5.2-codex','azure-openai-responses/gpt-5.2-pro','azure-openai-responses/gpt-5.3-codex','azure-openai-responses/gpt-5.3-codex-spark','github-copilot/claude-opus-4.6','github-copilot/gpt-5.2','github-copilot/gpt-5.2-codex','openai/gpt-5.2','openai/gpt-5.2-chat-latest','openai/gpt-5.2-codex','openai/gpt-5.2-pro','openai/gpt-5.3-codex','openai/gpt-5.3-codex-spark','openai/gpt-6-astra','openai/gpt-6.1-sol','openai/gpt-6-sol','openai/gpt-6-luna','openai/gpt-5.6-sol','openai/gpt-5.6-terra','openai/gpt-5.6-luna','openai/gpt-6.1-terra','openai-codex/gpt-5.2','openai-codex/gpt-5.2-codex','openai-codex/gpt-5.3-codex','openai-codex/gpt-5.3-codex-spark','openai-codex/gpt-6.1-sol','openai-codex/gpt-6-sol','openai-codex/gpt-6-luna','openai-codex/gpt-6-astra','openai-codex/gpt-5.6-sol','openai-codex/gpt-5.6-terra','openai-codex/gpt-5.6-luna','openai-codex/gpt-6.1-terra','opencode/claude-opus-4-6','opencode/gpt-5.2','opencode/gpt-5.2-codex','opencode/gpt-5.3-codex','openrouter/openai/gpt-5.2','openrouter/openai/gpt-5.2-chat','openrouter/openai/gpt-5.2-codex','openrouter/openai/gpt-5.2-pro','openrouter/openai/gpt-5.3-codex','vercel-ai-gateway/anthropic/claude-opus-4.6','vercel-ai-gateway/openai/gpt-5.2','vercel-ai-gateway/openai/gpt-5.2-chat','vercel-ai-gateway/openai/gpt-5.2-codex','vercel-ai-gateway/openai/gpt-5.2-pro','vercel-ai-gateway/openai/gpt-5.3-codex','xai/grok-4.7','xai/grok-4.6','openai/gpt-5.4','openai/gpt-5.4-pro','openai/gpt-5.6','openai-codex/gpt-5.6','openai-codex/gpt-5.4','openai-codex/gpt-5.1-codex']);
2136
2136
  /* JOHNNESS_MODELS_MASTER:END */
2137
2137
  new Set(XHIGH_MODEL_REFS.map((entry) => entry.toLowerCase()));
2138
2138
  new Set(XHIGH_MODEL_REFS.map((entry) => entry.split("/")[1]?.toLowerCase()).filter((entry) => Boolean(entry)));
@@ -1,3 +1,4 @@
1
+ import { createSubscriptionAuthTracker, formatAuthProfileFallback } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/compact-B247y5Qt.js
2
3
  import { importOptional66 } from "./optional-module.js";
3
4
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
@@ -18434,6 +18435,7 @@ async function runEmbeddedPiAgent(params) {
18434
18435
  });
18435
18436
  if (lockedProfileId && !profileOrder.includes(lockedProfileId)) throw new Error(`Auth profile "${lockedProfileId}" is not configured for ${provider}.`);
18436
18437
  const profileCandidates = lockedProfileId ? [lockedProfileId] : profileOrder.length > 0 ? profileOrder : [void 0];
18438
+ const subscriptionAuth = createSubscriptionAuthTracker({ provider, model: modelId, store: authStore, preferredProfileId, lockedProfileId }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
18437
18439
  let profileIndex = 0;
18438
18440
  const initialThinkLevel = params.thinkLevel ?? "off";
18439
18441
  let thinkLevel = initialThinkLevel;
@@ -18565,7 +18567,8 @@ async function runEmbeddedPiAgent(params) {
18565
18567
  } else authStorage.setRuntimeApiKey(model.provider, apiKeyInfo.apiKey);
18566
18568
  lastProfileId = apiKeyInfo.profileId;
18567
18569
  };
18568
- const advanceAuthProfile = async () => {
18570
+ const advanceAuthProfile = async (reason) => {
18571
+ subscriptionAuth.failed(profileCandidates[profileIndex], reason);
18569
18572
  if (lockedProfileId) return false;
18570
18573
  let nextIndex = profileIndex + 1;
18571
18574
  while (nextIndex < profileCandidates.length) {
@@ -18615,7 +18618,7 @@ async function runEmbeddedPiAgent(params) {
18615
18618
  allInCooldown: false,
18616
18619
  error: err
18617
18620
  });
18618
- if (!await advanceAuthProfile()) throwAuthProfileFailover({
18621
+ if (!await advanceAuthProfile(/refresh/i.test(String(err)) ? "oauth_refresh_failed" : "credential_unavailable")) throwAuthProfileFailover({
18619
18622
  allInCooldown: false,
18620
18623
  error: err
18621
18624
  });
@@ -18987,7 +18990,7 @@ async function runEmbeddedPiAgent(params) {
18987
18990
  reason: promptProfileFailureReason
18988
18991
  });
18989
18992
  const promptFailoverFailure = isFailoverErrorMessage(errorText);
18990
- if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile()) {
18993
+ if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile(promptFailoverReason)) {
18991
18994
  await maybeBackoffBeforeOverloadFailover(promptFailoverReason);
18992
18995
  continue;
18993
18996
  }
@@ -19050,7 +19053,7 @@ async function runEmbeddedPiAgent(params) {
19050
19053
  if (timedOut && !isProbeSession) log$16.warn(`Profile ${lastProfileId} timed out. Trying next account...`);
19051
19054
  if (cloudCodeAssistFormatError) log$16.warn(`Profile ${lastProfileId} hit Cloud Code Assist format error. Tool calls will be sanitized on retry.`);
19052
19055
  }
19053
- if (await advanceAuthProfile()) {
19056
+ if (await advanceAuthProfile(timedOut ? "timeout" : assistantFailoverReason)) {
19054
19057
  await maybeBackoffBeforeOverloadFailover(assistantFailoverReason);
19055
19058
  continue;
19056
19059
  }
@@ -19084,6 +19087,7 @@ async function runEmbeddedPiAgent(params) {
19084
19087
  lastCallUsage: lastCallUsage ?? void 0,
19085
19088
  promptTokens,
19086
19089
  compactionCount: autoCompactionCount > 0 ? autoCompactionCount : void 0,
19090
+ authProfileFallback: subscriptionAuth.result(lastProfileId), // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
19087
19091
  authProfileId: lastProfileId // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89: profile that served this turn
19088
19092
  };
19089
19093
  const payloads = buildEmbeddedRunPayloads({
@@ -35875,7 +35879,7 @@ function resolveModelAuthLabel(params) {
35875
35879
  });
35876
35880
  if (profile.type === "oauth") return `oauth${label ? ` (${label})` : ""}`;
35877
35881
  if (profile.type === "token") return `token${label ? ` (${label})` : ""}`;
35878
- return `api-key${label ? ` (${label})` : ""}`;
35882
+ return `api-key${label ? ` (${label})` : ""}${formatAuthProfileFallback(params.sessionEntry, profileId)}`;
35879
35883
  }
35880
35884
  const envKey = resolveEnvApiKey(providerKey);
35881
35885
  if (envKey?.apiKey) {
@@ -58610,6 +58614,7 @@ async function persistSessionUsageUpdate(params) {
58610
58614
  modelProvider: params.providerUsed ?? entry.modelProvider,
58611
58615
  model: params.modelUsed ?? entry.model,
58612
58616
  authProfileServed: params.authProfileServed ?? entry.authProfileServed, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
58617
+ authProfileFallback: params.authProfileFallback ?? null, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
58613
58618
  contextTokens: resolvedContextTokens,
58614
58619
  systemPromptReport: params.systemPromptReport ?? entry.systemPromptReport,
58615
58620
  updatedAt: Date.now()
@@ -58890,6 +58895,7 @@ function createFollowupRunner(params) {
58890
58895
  modelUsed,
58891
58896
  providerUsed: fallbackProvider,
58892
58897
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
58898
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
58893
58899
  contextTokensUsed,
58894
58900
  systemPromptReport: runResult.meta?.systemPromptReport,
58895
58901
  logLabel: "followup"
@@ -59228,6 +59234,7 @@ async function runReplyAgent(params) {
59228
59234
  modelUsed,
59229
59235
  providerUsed,
59230
59236
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
59237
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
59231
59238
  contextTokensUsed,
59232
59239
  systemPromptReport: runResult.meta?.systemPromptReport,
59233
59240
  cliSessionId
@@ -72222,6 +72229,7 @@ async function updateSessionStoreAfterAgentRun(params) {
72222
72229
  model: modelUsed
72223
72230
  });
72224
72231
  if (result.meta.agentMeta?.authProfileId) next.authProfileServed = result.meta.agentMeta.authProfileId; // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
72232
+ next.authProfileFallback = result.meta.agentMeta?.authProfileFallback ?? null; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
72225
72233
  if (isCliProvider(providerUsed, cfg)) {
72226
72234
  const cliSessionId = result.meta.agentMeta?.sessionId?.trim();
72227
72235
  if (cliSessionId) setCliSessionId(next, providerUsed, cliSessionId);
@@ -1852,7 +1852,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
1852
1852
  grok46: "xai/grok-4.6"
1853
1853
  });
1854
1854
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
1855
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
1855
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
1856
1856
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
1857
1857
  /* JOHNNESS_MODELS_MASTER:END */
1858
1858
  const DEFAULT_MODEL_COST = {
@@ -5004,7 +5004,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
5004
5004
  grok46: "xai/grok-4.6"
5005
5005
  });
5006
5006
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
5007
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
5007
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
5008
5008
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
5009
5009
  /* JOHNNESS_MODELS_MASTER:END */
5010
5010
  const DEFAULT_MODEL_COST = {
@@ -1,3 +1,4 @@
1
+ import { readCurrentAnthropicAuth } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { claudeSubscriptionOrder } from "./subscription-login.js"; // JOHNNESS_PATCH_SUBSCRIPTION_LOGIN_86
2
3
  // JOHNNESS_PATCH_CATALOG_REFRESH_65
3
4
  import { mergeShippedCatalog65 } from "./catalog-refresh.js";
@@ -563,8 +564,10 @@ function codexOwnershipContext(agentDir) {
563
564
  log: (event) => log$10.info("Codex OAuth ownership", event)
564
565
  };
565
566
  }
567
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
566
568
  function readCodexOwnedAuthStore(agentDir, store) {
567
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
569
+ const context = codexOwnershipContext(agentDir);
570
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
568
571
  }
569
572
  function sanitizeAuthProfileCredential(credential) {
570
573
  const sanitized = { ...credential };
@@ -1710,7 +1713,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
1710
1713
  grok46: "xai/grok-4.6"
1711
1714
  });
1712
1715
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
1713
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
1716
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
1714
1717
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
1715
1718
  /* JOHNNESS_MODELS_MASTER:END */
1716
1719
  const DEFAULT_MODEL_COST = {
@@ -14677,7 +14680,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
14677
14680
  const authPath = resolveAuthStorePath(params.agentDir);
14678
14681
  syncFs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
14679
14682
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
14680
- const store = ensureAuthProfileStore(params.agentDir);
14683
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
14681
14684
  const cred = store.profiles[params.profileId];
14682
14685
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
14683
14686
  if (!cred || cred.type !== "oauth") return null;
@@ -1,3 +1,4 @@
1
+ import { readCurrentAnthropicAuth } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { claudeSubscriptionOrder } from "./subscription-login.js"; // JOHNNESS_PATCH_SUBSCRIPTION_LOGIN_86
2
3
  // JOHNNESS_PATCH_CATALOG_REFRESH_65
3
4
  import { mergeShippedCatalog65 } from "./catalog-refresh.js";
@@ -566,8 +567,10 @@ function codexOwnershipContext(agentDir) {
566
567
  log: (event) => log$9.info("Codex OAuth ownership", event)
567
568
  };
568
569
  }
570
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
569
571
  function readCodexOwnedAuthStore(agentDir, store) {
570
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
572
+ const context = codexOwnershipContext(agentDir);
573
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
571
574
  }
572
575
  function sanitizeAuthProfileCredential(credential) {
573
576
  const sanitized = { ...credential };
@@ -1685,7 +1688,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
1685
1688
  grok46: "xai/grok-4.6"
1686
1689
  });
1687
1690
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
1688
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
1691
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
1689
1692
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
1690
1693
  /* JOHNNESS_MODELS_MASTER:END */
1691
1694
  const DEFAULT_MODEL_COST = {
@@ -14566,7 +14569,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
14566
14569
  const authPath = resolveAuthStorePath(params.agentDir);
14567
14570
  fs$1.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
14568
14571
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
14569
- const store = ensureAuthProfileStore(params.agentDir);
14572
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
14570
14573
  const cred = store.profiles[params.profileId];
14571
14574
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
14572
14575
  if (!cred || cred.type !== "oauth") return null;
@@ -1,3 +1,4 @@
1
+ import { readCurrentAnthropicAuth } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { claudeSubscriptionOrder } from "./subscription-login.js"; // JOHNNESS_PATCH_SUBSCRIPTION_LOGIN_86
2
3
  // JOHNNESS_PATCH_CATALOG_REFRESH_65
3
4
  import { mergeShippedCatalog65 } from "./catalog-refresh.js";
@@ -457,8 +458,10 @@ function codexOwnershipContext(agentDir) {
457
458
  log: (event) => log$8.info("Codex OAuth ownership", event)
458
459
  };
459
460
  }
461
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
460
462
  function readCodexOwnedAuthStore(agentDir, store) {
461
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
463
+ const context = codexOwnershipContext(agentDir);
464
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
462
465
  }
463
466
  function sanitizeAuthProfileCredential(credential) {
464
467
  const sanitized = { ...credential };
@@ -1616,7 +1619,7 @@ Object.assign(DEFAULT_MODEL_ALIASES, {
1616
1619
  grok46: "xai/grok-4.6"
1617
1620
  });
1618
1621
  /* JOHNNESS_MODELS_MASTER:BEGIN aliases (generated from models/models.json by scripts/models-sync.mjs; edit the master, not this block) */
1619
- const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-5.6-terra","grok":"xai/grok-4.7"};
1622
+ const JOHNNESS_MASTER_ALIASES = {"deepseek":"deepseek/deepseek-flash","deepseek-pro":"deepseek/deepseek-v4-pro","gemini-lite":"google/gemini-3.5-flash-lite","muse":"meta/muse-spark-1.3","muse-spark":"meta/muse-spark-1.3","opus46":"anthropic/claude-opus-4-6","sonnet46":"anthropic/claude-sonnet-4-6","gpt-mini":"openai/gpt-5-mini","gemini":"google/gemini-4-argon","gemini31":"google/gemini-3.1-pro-preview","gemini-flash":"google/gemini-3.8-flash","gemini-flash-lite":"google/gemini-2.5-flash-lite","flash":"google/gemini-3.8-flash","banana":"google/gemini-nano-banana-2.1","nano-banana":"google/gemini-nano-banana-2.1","nano-banana-pro":"google/gemini-3-pro-image","nano-banana-pro-preview":"google/gemini-3-pro-image-preview","nano-banana-lite":"google/gemini-3.1-flash-lite-image","grok-image":"xai/grok-imagine-image-2.0","image":"openai-codex/gpt-image-2.5-sunburst","image-sunburst":"openai-codex/gpt-image-2.5-sunburst","image-flare":"openai-codex/gpt-image-2.5-flare","image2":"openai-codex/gpt-image-2","sonnet5":"anthropic/claude-sonnet-5","opus5":"anthropic/claude-opus-5","haiku45":"anthropic/claude-haiku-4-5","sol6":"openai-codex/gpt-6-sol","sol56":"openai-codex/gpt-5.6-sol","luna56":"openai-codex/gpt-5.6-luna","grok46":"xai/grok-4.6","terra56":"openai-codex/gpt-5.6-terra","opus":"anthropic/claude-opus-5-5","sonnet":"anthropic/claude-sonnet-5-5","fable":"anthropic/claude-fable-5-1","haiku":"anthropic/claude-haiku-5-5","gpt":"openai-codex/gpt-6.1-sol","sol":"openai-codex/gpt-6.1-sol","luna":"openai-codex/gpt-6-luna","astra":"openai-codex/gpt-6-astra","terra":"openai-codex/gpt-6.1-terra","grok":"xai/grok-4.7"};
1620
1623
  for (const [alias, ref] of Object.entries(JOHNNESS_MASTER_ALIASES)) DEFAULT_MODEL_ALIASES[alias] = ref;
1621
1624
  /* JOHNNESS_MODELS_MASTER:END */
1622
1625
  const DEFAULT_MODEL_COST = {
@@ -12939,7 +12942,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
12939
12942
  const authPath = resolveAuthStorePath(params.agentDir);
12940
12943
  fs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
12941
12944
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
12942
- const store = ensureAuthProfileStore(params.agentDir);
12945
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
12943
12946
  const cred = store.profiles[params.profileId];
12944
12947
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
12945
12948
  if (!cred || cred.type !== "oauth") return null;
@@ -1,3 +1,4 @@
1
+ import { createSubscriptionAuthTracker, formatAuthProfileFallback } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/pi-embedded-C6ITuRXf.js
2
3
  import { importOptional66 } from "./optional-module.js";
3
4
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
@@ -27914,7 +27915,7 @@ function resolveModelAuthLabel(params) {
27914
27915
  });
27915
27916
  if (profile.type === "oauth") return `oauth${label ? ` (${label})` : ""}`;
27916
27917
  if (profile.type === "token") return `token${label ? ` (${label})` : ""}`;
27917
- return `api-key${label ? ` (${label})` : ""}`;
27918
+ return `api-key${label ? ` (${label})` : ""}${formatAuthProfileFallback(params.sessionEntry, profileId)}`;
27918
27919
  }
27919
27920
  const envKey = resolveEnvApiKey(providerKey);
27920
27921
  if (envKey?.apiKey) {
@@ -52865,6 +52866,7 @@ async function persistSessionUsageUpdate(params) {
52865
52866
  modelProvider: params.providerUsed ?? entry.modelProvider,
52866
52867
  model: params.modelUsed ?? entry.model,
52867
52868
  authProfileServed: params.authProfileServed ?? entry.authProfileServed, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
52869
+ authProfileFallback: params.authProfileFallback ?? null, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
52868
52870
  contextTokens: resolvedContextTokens,
52869
52871
  systemPromptReport: params.systemPromptReport ?? entry.systemPromptReport,
52870
52872
  updatedAt: Date.now()
@@ -53145,6 +53147,7 @@ function createFollowupRunner(params) {
53145
53147
  modelUsed,
53146
53148
  providerUsed: fallbackProvider,
53147
53149
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
53150
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
53148
53151
  contextTokensUsed,
53149
53152
  systemPromptReport: runResult.meta?.systemPromptReport,
53150
53153
  logLabel: "followup"
@@ -53610,6 +53613,7 @@ async function runReplyAgent(params) {
53610
53613
  modelUsed,
53611
53614
  providerUsed,
53612
53615
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
53616
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
53613
53617
  contextTokensUsed,
53614
53618
  systemPromptReport: runResult.meta?.systemPromptReport,
53615
53619
  cliSessionId
@@ -69781,6 +69785,7 @@ async function updateSessionStoreAfterAgentRun(params) {
69781
69785
  model: modelUsed
69782
69786
  });
69783
69787
  if (result.meta.agentMeta?.authProfileId) next.authProfileServed = result.meta.agentMeta.authProfileId; // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
69788
+ next.authProfileFallback = result.meta.agentMeta?.authProfileFallback ?? null; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
69784
69789
  if (isCliProvider(providerUsed, cfg)) {
69785
69790
  const cliSessionId = result.meta.agentMeta?.sessionId?.trim();
69786
69791
  if (cliSessionId) setCliSessionId(next, providerUsed, cliSessionId);
@@ -104032,6 +104037,7 @@ async function runEmbeddedPiAgent(params) {
104032
104037
  });
104033
104038
  if (lockedProfileId && !profileOrder.includes(lockedProfileId)) throw new Error(`Auth profile "${lockedProfileId}" is not configured for ${provider}.`);
104034
104039
  const profileCandidates = lockedProfileId ? [lockedProfileId] : profileOrder.length > 0 ? profileOrder : [void 0];
104040
+ const subscriptionAuth = createSubscriptionAuthTracker({ provider, model: modelId, store: authStore, preferredProfileId, lockedProfileId }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
104035
104041
  let profileIndex = 0;
104036
104042
  const initialThinkLevel = params.thinkLevel ?? "off";
104037
104043
  let thinkLevel = initialThinkLevel;
@@ -104163,7 +104169,8 @@ async function runEmbeddedPiAgent(params) {
104163
104169
  } else authStorage.setRuntimeApiKey(model.provider, apiKeyInfo.apiKey);
104164
104170
  lastProfileId = apiKeyInfo.profileId;
104165
104171
  };
104166
- const advanceAuthProfile = async () => {
104172
+ const advanceAuthProfile = async (reason) => {
104173
+ subscriptionAuth.failed(profileCandidates[profileIndex], reason);
104167
104174
  if (lockedProfileId) return false;
104168
104175
  let nextIndex = profileIndex + 1;
104169
104176
  while (nextIndex < profileCandidates.length) {
@@ -104213,7 +104220,7 @@ async function runEmbeddedPiAgent(params) {
104213
104220
  allInCooldown: false,
104214
104221
  error: err
104215
104222
  });
104216
- if (!await advanceAuthProfile()) throwAuthProfileFailover({
104223
+ if (!await advanceAuthProfile(/refresh/i.test(String(err)) ? "oauth_refresh_failed" : "credential_unavailable")) throwAuthProfileFailover({
104217
104224
  allInCooldown: false,
104218
104225
  error: err
104219
104226
  });
@@ -104592,7 +104599,7 @@ async function runEmbeddedPiAgent(params) {
104592
104599
  reason: promptProfileFailureReason
104593
104600
  });
104594
104601
  const promptFailoverFailure = isFailoverErrorMessage(errorText);
104595
- if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile()) {
104602
+ if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile(promptFailoverReason)) {
104596
104603
  await maybeBackoffBeforeOverloadFailover(promptFailoverReason);
104597
104604
  continue;
104598
104605
  }
@@ -104655,7 +104662,7 @@ async function runEmbeddedPiAgent(params) {
104655
104662
  if (timedOut && !isProbeSession) log$3.warn(`Profile ${lastProfileId} timed out. Trying next account...`);
104656
104663
  if (cloudCodeAssistFormatError) log$3.warn(`Profile ${lastProfileId} hit Cloud Code Assist format error. Tool calls will be sanitized on retry.`);
104657
104664
  }
104658
- if (await advanceAuthProfile()) {
104665
+ if (await advanceAuthProfile(timedOut ? "timeout" : assistantFailoverReason)) {
104659
104666
  await maybeBackoffBeforeOverloadFailover(assistantFailoverReason);
104660
104667
  continue;
104661
104668
  }
@@ -104704,6 +104711,7 @@ async function runEmbeddedPiAgent(params) {
104704
104711
  lastCallUsage: lastCallUsage ?? void 0,
104705
104712
  promptTokens,
104706
104713
  compactionCount: autoCompactionCount > 0 ? autoCompactionCount : void 0,
104714
+ authProfileFallback: subscriptionAuth.result(lastProfileId), // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
104707
104715
  authProfileId: lastProfileId // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89: profile that served this turn
104708
104716
  };
104709
104717
  const payloads = buildEmbeddedRunPayloads({
@@ -1,3 +1,4 @@
1
+ import { createSubscriptionAuthTracker, formatAuthProfileFallback } from "./anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
1
2
  import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/pi-embedded-DoQsYfIY.js
2
3
  import { importOptional66 } from "./optional-module.js";
3
4
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
@@ -27910,7 +27911,7 @@ function resolveModelAuthLabel(params) {
27910
27911
  });
27911
27912
  if (profile.type === "oauth") return `oauth${label ? ` (${label})` : ""}`;
27912
27913
  if (profile.type === "token") return `token${label ? ` (${label})` : ""}`;
27913
- return `api-key${label ? ` (${label})` : ""}`;
27914
+ return `api-key${label ? ` (${label})` : ""}${formatAuthProfileFallback(params.sessionEntry, profileId)}`;
27914
27915
  }
27915
27916
  const envKey = resolveEnvApiKey(providerKey);
27916
27917
  if (envKey?.apiKey) {
@@ -52861,6 +52862,7 @@ async function persistSessionUsageUpdate(params) {
52861
52862
  modelProvider: params.providerUsed ?? entry.modelProvider,
52862
52863
  model: params.modelUsed ?? entry.model,
52863
52864
  authProfileServed: params.authProfileServed ?? entry.authProfileServed, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
52865
+ authProfileFallback: params.authProfileFallback ?? null, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
52864
52866
  contextTokens: resolvedContextTokens,
52865
52867
  systemPromptReport: params.systemPromptReport ?? entry.systemPromptReport,
52866
52868
  updatedAt: Date.now()
@@ -53141,6 +53143,7 @@ function createFollowupRunner(params) {
53141
53143
  modelUsed,
53142
53144
  providerUsed: fallbackProvider,
53143
53145
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
53146
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
53144
53147
  contextTokensUsed,
53145
53148
  systemPromptReport: runResult.meta?.systemPromptReport,
53146
53149
  logLabel: "followup"
@@ -53606,6 +53609,7 @@ async function runReplyAgent(params) {
53606
53609
  modelUsed,
53607
53610
  providerUsed,
53608
53611
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
53612
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
53609
53613
  contextTokensUsed,
53610
53614
  systemPromptReport: runResult.meta?.systemPromptReport,
53611
53615
  cliSessionId
@@ -69777,6 +69781,7 @@ async function updateSessionStoreAfterAgentRun(params) {
69777
69781
  model: modelUsed
69778
69782
  });
69779
69783
  if (result.meta.agentMeta?.authProfileId) next.authProfileServed = result.meta.agentMeta.authProfileId; // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
69784
+ next.authProfileFallback = result.meta.agentMeta?.authProfileFallback ?? null; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
69780
69785
  if (isCliProvider(providerUsed, cfg)) {
69781
69786
  const cliSessionId = result.meta.agentMeta?.sessionId?.trim();
69782
69787
  if (cliSessionId) setCliSessionId(next, providerUsed, cliSessionId);
@@ -104022,6 +104027,7 @@ async function runEmbeddedPiAgent(params) {
104022
104027
  });
104023
104028
  if (lockedProfileId && !profileOrder.includes(lockedProfileId)) throw new Error(`Auth profile "${lockedProfileId}" is not configured for ${provider}.`);
104024
104029
  const profileCandidates = lockedProfileId ? [lockedProfileId] : profileOrder.length > 0 ? profileOrder : [void 0];
104030
+ const subscriptionAuth = createSubscriptionAuthTracker({ provider, model: modelId, store: authStore, preferredProfileId, lockedProfileId }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
104025
104031
  let profileIndex = 0;
104026
104032
  const initialThinkLevel = params.thinkLevel ?? "off";
104027
104033
  let thinkLevel = initialThinkLevel;
@@ -104153,7 +104159,8 @@ async function runEmbeddedPiAgent(params) {
104153
104159
  } else authStorage.setRuntimeApiKey(model.provider, apiKeyInfo.apiKey);
104154
104160
  lastProfileId = apiKeyInfo.profileId;
104155
104161
  };
104156
- const advanceAuthProfile = async () => {
104162
+ const advanceAuthProfile = async (reason) => {
104163
+ subscriptionAuth.failed(profileCandidates[profileIndex], reason);
104157
104164
  if (lockedProfileId) return false;
104158
104165
  let nextIndex = profileIndex + 1;
104159
104166
  while (nextIndex < profileCandidates.length) {
@@ -104203,7 +104210,7 @@ async function runEmbeddedPiAgent(params) {
104203
104210
  allInCooldown: false,
104204
104211
  error: err
104205
104212
  });
104206
- if (!await advanceAuthProfile()) throwAuthProfileFailover({
104213
+ if (!await advanceAuthProfile(/refresh/i.test(String(err)) ? "oauth_refresh_failed" : "credential_unavailable")) throwAuthProfileFailover({
104207
104214
  allInCooldown: false,
104208
104215
  error: err
104209
104216
  });
@@ -104575,7 +104582,7 @@ async function runEmbeddedPiAgent(params) {
104575
104582
  reason: promptProfileFailureReason
104576
104583
  });
104577
104584
  const promptFailoverFailure = isFailoverErrorMessage(errorText);
104578
- if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile()) {
104585
+ if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile(promptFailoverReason)) {
104579
104586
  await maybeBackoffBeforeOverloadFailover(promptFailoverReason);
104580
104587
  continue;
104581
104588
  }
@@ -104638,7 +104645,7 @@ async function runEmbeddedPiAgent(params) {
104638
104645
  if (timedOut && !isProbeSession) log$3.warn(`Profile ${lastProfileId} timed out. Trying next account...`);
104639
104646
  if (cloudCodeAssistFormatError) log$3.warn(`Profile ${lastProfileId} hit Cloud Code Assist format error. Tool calls will be sanitized on retry.`);
104640
104647
  }
104641
- if (await advanceAuthProfile()) {
104648
+ if (await advanceAuthProfile(timedOut ? "timeout" : assistantFailoverReason)) {
104642
104649
  await maybeBackoffBeforeOverloadFailover(assistantFailoverReason);
104643
104650
  continue;
104644
104651
  }
@@ -104672,6 +104679,7 @@ async function runEmbeddedPiAgent(params) {
104672
104679
  lastCallUsage: lastCallUsage ?? void 0,
104673
104680
  promptTokens,
104674
104681
  compactionCount: autoCompactionCount > 0 ? autoCompactionCount : void 0,
104682
+ authProfileFallback: subscriptionAuth.result(lastProfileId), // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
104675
104683
  authProfileId: lastProfileId // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89: profile that served this turn
104676
104684
  };
104677
104685
  const payloads = buildEmbeddedRunPayloads({