johns-harness 2026.10.21 → 2026.10.22

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 (72) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +3 -0
  3. package/dist/auth-profiles-5CHn7vq1.js +4 -2
  4. package/dist/compact-B247y5Qt.js +28 -69
  5. package/dist/config-koj5M_EO.js +4 -2
  6. package/dist/daemon-cli.js +4 -2
  7. package/dist/model-selection-BGlGpPgM.js +4 -2
  8. package/dist/model-selection-BU6wl1le.js +4 -2
  9. package/dist/model-selection-L7RMwsG-.js +4 -2
  10. package/dist/pi-embedded-C6ITuRXf.js +29 -84
  11. package/dist/pi-embedded-DoQsYfIY.js +29 -84
  12. package/dist/plugin-sdk/bluebubbles.js +2 -1
  13. package/dist/plugin-sdk/config/types.models.d.ts +1 -0
  14. package/dist/plugin-sdk/config/zod-schema.core.d.ts +3 -0
  15. package/dist/plugin-sdk/config/zod-schema.d.ts +1 -0
  16. package/dist/plugin-sdk/config-4Fgm-yEH.js +4 -2
  17. package/dist/plugin-sdk/config-Di-lyCdh.js +4 -2
  18. package/dist/plugin-sdk/discord.js +4 -2
  19. package/dist/plugin-sdk/dispatch-BP0viZiL.js +29 -84
  20. package/dist/plugin-sdk/dispatch-CQsjmw7g.js +29 -84
  21. package/dist/plugin-sdk/dispatch-Cerq29sy.js +29 -84
  22. package/dist/plugin-sdk/dispatch-Cndjtt0g.js +29 -84
  23. package/dist/plugin-sdk/dispatch-UogiJYul.js +29 -84
  24. package/dist/plugin-sdk/feishu.js +2 -1
  25. package/dist/plugin-sdk/googlechat.js +2 -1
  26. package/dist/plugin-sdk/imessage.js +2 -1
  27. package/dist/plugin-sdk/model-auth-CX9cPHdC.js +4 -2
  28. package/dist/plugin-sdk/model-auth-Ciehz0x5.js +4 -2
  29. package/dist/plugin-sdk/model-auth-DVyo6JSX.js +4 -2
  30. package/dist/plugin-sdk/model-auth-kiHYHGrs.js +4 -2
  31. package/dist/plugin-sdk/model-selection-Dbj4jDYM.js +4 -2
  32. package/dist/plugin-sdk/nostr.js +2 -1
  33. package/dist/plugin-sdk/reply-DbZnH8-h.js +29 -84
  34. package/dist/plugin-sdk/signal.js +2 -1
  35. package/dist/plugin-sdk/slack.js +2 -1
  36. package/dist/plugin-sdk/telegram.js +2 -1
  37. package/dist/plugin-sdk/tlon.js +2 -1
  38. package/dist/plugin-sdk/twitch.js +2 -1
  39. package/dist/plugin-sdk/utils/usage-format.d.ts +1 -0
  40. package/dist/plugin-sdk/voice-call.js +2 -1
  41. package/dist/plugin-sdk/zalo.js +2 -1
  42. package/dist/plugin-sdk/zalouser.js +2 -1
  43. package/dist/reply-C5LKjXcC.js +28 -69
  44. package/dist/session-cost-usage-5CCZbVhO.js +4 -19
  45. package/dist/session-cost-usage-CkCo2kVx.js +4 -19
  46. package/dist/usage-billing.js +88 -0
  47. package/dist/usage-format-CNGi1eHQ.js +6 -11
  48. package/dist/usage-format-CvaU1ntA.js +6 -11
  49. package/docs/PATCHES.md +13 -1
  50. package/docs/providers/anthropic.md +8 -2
  51. package/docs/reference/token-use.md +11 -1
  52. package/models/models.json +33 -33
  53. package/node_modules/@mariozechner/pi-ai/dist/models.d.ts +1 -0
  54. package/node_modules/@mariozechner/pi-ai/dist/models.generated.js +32 -32
  55. package/node_modules/@mariozechner/pi-ai/dist/models.js +16 -5
  56. package/node_modules/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
  57. package/node_modules/@mariozechner/pi-ai/dist/providers/google-vertex.js +1 -1
  58. package/node_modules/@mariozechner/pi-ai/dist/providers/google.js +1 -1
  59. package/node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
  60. package/node_modules/@mariozechner/pi-ai/dist/types.d.ts +3 -0
  61. package/node_modules/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
  62. package/package.json +1 -1
  63. package/scripts/models-sync.mjs +1 -0
  64. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.d.ts +25 -0
  65. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.generated.js +32 -32
  66. package/vendor/patched-deps/@mariozechner/pi-ai/dist/models.js +16 -5
  67. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/anthropic.js +15 -0
  68. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google-vertex.js +371 -0
  69. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/google.js +1 -1
  70. package/vendor/patched-deps/@mariozechner/pi-ai/dist/providers/openai-completions.js +5 -5
  71. package/vendor/patched-deps/@mariozechner/pi-ai/dist/types.d.ts +282 -0
  72. package/vendor/patched-deps/@mariozechner/pi-coding-agent/dist/core/model-registry.js +3 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 2026.10.22
