johns-harness 2026.10.21 → 2026.10.23

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 (73) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +3 -0
  3. package/dist/anthropic-auth-runtime.js +62 -0
  4. package/dist/auth-profiles-5CHn7vq1.js +9 -4
  5. package/dist/compact-B247y5Qt.js +41 -74
  6. package/dist/config-koj5M_EO.js +4 -2
  7. package/dist/daemon-cli.js +4 -2
  8. package/dist/model-selection-BGlGpPgM.js +9 -4
  9. package/dist/model-selection-BU6wl1le.js +9 -4
  10. package/dist/model-selection-L7RMwsG-.js +9 -4
  11. package/dist/pi-embedded-C6ITuRXf.js +42 -89
  12. package/dist/pi-embedded-DoQsYfIY.js +42 -89
  13. package/dist/plugin-sdk/bluebubbles.js +2 -1
  14. package/dist/plugin-sdk/config/types.models.d.ts +1 -0
  15. package/dist/plugin-sdk/config/zod-schema.core.d.ts +3 -0
  16. package/dist/plugin-sdk/config/zod-schema.d.ts +1 -0
  17. package/dist/plugin-sdk/config-4Fgm-yEH.js +9 -4
  18. package/dist/plugin-sdk/config-Di-lyCdh.js +9 -4
  19. package/dist/plugin-sdk/discord.js +4 -2
  20. package/dist/plugin-sdk/dispatch-BP0viZiL.js +42 -89
  21. package/dist/plugin-sdk/dispatch-CQsjmw7g.js +42 -89
  22. package/dist/plugin-sdk/dispatch-Cerq29sy.js +42 -89
  23. package/dist/plugin-sdk/dispatch-Cndjtt0g.js +42 -89
  24. package/dist/plugin-sdk/dispatch-UogiJYul.js +42 -89
  25. package/dist/plugin-sdk/feishu.js +2 -1
  26. package/dist/plugin-sdk/googlechat.js +2 -1
  27. package/dist/plugin-sdk/imessage.js +2 -1
  28. package/dist/plugin-sdk/model-auth-CX9cPHdC.js +9 -4
  29. package/dist/plugin-sdk/model-auth-Ciehz0x5.js +9 -4
  30. package/dist/plugin-sdk/model-auth-DVyo6JSX.js +9 -4
  31. package/dist/plugin-sdk/model-auth-kiHYHGrs.js +9 -4
  32. package/dist/plugin-sdk/model-selection-Dbj4jDYM.js +8 -3
  33. package/dist/plugin-sdk/nostr.js +2 -1
  34. package/dist/plugin-sdk/reply-DbZnH8-h.js +42 -89
  35. package/dist/plugin-sdk/signal.js +2 -1
  36. package/dist/plugin-sdk/slack.js +2 -1
  37. package/dist/plugin-sdk/telegram.js +2 -1
  38. package/dist/plugin-sdk/tlon.js +2 -1
  39. package/dist/plugin-sdk/twitch.js +2 -1
  40. package/dist/plugin-sdk/utils/usage-format.d.ts +1 -0
  41. package/dist/plugin-sdk/voice-call.js +2 -1
  42. package/dist/plugin-sdk/zalo.js +2 -1
  43. package/dist/plugin-sdk/zalouser.js +2 -1
  44. package/dist/reply-C5LKjXcC.js +41 -74
  45. package/dist/session-cost-usage-5CCZbVhO.js +4 -19
  46. package/dist/session-cost-usage-CkCo2kVx.js +4 -19
  47. package/dist/usage-billing.js +88 -0
  48. package/dist/usage-format-CNGi1eHQ.js +6 -11
  49. package/dist/usage-format-CvaU1ntA.js +6 -11
  50. package/docs/PATCHES.md +54 -1
  51. package/docs/providers/anthropic.md +23 -2
  52. package/docs/reference/token-use.md +11 -1
  53. package/models/models.json +33 -33
  54. package/node_modules/@mariozechner/pi-ai/dist/models.d.ts +1 -0
  55. package/node_modules/@mariozechner/pi-ai/dist/models.generated.js +32 -32
  56. package/node_modules/@mariozechner/pi-ai/dist/models.js +16 -5
  57. package/node_modules/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
  58. package/node_modules/@mariozechner/pi-ai/dist/providers/google-vertex.js +1 -1
  59. package/node_modules/@mariozechner/pi-ai/dist/providers/google.js +1 -1
  60. package/node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
  61. package/node_modules/@mariozechner/pi-ai/dist/types.d.ts +3 -0
  62. package/node_modules/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
  63. package/package.json +1 -1
  64. package/scripts/models-sync.mjs +1 -0
  65. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.d.ts +25 -0
  66. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.generated.js +32 -32
  67. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.js +16 -5
  68. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
  69. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google-vertex.js +371 -0
  70. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google.js +1 -1
  71. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
  72. package/vendor/patched-deps/@mariozechner/pi-ai/dist/types.d.ts +282 -0
  73. package/vendor/patched-deps/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