4
+
5
+ ### Fixed
6
+ - `/status` and `session_status` include all billable input, cache reads, cache writes and output from one coherent latest API response. New-input counts include cache creation. Costs show every component without rounding away small charges, with a separate total for multi-call turns.
7
+ - Anthropic preserves native five-minute/one-hour cache creation buckets and uses the duration-specific price. Subscription-zero accounting remains zero; session cumulative totals sum the same corrected provider costs.
8
+ - OpenAI Chat Completions no longer counts reasoning twice. DeepSeek cache-hit fields are recognized, and Google native/Vertex subtract cached prompt tokens before pricing ordinary input. OpenAI Responses service-tier adjustments remain intact.
9
+ - Missing recorded costs are explicitly estimated using configured or shipped model prices. The backward transcript reader handles large assistant/tool records instead of silently missing usage outside an 8 KiB tail.
10
+
3
11
  ## 2026.10.21
4
12
 
5
13
  - **One-hour prompt caching is the Anthropic default**, for both API keys and Claude subscriptions, including models with no params configured. This avoids rewriting a large cached prompt after gaps longer than five minutes. Explicit `none`, `short`, `long` and legacy `cacheControlTtl` settings still win; Bedrock behavior is unchanged.
package/README.md CHANGED
@@ -34,6 +34,9 @@ The package is `johns-harness`; the command it installs is `johnness`.
34
34
 
35
35
  The wizard sets up credentials, workspace, gateway, and channels. It asks for your MemoryRouter API key and links to [signup](https://app.memoryrouter.ai/signup), free to start. If you skip memory in the wizard, the third line stores the key later.
36
36
 
37
+ ### API spend at a glance
38
+ `/status` shows the last API call's complete new-input, cached-read and output counts, with separate input/read/write/output costs. Anthropic one-hour and five-minute cache writes are priced separately. Tool-heavy turns also show a total across their API calls; `/usage cost` shows the cumulative session total. See [token use and costs](docs/reference/token-use.md).
39
+
37
40
  ### First run
38
41
 
39
42
  | Step | Command | What happens |
@@ -5181,7 +5181,8 @@ function resolveModelCost(raw) {
5181
5181
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
5182
5182
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
5183
5183
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
5184
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
5184
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
5185
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
5185
5186
  };
5186
5187
  }
5187
5188
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -10084,7 +10085,8 @@ const ModelDefinitionSchema = z.object({
10084
10085
  input: z.number().optional(),
10085
10086
  output: z.number().optional(),
10086
10087
  cacheRead: z.number().optional(),
10087
- cacheWrite: z.number().optional()
10088
+ cacheWrite: z.number().optional(),
10089
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
10088
10090
  }).strict().optional(),
10089
10091
  contextWindow: z.number().positive().optional(),
10090
10092
  maxTokens: z.number().positive().optional(),
@@ -1,3 +1,4 @@
1
+ import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/compact-B247y5Qt.js
1
2
  import { importOptional66 } from "./optional-module.js";
2
3
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
3
4
  import { createImageGenerateTool } from "./image-generate.js";
@@ -8500,6 +8501,8 @@ function normalizeAssistantUsageSnapshot(usage) {
8500
8501
  cacheRead,
8501
8502
  cacheWrite,
8502
8503
  totalTokens,
8504
+ ...normalized.cacheWrite5m !== undefined ? { cacheWrite5m: normalized.cacheWrite5m } : {},
8505
+ ...normalized.cacheWrite1h !== undefined ? { cacheWrite1h: normalized.cacheWrite1h } : {},
8503
8506
  ...cost ? { cost } : {}
8504
8507
  };
8505
8508
  }
@@ -34240,62 +34243,19 @@ const readUsageFromSessionLog = (sessionId, sessionEntry, agentId, sessionKey, s
34240
34243
  } catch {
34241
34244
  return;
34242
34245
  }
34243
- if (!fs.existsSync(logPath)) return;
34244
- try {
34245
- const TAIL_BYTES = 8192;
34246
- const stat = fs.statSync(logPath);
34247
- const offset = Math.max(0, stat.size - TAIL_BYTES);
34248
- const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
34249
- const fd = fs.openSync(logPath, "r");
34250
- try {
34251
- fs.readSync(fd, buf, 0, buf.length, offset);
34252
- } finally {
34253
- fs.closeSync(fd);
34254
- }
34255
- const tail = buf.toString("utf-8");
34256
- const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
34257
- let input = 0;
34258
- let output = 0;
34259
- let promptTokens = 0;
34260
- let model;
34261
- let lastUsage;
34262
- for (const line of lines) {
34263
- if (!line.trim()) continue;
34264
- try {
34265
- const parsed = JSON.parse(line);
34266
- const usage = normalizeUsage(parsed.message?.usage ?? parsed.usage);
34267
- if (usage) lastUsage = usage;
34268
- model = parsed.message?.model ?? parsed.model ?? model;
34269
- } catch {}
34270
- }
34271
- if (!lastUsage) return;
34272
- input = lastUsage.input ?? 0;
34273
- output = lastUsage.output ?? 0;
34274
- promptTokens = derivePromptTokens(lastUsage) ?? lastUsage.total ?? input + output;
34275
- const total = lastUsage.total ?? promptTokens + output;
34276
- if (promptTokens === 0 && total === 0) return;
34277
- return {
34278
- input,
34279
- output,
34280
- promptTokens,
34281
- total,
34282
- model
34283
- };
34284
- } catch {
34285
- return;
34286
- }
34246
+ return readLatestBillingUsage(logPath);
34287
34247
  };