@@ -1,3 +1,5 @@
1
+ import { createSubscriptionAuthTracker, formatAuthProfileFallback } from "../anthropic-auth-runtime.js"; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
2
+ import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "../usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/plugin-sdk/dispatch-UogiJYul.js
1
3
  import { importOptional66 } from "../optional-module.js";
2
4
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
3
5
  import { createImageGenerateTool } from "../image-generate.js";
@@ -24597,7 +24599,7 @@ function resolveModelAuthLabel(params) {
24597
24599
  });
24598
24600
  if (profile.type === "oauth") return `oauth${label ? ` (${label})` : ""}`;
24599
24601
  if (profile.type === "token") return `token${label ? ` (${label})` : ""}`;
24600
- return `api-key${label ? ` (${label})` : ""}`;
24602
+ return `api-key${label ? ` (${label})` : ""}${formatAuthProfileFallback(params.sessionEntry, profileId)}`;
24601
24603
  }
24602
24604
  const envKey = resolveEnvApiKey(providerKey);
24603
24605
  if (envKey?.apiKey) {
@@ -34248,21 +34250,7 @@ function hasNonzeroUsage(usage) {
34248
34250
  ].some((v) => typeof v === "number" && Number.isFinite(v) && v > 0);
34249
34251
  }
34250
34252
  function normalizeUsage(raw) {
34251
- if (!raw) return;
34252
- const rawInput = asFiniteNumber(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens);
34253
- const input = rawInput !== void 0 && rawInput < 0 ? 0 : rawInput;
34254
- const output = asFiniteNumber(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens);
34255
- const cacheRead = asFiniteNumber(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens);
34256
- const cacheWrite = asFiniteNumber(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
34257
- const total = asFiniteNumber(raw.total ?? raw.totalTokens ?? raw.total_tokens);
34258
- if (input === void 0 && output === void 0 && cacheRead === void 0 && cacheWrite === void 0 && total === void 0) return;
34259
- return {
34260
- input,
34261
- output,
34262
- cacheRead,
34263
- cacheWrite,
34264
- total
34265
- };
34253
+ return normalizeBillingUsage(raw);
34266
34254
  }
34267
34255
  function derivePromptTokens(usage) {
34268
34256
  if (!usage) return;
@@ -34316,6 +34304,7 @@ async function updateSessionStoreAfterAgentRun(params) {
34316
34304
  model: modelUsed
34317
34305
  });
34318
34306
  if (result.meta.agentMeta?.authProfileId) next.authProfileServed = result.meta.agentMeta.authProfileId; // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
34307
+ next.authProfileFallback = result.meta.agentMeta?.authProfileFallback ?? null; // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
34319
34308
  if (isCliProvider(providerUsed, cfg)) {
34320
34309
  const cliSessionId = result.meta.agentMeta?.sessionId?.trim();
34321
34310
  if (cliSessionId) setCliSessionId(next, providerUsed, cliSessionId);
@@ -48811,62 +48800,19 @@ const readUsageFromSessionLog = (sessionId, sessionEntry, agentId, sessionKey, s
48811
48800
  } catch {
48812
48801
  return;
48813
48802
  }
48814
- if (!fs.existsSync(logPath)) return;
48815
- try {
48816
- const TAIL_BYTES = 8192;
48817
- const stat = fs.statSync(logPath);
48818
- const offset = Math.max(0, stat.size - TAIL_BYTES);
48819
- const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
48820
- const fd = fs.openSync(logPath, "r");
48821
- try {
48822
- fs.readSync(fd, buf, 0, buf.length, offset);
48823
- } finally {
48824
- fs.closeSync(fd);
48825
- }
48826
- const tail = buf.toString("utf-8");
48827
- const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
48828
- let input = 0;
48829
- let output = 0;
48830
- let promptTokens = 0;
48831
- let model;
48832
- let lastUsage;
48833
- for (const line of lines) {
48834
- if (!line.trim()) continue;
48835
- try {
48836
- const parsed = JSON.parse(line);
48837
- const usage = normalizeUsage(parsed.message?.usage ?? parsed.usage);
48838
- if (usage) lastUsage = usage;
48839
- model = parsed.message?.model ?? parsed.model ?? model;
48840
- } catch {}
48841
- }
48842
- if (!lastUsage) return;
48843
- input = lastUsage.input ?? 0;
48844
- output = lastUsage.output ?? 0;
48845
- promptTokens = derivePromptTokens(lastUsage) ?? lastUsage.total ?? input + output;
48846
- const total = lastUsage.total ?? promptTokens + output;
48847
- if (promptTokens === 0 && total === 0) return;
48848
- return {
48849
- input,
48850
- output,
48851
- promptTokens,
48852
- total,
48853
- model
48854
- };
48855
- } catch {
48856
- return;
48857
- }
48803
+ return readLatestBillingUsage(logPath);
48858
48804
  };
48859
- const formatUsagePair = (input, output) => {
48860
- if (input == null && output == null) return null;
48861
- return `🧮 Tokens: ${typeof input === "number" ? formatTokenCount$1(input) : "?"} in / ${typeof output === "number" ? formatTokenCount$1(output) : "?"} out`;
48805
+ const formatUsagePair = (input, output, cacheRead, cacheWrite) => {
48806
+ if (input == null && output == null && cacheRead == null && cacheWrite == null) return null;
48807
+ const count = value => typeof value === "number" ? value.toLocaleString("en-US") : "?";
48808
+ return `🧮 Tokens: ${count((input ?? 0) + (cacheWrite ?? 0))} new in / ${count(cacheRead ?? 0)} cached / ${count(output)} out (last API call)`;
48862
48809
  };
48863
- const formatCacheLine = (input, cacheRead, cacheWrite) => {
48810
+ const formatCacheLine = (input, cacheRead, cacheWrite, usage) => {
48864
48811
  if (!cacheRead && !cacheWrite) return null;
48865
- if ((typeof cacheRead !== "number" || cacheRead <= 0) && (typeof cacheWrite !== "number" || cacheWrite <= 0)) return null;
48866
- const cachedLabel = typeof cacheRead === "number" ? formatTokenCount$1(cacheRead) : "0";
48867
- const newLabel = typeof cacheWrite === "number" ? formatTokenCount$1(cacheWrite) : "0";
48868
- const totalInput = (typeof cacheRead === "number" ? cacheRead : 0) + (typeof cacheWrite === "number" ? cacheWrite : 0) + (typeof input === "number" ? input : 0);
48869
- return `🗄️ Cache: ${totalInput > 0 && typeof cacheRead === "number" ? Math.round(cacheRead / totalInput * 100) : 0}% hit · ${cachedLabel} cached, ${newLabel} new`;
48812
+ const total = (input ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
48813
+ const count = value => (value ?? 0).toLocaleString("en-US");
48814
+ const ttl = usage?.cacheWrite5m !== undefined || usage?.cacheWrite1h !== undefined ? ` (${count(usage.cacheWrite5m)} 5m / ${count(usage.cacheWrite1h)} 1h)` : "";
48815
+ return `🗄️ Cache: ${total > 0 ? Math.round((cacheRead ?? 0) / total * 100) : 0}% hit · ${count(input)} uncached + ${count(cacheWrite)} written${ttl}`;
48870
48816
  };
48871
48817
  const formatMediaUnderstandingLine = (decisions) => {
48872
48818
  if (!decisions || decisions.length === 0) return null;
@@ -48945,10 +48891,14 @@ function buildStatusMessage(args) {
48945
48891
  let outputTokens = entry?.outputTokens;
48946
48892
  let cacheRead = entry?.cacheRead;
48947
48893
  let cacheWrite = entry?.cacheWrite;
48894
+ let billingUsage;
48948
48895
  let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
48949
- if (args.includeTranscriptUsage) {
48896
+ if (entry?.sessionId) { // Always use a coherent latest API call on both status paths.
48950
48897
  const logUsage = readUsageFromSessionLog(entry?.sessionId, entry, args.agentId, args.sessionKey, args.sessionStorePath);
48951
48898
  if (logUsage) {
48899
+ billingUsage = logUsage;
48900
+ cacheRead = logUsage.cacheRead ?? 0;
48901
+ cacheWrite = logUsage.cacheWrite ?? 0;
48952
48902
  const candidate = logUsage.promptTokens || logUsage.total;
48953
48903
  if (!totalTokens || totalTokens === 0 || candidate > totalTokens) totalTokens = candidate;
48954
48904
  if (!entry?.model && logUsage.model) {
@@ -48967,8 +48917,10 @@ function buildStatusMessage(args) {
48967
48917
  model: logUsage.model,
48968
48918
  fallbackContextTokens: contextTokens ?? void 0
48969
48919
  }) ?? contextTokens;
48970
- if (!inputTokens || inputTokens === 0) inputTokens = logUsage.input;
48971
- if (!outputTokens || outputTokens === 0) outputTokens = logUsage.output;
48920
+ inputTokens = logUsage.input ?? 0;
48921
+ outputTokens = logUsage.output ?? 0;
48922
+ if (logUsage.provider) activeProvider = logUsage.provider;
48923
+ if (logUsage.model) activeModel = logUsage.model;
48972
48924
  }
48973
48925
  }
48974
48926
  const thinkLevel = args.resolvedThink ?? args.sessionEntry?.thinkingLevel ?? args.agent?.thinkingDefault ?? "off";
@@ -49004,21 +48956,14 @@ function buildStatusMessage(args) {
49004
48956
  state: entry
49005
48957
  });
49006
48958
  const effectiveCostAuthMode = fallbackState.active ? activeAuthMode : selectedAuthMode ?? activeAuthMode;
49007
- const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
48959
+ const showCost = billingUsage?.cost?.total > 0 || effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
49008
48960
  const costConfig = showCost ? resolveModelCostConfig({
49009
48961
  provider: activeProvider,
49010
48962
  model: activeModel,
49011
48963
  config: args.config
49012
48964
  }) : void 0;
49013
- const hasUsage = typeof inputTokens === "number" || typeof outputTokens === "number";
49014
- const cost = showCost && hasUsage ? estimateUsageCost({
49015
- usage: {
49016
- input: inputTokens ?? void 0,
49017
- output: outputTokens ?? void 0
49018
- },
49019
- cost: costConfig
49020
- }) : void 0;
49021
- const costLabel = showCost && hasUsage ? formatUsd(cost) : void 0;
48965
+ const hasUsage = [inputTokens, outputTokens, cacheRead, cacheWrite].some(value => typeof value === "number");
48966
+ billingUsage ??= { input: inputTokens, output: outputTokens, cacheRead, cacheWrite };
49022
48967
  const selectedAuthLabel = selectedAuthLabelValue ? ` · 🔑 ${selectedAuthLabelValue}` : "";
49023
48968
  const channelModelNote = (() => {
49024
48969
  if (!args.config || !entry) return;
@@ -49050,9 +48995,9 @@ function buildStatusMessage(args) {
49050
48995
  const fallbackLine = fallbackState.active ? `↪️ Fallback: ${activeModelLabel}${showFallbackAuth ? ` · 🔑 ${activeAuthLabelValue}` : ""} (${fallbackState.reason ?? "selected model unavailable"})` : null;
49051
48996
  const commit = resolveCommitHash();
49052
48997
  const versionLine = `🦞 Johnness ${VERSION}${commit ? ` (${commit})` : ""}`;
49053
- const usagePair = formatUsagePair(inputTokens, outputTokens);
49054
- const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite);
49055
- const costLine = costLabel ? `💵 Cost: ${costLabel}` : null;
48998
+ const usagePair = formatUsagePair(inputTokens, outputTokens, cacheRead, cacheWrite);
48999
+ const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite, billingUsage);
49000
+ const costLine = showCost && hasUsage ? formatBillingCost(billingUsage, costConfig) : null;
49056
49001
  const usageCostLine = usagePair && costLine ? `${usagePair} · ${costLine}` : usagePair ?? costLine;
49057
49002
  const mediaLine = formatMediaUnderstandingLine(args.mediaDecisions);
49058
49003
  const voiceLine = formatVoiceModeLine(args.config, args.sessionEntry);
@@ -81134,6 +81079,8 @@ function normalizeAssistantUsageSnapshot(usage) {
81134
81079
  cacheRead,
81135
81080
  cacheWrite,
81136
81081
  totalTokens,
81082
+ ...normalized.cacheWrite5m !== undefined ? { cacheWrite5m: normalized.cacheWrite5m } : {},
81083
+ ...normalized.cacheWrite1h !== undefined ? { cacheWrite1h: normalized.cacheWrite1h } : {},
81137
81084
  ...cost ? { cost } : {}
81138
81085
  };
81139
81086
  }
@@ -88321,6 +88268,7 @@ async function runEmbeddedPiAgent(params) {
88321
88268
  });
88322
88269
  if (lockedProfileId && !profileOrder.includes(lockedProfileId)) throw new Error(`Auth profile "${lockedProfileId}" is not configured for ${provider}.`);
88323
88270
  const profileCandidates = lockedProfileId ? [lockedProfileId] : profileOrder.length > 0 ? profileOrder : [void 0];
88271
+ const subscriptionAuth = createSubscriptionAuthTracker({ provider, model: modelId, store: authStore, preferredProfileId, lockedProfileId }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
88324
88272
  let profileIndex = 0;
88325
88273
  const initialThinkLevel = params.thinkLevel ?? "off";
88326
88274
  let thinkLevel = initialThinkLevel;
@@ -88452,7 +88400,8 @@ async function runEmbeddedPiAgent(params) {
88452
88400
  } else authStorage.setRuntimeApiKey(model.provider, apiKeyInfo.apiKey);
88453
88401
  lastProfileId = apiKeyInfo.profileId;
88454
88402
  };
88455
- const advanceAuthProfile = async () => {
88403
+ const advanceAuthProfile = async (reason) => {
88404
+ subscriptionAuth.failed(profileCandidates[profileIndex], reason);
88456
88405
  if (lockedProfileId) return false;
88457
88406
  let nextIndex = profileIndex + 1;
88458
88407
  while (nextIndex < profileCandidates.length) {
@@ -88502,7 +88451,7 @@ async function runEmbeddedPiAgent(params) {
88502
88451
  allInCooldown: false,
88503
88452
  error: err
88504
88453
  });
88505
- if (!await advanceAuthProfile()) throwAuthProfileFailover({
88454
+ if (!await advanceAuthProfile(/refresh/i.test(String(err)) ? "oauth_refresh_failed" : "credential_unavailable")) throwAuthProfileFailover({
88506
88455
  allInCooldown: false,
88507
88456
  error: err
88508
88457
  });
@@ -88871,7 +88820,7 @@ async function runEmbeddedPiAgent(params) {
88871
88820
  reason: promptProfileFailureReason
88872
88821
  });
88873
88822
  const promptFailoverFailure = isFailoverErrorMessage(errorText);
88874
- if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile()) {
88823
+ if (promptFailoverFailure && promptFailoverReason !== "timeout" && await advanceAuthProfile(promptFailoverReason)) {
88875
88824
  await maybeBackoffBeforeOverloadFailover(promptFailoverReason);
88876
88825
  continue;
88877
88826
  }
@@ -88934,7 +88883,7 @@ async function runEmbeddedPiAgent(params) {
88934
88883
  if (timedOut && !isProbeSession) log$7.warn(`Profile ${lastProfileId} timed out. Trying next account...`);
88935
88884
  if (cloudCodeAssistFormatError) log$7.warn(`Profile ${lastProfileId} hit Cloud Code Assist format error. Tool calls will be sanitized on retry.`);
88936
88885
  }
88937
- if (await advanceAuthProfile()) {
88886
+ if (await advanceAuthProfile(timedOut ? "timeout" : assistantFailoverReason)) {
88938
88887
  await maybeBackoffBeforeOverloadFailover(assistantFailoverReason);
88939
88888
  continue;
88940
88889
  }
@@ -88968,6 +88917,7 @@ async function runEmbeddedPiAgent(params) {
88968
88917
  lastCallUsage: lastCallUsage ?? void 0,
88969
88918
  promptTokens,
88970
88919
  compactionCount: autoCompactionCount > 0 ? autoCompactionCount : void 0,
88920
+ authProfileFallback: subscriptionAuth.result(lastProfileId), // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
88971
88921
  authProfileId: lastProfileId // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89: profile that served this turn
88972
88922
  };
88973
88923
  const payloads = buildEmbeddedRunPayloads({
@@ -100362,6 +100312,7 @@ async function persistSessionUsageUpdate(params) {
100362
100312
  modelProvider: params.providerUsed ?? entry.modelProvider,
100363
100313
  model: params.modelUsed ?? entry.model,
100364
100314
  authProfileServed: params.authProfileServed ?? entry.authProfileServed, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
100315
+ authProfileFallback: params.authProfileFallback ?? null, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
100365
100316
  contextTokens: resolvedContextTokens,
100366
100317
  systemPromptReport: params.systemPromptReport ?? entry.systemPromptReport,
100367
100318
  updatedAt: Date.now()
@@ -100642,6 +100593,7 @@ function createFollowupRunner(params) {
100642
100593
  modelUsed,
100643
100594
  providerUsed: fallbackProvider,
100644
100595
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
100596
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
100645
100597
  contextTokensUsed,
100646
100598
  systemPromptReport: runResult.meta?.systemPromptReport,
100647
100599
  logLabel: "followup"
@@ -100980,6 +100932,7 @@ async function runReplyAgent(params) {
100980
100932
  modelUsed,
100981
100933
  providerUsed,
100982
100934
  authProfileServed: runResult.meta?.agentMeta?.authProfileId, // JOHNNESS_PATCH_SUBSCRIPTION_ACTIVATE_89
100935
+ authProfileFallback: runResult.meta?.agentMeta?.authProfileFallback, // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
100983
100936
  contextTokensUsed,
100984
100937
  systemPromptReport: runResult.meta?.systemPromptReport,
100985
100938
  cliSessionId
@@ -3603,7 +3603,8 @@ const ModelDefinitionSchema = z.object({
3603
3603
  input: z.number().optional(),
3604
3604
  output: z.number().optional(),
3605
3605
  cacheRead: z.number().optional(),
3606
- cacheWrite: z.number().optional()
3606
+ cacheWrite: z.number().optional(),
3607
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
3607
3608
  }).strict().optional(),
3608
3609
  contextWindow: z.number().positive().optional(),
3609
3610
  maxTokens: z.number().positive().optional(),
@@ -3780,7 +3780,8 @@ const ModelDefinitionSchema = z.object({
3780
3780
  input: z.number().optional(),
3781
3781
  output: z.number().optional(),
3782
3782
  cacheRead: z.number().optional(),
3783
- cacheWrite: z.number().optional()
3783
+ cacheWrite: z.number().optional(),
3784
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
3784
3785
  }).strict().optional(),
3785
3786
  contextWindow: z.number().positive().optional(),
3786
3787
  maxTokens: z.number().positive().optional(),
@@ -4065,7 +4065,8 @@ const ModelDefinitionSchema = z.object({
4065
4065
  input: z.number().optional(),
4066
4066
  output: z.number().optional(),
4067
4067
  cacheRead: z.number().optional(),
4068
- cacheWrite: z.number().optional()
4068
+ cacheWrite: z.number().optional(),
4069
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
4069
4070
  }).strict().optional(),
4070
4071
  contextWindow: z.number().positive().optional(),
4071
4072
  maxTokens: z.number().positive().optional(),
@@ -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";
@@ -5202,8 +5203,10 @@ function codexOwnershipContext(agentDir) {
5202
5203
  log: (event) => log$8.info("Codex OAuth ownership", event)
5203
5204
  };
5204
5205
  }
5206
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
5205
5207
  function readCodexOwnedAuthStore(agentDir, store) {
5206
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
5208
+ const context = codexOwnershipContext(agentDir);
5209
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
5207
5210
  }
5208
5211
  function sanitizeAuthProfileCredential(credential) {
5209
5212
  const sanitized = { ...credential };
@@ -6047,7 +6050,8 @@ function resolveModelCost(raw) {
6047
6050
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
6048
6051
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
6049
6052
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
6050
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
6053
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
6054
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
6051
6055
  };
6052
6056
  }
6053
6057
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -11776,7 +11780,8 @@ const ModelDefinitionSchema = z.object({
11776
11780
  input: z.number().optional(),
11777
11781
  output: z.number().optional(),
11778
11782
  cacheRead: z.number().optional(),
11779
- cacheWrite: z.number().optional()
11783
+ cacheWrite: z.number().optional(),
11784
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
11780
11785
  }).strict().optional(),
11781
11786
  contextWindow: z.number().positive().optional(),
11782
11787
  maxTokens: z.number().positive().optional(),
@@ -17939,7 +17944,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
17939
17944
  const authPath = resolveAuthStorePath(params.agentDir);
17940
17945
  fs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
17941
17946
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
17942
- const store = ensureAuthProfileStore(params.agentDir);
17947
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
17943
17948
  const cred = store.profiles[params.profileId];
17944
17949
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
17945
17950
  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";
@@ -4171,8 +4172,10 @@ function codexOwnershipContext(agentDir) {
4171
4172
  log: (event) => log$9.info("Codex OAuth ownership", event)
4172
4173
  };
4173
4174
  }
4175
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
4174
4176
  function readCodexOwnedAuthStore(agentDir, store) {
4175
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
4177
+ const context = codexOwnershipContext(agentDir);
4178
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
4176
4179
  }
4177
4180
  function sanitizeAuthProfileCredential(credential) {
4178
4181
  const sanitized = { ...credential };
@@ -5016,7 +5019,8 @@ function resolveModelCost(raw) {
5016
5019
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
5017
5020
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
5018
5021
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
5019
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
5022
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
5023
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
5020
5024
  };
5021
5025
  }
5022
5026
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -11776,7 +11780,8 @@ const ModelDefinitionSchema = z.object({
11776
11780
  input: z.number().optional(),
11777
11781
  output: z.number().optional(),
11778
11782
  cacheRead: z.number().optional(),
11779
- cacheWrite: z.number().optional()
11783
+ cacheWrite: z.number().optional(),
11784
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
11780
11785
  }).strict().optional(),
11781
11786
  contextWindow: z.number().positive().optional(),
11782
11787
  maxTokens: z.number().positive().optional(),
@@ -17949,7 +17954,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
17949
17954
  const authPath = resolveAuthStorePath(params.agentDir);
17950
17955
  fs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
17951
17956
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
17952
- const store = ensureAuthProfileStore(params.agentDir);
17957
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
17953
17958
  const cred = store.profiles[params.profileId];
17954
17959
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
17955
17960
  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";
@@ -4175,8 +4176,10 @@ function codexOwnershipContext(agentDir) {
4175
4176
  log: (event) => log$9.info("Codex OAuth ownership", event)
4176
4177
  };
4177
4178
  }
4179
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
4178
4180
  function readCodexOwnedAuthStore(agentDir, store) {
4179
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
4181
+ const context = codexOwnershipContext(agentDir);
4182
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
4180
4183
  }
4181
4184
  function sanitizeAuthProfileCredential(credential) {
4182
4185
  const sanitized = { ...credential };
@@ -5020,7 +5023,8 @@ function resolveModelCost(raw) {
5020
5023
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
5021
5024
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
5022
5025
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
5023
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
5026
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
5027
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
5024
5028
  };
5025
5029
  }
5026
5030
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -11780,7 +11784,8 @@ const ModelDefinitionSchema = z.object({
11780
11784
  input: z.number().optional(),
11781
11785
  output: z.number().optional(),
11782
11786
  cacheRead: z.number().optional(),
11783
- cacheWrite: z.number().optional()
11787
+ cacheWrite: z.number().optional(),
11788
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
11784
11789
  }).strict().optional(),
11785
11790
  contextWindow: z.number().positive().optional(),
11786
11791
  maxTokens: z.number().positive().optional(),
@@ -17956,7 +17961,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
17956
17961
  const authPath = resolveAuthStorePath(params.agentDir);
17957
17962
  fs.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
17958
17963
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
17959
- const store = ensureAuthProfileStore(params.agentDir);
17964
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
17960
17965
  const cred = store.profiles[params.profileId];
17961
17966
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
17962
17967
  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";
@@ -4175,8 +4176,10 @@ function codexOwnershipContext(agentDir) {
4175
4176
  log: (event) => log$9.info("Codex OAuth ownership", event)
4176
4177
  };
4177
4178
  }
4179
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
4178
4180
  function readCodexOwnedAuthStore(agentDir, store) {
4179
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
4181
+ const context = codexOwnershipContext(agentDir);
4182
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
4180
4183
  }
4181
4184
  function sanitizeAuthProfileCredential(credential) {
4182
4185
  const sanitized = { ...credential };
@@ -5020,7 +5023,8 @@ function resolveModelCost(raw) {
5020
5023
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
5021
5024
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
5022
5025
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
5023
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
5026
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
5027
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
5024
5028
  };
5025
5029
  }
5026
5030
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -11780,7 +11784,8 @@ const ModelDefinitionSchema = z.object({
11780
11784
  input: z.number().optional(),
11781
11785
  output: z.number().optional(),
11782
11786
  cacheRead: z.number().optional(),
11783
- cacheWrite: z.number().optional()
11787
+ cacheWrite: z.number().optional(),
11788
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
11784
11789
  }).strict().optional(),
11785
11790
  contextWindow: z.number().positive().optional(),
11786
11791
  maxTokens: z.number().positive().optional(),
@@ -17946,7 +17951,7 @@ async function refreshUnmanagedOAuthTokenWithLock(params) {
17946
17951
  const authPath = resolveAuthStorePath(params.agentDir);
17947
17952
  fs$1.mkdirSync(path.dirname(authPath), { recursive: true, mode: 448 });
17948
17953
  return await withFileLock(authPath, AUTH_STORE_LOCK_OPTIONS, async () => {
17949
- const store = ensureAuthProfileStore(params.agentDir);
17954
+ const store = loadAuthProfileStoreForRuntime(params.agentDir, { allowKeychainPrompt: false }); // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92: reread under lock
17950
17955
  const cred = store.profiles[params.profileId];
17951
17956
  if (cred?.provider === "openai-codex") throw new Error("Auth profile changed to Codex; resolve again through its owner");
17952
17957
  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
  // JOHNNESS_PATCH_CATALOG_REFRESH_65
2
3
  import { mergeShippedCatalog65 } from "../catalog-refresh.js";
3
4
  // JOHNNESS_PATCH_NATIVE_STATE_MIGRATION_59
@@ -3957,8 +3958,10 @@ function codexOwnershipContext(agentDir) {
3957
3958
  log: (event) => log$8.info("Codex OAuth ownership", event)
3958
3959
  };
3959
3960
  }
3961
+ // JOHNNESS_PATCH_SUBSCRIPTION_RUNTIME_92
3960
3962
  function readCodexOwnedAuthStore(agentDir, store) {
3961
- return readManagedCodexStore({ ...codexOwnershipContext(agentDir), store });
3963
+ const context = codexOwnershipContext(agentDir);
3964
+ return readCurrentAnthropicAuth({ ...context, store: readManagedCodexStore({ ...context, store }) });
3962
3965
  }
3963
3966
  const runtimeAuthStoreSnapshots = /* @__PURE__ */ new Map();
3964
3967
  function resolveRuntimeStoreKey(agentDir) {
@@ -4828,7 +4831,8 @@ function resolveModelCost(raw) {
4828
4831
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
4829
4832
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
4830
4833
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
4831
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
4834
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
4835
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
4832
4836
  };
4833
4837
  }
4834
4838
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -7914,7 +7918,8 @@ const ModelDefinitionSchema = z.object({
7914
7918
  input: z.number().optional(),
7915
7919
  output: z.number().optional(),
7916
7920
  cacheRead: z.number().optional(),
7917
- cacheWrite: z.number().optional()
7921
+ cacheWrite: z.number().optional(),
7922
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
7918
7923
  }).strict().optional(),
7919
7924
  contextWindow: z.number().positive().optional(),
7920
7925
  maxTokens: z.number().positive().optional(),
@@ -295,7 +295,8 @@ const ModelDefinitionSchema = z.object({
295
295
  input: z.number().optional(),
296
296
  output: z.number().optional(),
297
297
  cacheRead: z.number().optional(),
298
- cacheWrite: z.number().optional()
298
+ cacheWrite: z.number().optional(),
299
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
299
300
  }).strict().optional(),
300
301
  contextWindow: z.number().positive().optional(),
301
302
  maxTokens: z.number().positive().optional(),