34288
- const formatUsagePair = (input, output) => {
34289
- if (input == null && output == null) return null;
34290
- return `🧮 Tokens: ${typeof input === "number" ? formatTokenCount(input) : "?"} in / ${typeof output === "number" ? formatTokenCount(output) : "?"} out`;
34248
+ const formatUsagePair = (input, output, cacheRead, cacheWrite) => {
34249
+ if (input == null && output == null && cacheRead == null && cacheWrite == null) return null;
34250
+ const count = value => typeof value === "number" ? value.toLocaleString("en-US") : "?";
34251
+ return `🧮 Tokens: ${count((input ?? 0) + (cacheWrite ?? 0))} new in / ${count(cacheRead ?? 0)} cached / ${count(output)} out (last API call)`;
34291
34252
  };
34292
- const formatCacheLine = (input, cacheRead, cacheWrite) => {
34253
+ const formatCacheLine = (input, cacheRead, cacheWrite, usage) => {
34293
34254
  if (!cacheRead && !cacheWrite) return null;
34294
- if ((typeof cacheRead !== "number" || cacheRead <= 0) && (typeof cacheWrite !== "number" || cacheWrite <= 0)) return null;
34295
- const cachedLabel = typeof cacheRead === "number" ? formatTokenCount(cacheRead) : "0";
34296
- const newLabel = typeof cacheWrite === "number" ? formatTokenCount(cacheWrite) : "0";
34297
- const totalInput = (typeof cacheRead === "number" ? cacheRead : 0) + (typeof cacheWrite === "number" ? cacheWrite : 0) + (typeof input === "number" ? input : 0);
34298
- return `🗄️ Cache: ${totalInput > 0 && typeof cacheRead === "number" ? Math.round(cacheRead / totalInput * 100) : 0}% hit · ${cachedLabel} cached, ${newLabel} new`;
34255
+ const total = (input ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
34256
+ const count = value => (value ?? 0).toLocaleString("en-US");
34257
+ const ttl = usage?.cacheWrite5m !== undefined || usage?.cacheWrite1h !== undefined ? ` (${count(usage.cacheWrite5m)} 5m / ${count(usage.cacheWrite1h)} 1h)` : "";
34258
+ return `🗄️ Cache: ${total > 0 ? Math.round((cacheRead ?? 0) / total * 100) : 0}% hit · ${count(input)} uncached + ${count(cacheWrite)} written${ttl}`;
34299
34259
  };
34300
34260
  const formatMediaUnderstandingLine = (decisions) => {
34301
34261
  if (!decisions || decisions.length === 0) return null;
@@ -34374,10 +34334,14 @@ function buildStatusMessage(args) {
34374
34334
  let outputTokens = entry?.outputTokens;
34375
34335
  let cacheRead = entry?.cacheRead;
34376
34336
  let cacheWrite = entry?.cacheWrite;
34337
+ let billingUsage;
34377
34338
  let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
34378
- if (args.includeTranscriptUsage) {
34339
+ if (entry?.sessionId) { // Always use a coherent latest API call on both status paths.
34379
34340
  const logUsage = readUsageFromSessionLog(entry?.sessionId, entry, args.agentId, args.sessionKey, args.sessionStorePath);
34380
34341
  if (logUsage) {
34342
+ billingUsage = logUsage;
34343
+ cacheRead = logUsage.cacheRead ?? 0;
34344
+ cacheWrite = logUsage.cacheWrite ?? 0;
34381
34345
  const candidate = logUsage.promptTokens || logUsage.total;
34382
34346
  if (!totalTokens || totalTokens === 0 || candidate > totalTokens) totalTokens = candidate;
34383
34347
  if (!entry?.model && logUsage.model) {
@@ -34396,8 +34360,10 @@ function buildStatusMessage(args) {
34396
34360
  model: logUsage.model,
34397
34361
  fallbackContextTokens: contextTokens ?? void 0
34398
34362
  }) ?? contextTokens;
34399
- if (!inputTokens || inputTokens === 0) inputTokens = logUsage.input;
34400
- if (!outputTokens || outputTokens === 0) outputTokens = logUsage.output;
34363
+ inputTokens = logUsage.input ?? 0;
34364
+ outputTokens = logUsage.output ?? 0;
34365
+ if (logUsage.provider) activeProvider = logUsage.provider;
34366
+ if (logUsage.model) activeModel = logUsage.model;
34401
34367
  }
34402
34368
  }
34403
34369
  const thinkLevel = args.resolvedThink ?? args.sessionEntry?.thinkingLevel ?? args.agent?.thinkingDefault ?? "off";
@@ -34433,21 +34399,14 @@ function buildStatusMessage(args) {
34433
34399
  state: entry
34434
34400
  });
34435
34401
  const effectiveCostAuthMode = fallbackState.active ? activeAuthMode : selectedAuthMode ?? activeAuthMode;
34436
- const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
34402
+ const showCost = billingUsage?.cost?.total > 0 || effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
34437
34403
  const costConfig = showCost ? resolveModelCostConfig({
34438
34404
  provider: activeProvider,
34439
34405
  model: activeModel,
34440
34406
  config: args.config
34441
34407
  }) : void 0;
34442
- const hasUsage = typeof inputTokens === "number" || typeof outputTokens === "number";
34443
- const cost = showCost && hasUsage ? estimateUsageCost({
34444
- usage: {
34445
- input: inputTokens ?? void 0,
34446
- output: outputTokens ?? void 0
34447
- },
34448
- cost: costConfig
34449
- }) : void 0;
34450
- const costLabel = showCost && hasUsage ? formatUsd(cost) : void 0;
34408
+ const hasUsage = [inputTokens, outputTokens, cacheRead, cacheWrite].some(value => typeof value === "number");
34409
+ billingUsage ??= { input: inputTokens, output: outputTokens, cacheRead, cacheWrite };
34451
34410
  const selectedAuthLabel = selectedAuthLabelValue ? ` · 🔑 ${selectedAuthLabelValue}` : "";
34452
34411
  const channelModelNote = (() => {
34453
34412
  if (!args.config || !entry) return;
@@ -34479,9 +34438,9 @@ function buildStatusMessage(args) {
34479
34438
  const fallbackLine = fallbackState.active ? `↪️ Fallback: ${activeModelLabel}${showFallbackAuth ? ` · 🔑 ${activeAuthLabelValue}` : ""} (${fallbackState.reason ?? "selected model unavailable"})` : null;
34480
34439
  const commit = resolveCommitHash();
34481
34440
  const versionLine = `🦞 Johnness ${VERSION}${commit ? ` (${commit})` : ""}`;
34482
- const usagePair = formatUsagePair(inputTokens, outputTokens);
34483
- const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite);
34484
- const costLine = costLabel ? `💵 Cost: ${costLabel}` : null;
34441
+ const usagePair = formatUsagePair(inputTokens, outputTokens, cacheRead, cacheWrite);
34442
+ const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite, billingUsage);
34443
+ const costLine = showCost && hasUsage ? formatBillingCost(billingUsage, costConfig) : null;
34485
34444
  const usageCostLine = usagePair && costLine ? `${usagePair} · ${costLine}` : usagePair ?? costLine;
34486
34445
  const mediaLine = formatMediaUnderstandingLine(args.mediaDecisions);
34487
34446
  const voiceLine = formatVoiceModeLine(args.config, args.sessionEntry);
@@ -1875,7 +1875,8 @@ function resolveModelCost(raw) {
1875
1875
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
1876
1876
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
1877
1877
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
1878
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
1878
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
1879
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
1879
1880
  };
1880
1881
  }
1881
1882
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -5967,7 +5968,8 @@ const ModelDefinitionSchema = z.object({
5967
5968
  input: z.number().optional(),
5968
5969
  output: z.number().optional(),
5969
5970
  cacheRead: z.number().optional(),
5970
- cacheWrite: z.number().optional()
5971
+ cacheWrite: z.number().optional(),
5972
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
5971
5973
  }).strict().optional(),
5972
5974
  contextWindow: z.number().positive().optional(),
5973
5975
  maxTokens: z.number().positive().optional(),
@@ -5027,7 +5027,8 @@ function resolveModelCost(raw) {
5027
5027
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
5028
5028
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
5029
5029
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
5030
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
5030
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
5031
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
5031
5032
  };
5032
5033
  }
5033
5034
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -8900,7 +8901,8 @@ const ModelDefinitionSchema = z.object({
8900
8901
  input: z.number().optional(),
8901
8902
  output: z.number().optional(),
8902
8903
  cacheRead: z.number().optional(),
8903
- cacheWrite: z.number().optional()
8904
+ cacheWrite: z.number().optional(),
8905
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
8904
8906
  }).strict().optional(),
8905
8907
  contextWindow: z.number().positive().optional(),
8906
8908
  maxTokens: z.number().positive().optional(),
@@ -1733,7 +1733,8 @@ function resolveModelCost(raw) {
1733
1733
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
1734
1734
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
1735
1735
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
1736
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
1736
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
1737
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
1737
1738
  };
1738
1739
  }
1739
1740
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -8512,7 +8513,8 @@ const ModelDefinitionSchema = z.object({
8512
8513
  input: z.number().optional(),
8513
8514
  output: z.number().optional(),
8514
8515
  cacheRead: z.number().optional(),
8515
- cacheWrite: z.number().optional()
8516
+ cacheWrite: z.number().optional(),
8517
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
8516
8518
  }).strict().optional(),
8517
8519
  contextWindow: z.number().positive().optional(),
8518
8520
  maxTokens: z.number().positive().optional(),
@@ -1708,7 +1708,8 @@ function resolveModelCost(raw) {
1708
1708
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
1709
1709
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
1710
1710
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
1711
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
1711
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
1712
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
1712
1713
  };
1713
1714
  }
1714
1715
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -8401,7 +8402,8 @@ const ModelDefinitionSchema = z.object({
8401
8402
  input: z.number().optional(),
8402
8403
  output: z.number().optional(),
8403
8404
  cacheRead: z.number().optional(),
8404
- cacheWrite: z.number().optional()
8405
+ cacheWrite: z.number().optional(),
8406
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
8405
8407
  }).strict().optional(),
8406
8408
  contextWindow: z.number().positive().optional(),
8407
8409
  maxTokens: z.number().positive().optional(),
@@ -1639,7 +1639,8 @@ function resolveModelCost(raw) {
1639
1639
  input: typeof raw?.input === "number" ? raw.input : DEFAULT_MODEL_COST.input,
1640
1640
  output: typeof raw?.output === "number" ? raw.output : DEFAULT_MODEL_COST.output,
1641
1641
  cacheRead: typeof raw?.cacheRead === "number" ? raw.cacheRead : DEFAULT_MODEL_COST.cacheRead,
1642
- cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite
1642
+ cacheWrite: typeof raw?.cacheWrite === "number" ? raw.cacheWrite : DEFAULT_MODEL_COST.cacheWrite,
1643
+ ...typeof raw?.cacheWrite1h === "number" ? { cacheWrite1h: raw.cacheWrite1h } : {} // JOHNNESS_PATCH_USAGE_BILLING_91
1643
1644
  };
1644
1645
  }
1645
1646
  function resolveAnthropicDefaultAuthMode(cfg) {
@@ -6561,7 +6562,8 @@ const ModelDefinitionSchema = z.object({
6561
6562
  input: z.number().optional(),
6562
6563
  output: z.number().optional(),
6563
6564
  cacheRead: z.number().optional(),
6564
- cacheWrite: z.number().optional()
6565
+ cacheWrite: z.number().optional(),
6566
+ cacheWrite1h: z.number().nonnegative().optional() // JOHNNESS_PATCH_USAGE_BILLING_91
6565
6567
  }).strict().optional(),
6566
6568
  contextWindow: z.number().positive().optional(),
6567
6569
  maxTokens: z.number().positive().optional(),
@@ -1,3 +1,4 @@
1
+ import { normalizeBillingUsage, readLatestBillingUsage, formatBillingCost } from "./usage-billing.js"; // JOHNNESS_PATCH_USAGE_BILLING_91 dist/pi-embedded-C6ITuRXf.js
1
2
  import { importOptional66 } from "./optional-module.js";
2
3
  const JOHNNESS_PATCH_SLIM_TREE_66 = true;
3
4
  import { createImageGenerateTool } from "./image-generate.js";
@@ -24991,21 +24992,7 @@ function hasNonzeroUsage(usage) {
24991
24992
  ].some((v) => typeof v === "number" && Number.isFinite(v) && v > 0);
24992
24993
  }
24993
24994
  function normalizeUsage(raw) {
24994
- if (!raw) return;
24995
- const rawInput = asFiniteNumber(raw.input ?? raw.inputTokens ?? raw.input_tokens ?? raw.promptTokens ?? raw.prompt_tokens);
24996
- const input = rawInput !== void 0 && rawInput < 0 ? 0 : rawInput;
24997
- const output = asFiniteNumber(raw.output ?? raw.outputTokens ?? raw.output_tokens ?? raw.completionTokens ?? raw.completion_tokens);
24998
- const cacheRead = asFiniteNumber(raw.cacheRead ?? raw.cache_read ?? raw.cache_read_input_tokens ?? raw.cached_tokens ?? raw.prompt_tokens_details?.cached_tokens);
24999
- const cacheWrite = asFiniteNumber(raw.cacheWrite ?? raw.cache_write ?? raw.cache_creation_input_tokens);
25000
- const total = asFiniteNumber(raw.total ?? raw.totalTokens ?? raw.total_tokens);
25001
- if (input === void 0 && output === void 0 && cacheRead === void 0 && cacheWrite === void 0 && total === void 0) return;
25002
- return {
25003
- input,
25004
- output,
25005
- cacheRead,
25006
- cacheWrite,
25007
- total
25008
- };
24995
+ return normalizeBillingUsage(raw);
25009
24996
  }
25010
24997
  function derivePromptTokens(usage) {
25011
24998
  if (!usage) return;
@@ -25393,62 +25380,19 @@ const readUsageFromSessionLog = (sessionId, sessionEntry, agentId, sessionKey, s
25393
25380
  } catch {
25394
25381
  return;
25395
25382
  }
25396
- if (!fs$1.existsSync(logPath)) return;
25397
- try {
25398
- const TAIL_BYTES = 8192;
25399
- const stat = fs$1.statSync(logPath);
25400
- const offset = Math.max(0, stat.size - TAIL_BYTES);
25401
- const buf = Buffer.alloc(Math.min(TAIL_BYTES, stat.size));
25402
- const fd = fs$1.openSync(logPath, "r");
25403
- try {
25404
- fs$1.readSync(fd, buf, 0, buf.length, offset);
25405
- } finally {
25406
- fs$1.closeSync(fd);
25407
- }
25408
- const tail = buf.toString("utf-8");
25409
- const lines = (offset > 0 ? tail.slice(tail.indexOf("\n") + 1) : tail).split(/\n+/);
25410
- let input = 0;
25411
- let output = 0;
25412
- let promptTokens = 0;
25413
- let model;
25414
- let lastUsage;
25415
- for (const line of lines) {
25416
- if (!line.trim()) continue;
25417
- try {
25418
- const parsed = JSON.parse(line);
25419
- const usage = normalizeUsage(parsed.message?.usage ?? parsed.usage);
25420
- if (usage) lastUsage = usage;
25421
- model = parsed.message?.model ?? parsed.model ?? model;
25422
- } catch {}
25423
- }
25424
- if (!lastUsage) return;
25425
- input = lastUsage.input ?? 0;
25426
- output = lastUsage.output ?? 0;
25427
- promptTokens = derivePromptTokens(lastUsage) ?? lastUsage.total ?? input + output;
25428
- const total = lastUsage.total ?? promptTokens + output;
25429
- if (promptTokens === 0 && total === 0) return;
25430
- return {
25431
- input,
25432
- output,
25433
- promptTokens,
25434
- total,
25435
- model
25436
- };
25437
- } catch {
25438
- return;
25439
- }
25383
+ return readLatestBillingUsage(logPath);
25440
25384
  };
25441
- const formatUsagePair = (input, output) => {
25442
- if (input == null && output == null) return null;
25443
- return `🧮 Tokens: ${typeof input === "number" ? formatTokenCount(input) : "?"} in / ${typeof output === "number" ? formatTokenCount(output) : "?"} out`;
25385
+ const formatUsagePair = (input, output, cacheRead, cacheWrite) => {
25386
+ if (input == null && output == null && cacheRead == null && cacheWrite == null) return null;
25387
+ const count = value => typeof value === "number" ? value.toLocaleString("en-US") : "?";
25388
+ return `🧮 Tokens: ${count((input ?? 0) + (cacheWrite ?? 0))} new in / ${count(cacheRead ?? 0)} cached / ${count(output)} out (last API call)`;
25444
25389
  };
25445
- const formatCacheLine = (input, cacheRead, cacheWrite) => {
25390
+ const formatCacheLine = (input, cacheRead, cacheWrite, usage) => {
25446
25391
  if (!cacheRead && !cacheWrite) return null;
25447
- if ((typeof cacheRead !== "number" || cacheRead <= 0) && (typeof cacheWrite !== "number" || cacheWrite <= 0)) return null;
25448
- const cachedLabel = typeof cacheRead === "number" ? formatTokenCount(cacheRead) : "0";
25449
- const newLabel = typeof cacheWrite === "number" ? formatTokenCount(cacheWrite) : "0";
25450
- const totalInput = (typeof cacheRead === "number" ? cacheRead : 0) + (typeof cacheWrite === "number" ? cacheWrite : 0) + (typeof input === "number" ? input : 0);
25451
- return `🗄️ Cache: ${totalInput > 0 && typeof cacheRead === "number" ? Math.round(cacheRead / totalInput * 100) : 0}% hit · ${cachedLabel} cached, ${newLabel} new`;
25392
+ const total = (input ?? 0) + (cacheRead ?? 0) + (cacheWrite ?? 0);
25393
+ const count = value => (value ?? 0).toLocaleString("en-US");
25394
+ const ttl = usage?.cacheWrite5m !== undefined || usage?.cacheWrite1h !== undefined ? ` (${count(usage.cacheWrite5m)} 5m / ${count(usage.cacheWrite1h)} 1h)` : "";
25395
+ return `🗄️ Cache: ${total > 0 ? Math.round((cacheRead ?? 0) / total * 100) : 0}% hit · ${count(input)} uncached + ${count(cacheWrite)} written${ttl}`;
25452
25396
  };
25453
25397
  const formatMediaUnderstandingLine = (decisions) => {
25454
25398
  if (!decisions || decisions.length === 0) return null;
@@ -25527,10 +25471,14 @@ function buildStatusMessage(args) {
25527
25471
  let outputTokens = entry?.outputTokens;
25528
25472
  let cacheRead = entry?.cacheRead;
25529
25473
  let cacheWrite = entry?.cacheWrite;
25474
+ let billingUsage;
25530
25475
  let totalTokens = entry?.totalTokens ?? (entry?.inputTokens ?? 0) + (entry?.outputTokens ?? 0);
25531
- if (args.includeTranscriptUsage) {
25476
+ if (entry?.sessionId) { // Always use a coherent latest API call on both status paths.
25532
25477
  const logUsage = readUsageFromSessionLog(entry?.sessionId, entry, args.agentId, args.sessionKey, args.sessionStorePath);
25533
25478
  if (logUsage) {
25479
+ billingUsage = logUsage;
25480
+ cacheRead = logUsage.cacheRead ?? 0;
25481
+ cacheWrite = logUsage.cacheWrite ?? 0;
25534
25482
  const candidate = logUsage.promptTokens || logUsage.total;
25535
25483
  if (!totalTokens || totalTokens === 0 || candidate > totalTokens) totalTokens = candidate;
25536
25484
  if (!entry?.model && logUsage.model) {
@@ -25549,8 +25497,10 @@ function buildStatusMessage(args) {
25549
25497
  model: logUsage.model,
25550
25498
  fallbackContextTokens: contextTokens ?? void 0
25551
25499
  }) ?? contextTokens;
25552
- if (!inputTokens || inputTokens === 0) inputTokens = logUsage.input;
25553
- if (!outputTokens || outputTokens === 0) outputTokens = logUsage.output;
25500
+ inputTokens = logUsage.input ?? 0;
25501
+ outputTokens = logUsage.output ?? 0;
25502
+ if (logUsage.provider) activeProvider = logUsage.provider;
25503
+ if (logUsage.model) activeModel = logUsage.model;
25554
25504
  }
25555
25505
  }
25556
25506
  const thinkLevel = args.resolvedThink ?? args.sessionEntry?.thinkingLevel ?? args.agent?.thinkingDefault ?? "off";
@@ -25586,21 +25536,14 @@ function buildStatusMessage(args) {
25586
25536
  state: entry
25587
25537
  });
25588
25538
  const effectiveCostAuthMode = fallbackState.active ? activeAuthMode : selectedAuthMode ?? activeAuthMode;
25589
- const showCost = effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
25539
+ const showCost = billingUsage?.cost?.total > 0 || effectiveCostAuthMode === "api-key" || effectiveCostAuthMode === "mixed";
25590
25540
  const costConfig = showCost ? resolveModelCostConfig({
25591
25541
  provider: activeProvider,
25592
25542
  model: activeModel,
25593
25543
  config: args.config
25594
25544
  }) : void 0;
25595
- const hasUsage = typeof inputTokens === "number" || typeof outputTokens === "number";
25596
- const cost = showCost && hasUsage ? estimateUsageCost({
25597
- usage: {
25598
- input: inputTokens ?? void 0,
25599
- output: outputTokens ?? void 0
25600
- },
25601
- cost: costConfig
25602
- }) : void 0;
25603
- const costLabel = showCost && hasUsage ? formatUsd(cost) : void 0;
25545
+ const hasUsage = [inputTokens, outputTokens, cacheRead, cacheWrite].some(value => typeof value === "number");
25546
+ billingUsage ??= { input: inputTokens, output: outputTokens, cacheRead, cacheWrite };
25604
25547
  const selectedAuthLabel = selectedAuthLabelValue ? ` · 🔑 ${selectedAuthLabelValue}` : "";
25605
25548
  const channelModelNote = (() => {
25606
25549
  if (!args.config || !entry) return;
@@ -25632,9 +25575,9 @@ function buildStatusMessage(args) {
25632
25575
  const fallbackLine = fallbackState.active ? `↪️ Fallback: ${activeModelLabel}${showFallbackAuth ? ` · 🔑 ${activeAuthLabelValue}` : ""} (${fallbackState.reason ?? "selected model unavailable"})` : null;
25633
25576
  const commit = resolveCommitHash();
25634
25577
  const versionLine = `🦞 Johnness ${VERSION}${commit ? ` (${commit})` : ""}`;
25635
- const usagePair = formatUsagePair(inputTokens, outputTokens);
25636
- const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite);
25637
- const costLine = costLabel ? `💵 Cost: ${costLabel}` : null;
25578
+ const usagePair = formatUsagePair(inputTokens, outputTokens, cacheRead, cacheWrite);
25579
+ const cacheLine = formatCacheLine(inputTokens, cacheRead, cacheWrite, billingUsage);
25580
+ const costLine = showCost && hasUsage ? formatBillingCost(billingUsage, costConfig) : null;
25638
25581
  const usageCostLine = usagePair && costLine ? `${usagePair} · ${costLine}` : usagePair ?? costLine;
25639
25582
  const mediaLine = formatMediaUnderstandingLine(args.mediaDecisions);
25640
25583
  const voiceLine = formatVoiceModeLine(args.config, args.sessionEntry);
@@ -96745,6 +96688,8 @@ function normalizeAssistantUsageSnapshot(usage) {
96745
96688
  cacheRead,
96746
96689
  cacheWrite,
96747
96690
  totalTokens,
96691
+ ...normalized.cacheWrite5m !== undefined ? { cacheWrite5m: normalized.cacheWrite5m } : {},
96692
+ ...normalized.cacheWrite1h !== undefined ? { cacheWrite1h: normalized.cacheWrite1h } : {},
96748
96693
  ...cost ? { cost } : {}
96749
96694
  };
96750
96695
  }