python-yama 0.10.7__tar.gz → 0.10.8__tar.gz

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 (76) hide show
  1. {python_yama-0.10.7 → python_yama-0.10.8}/PKG-INFO +1 -1
  2. {python_yama-0.10.7 → python_yama-0.10.8}/docs/agent-skill-test-framework-design.md +21 -10
  3. {python_yama-0.10.7 → python_yama-0.10.8}/skills/eval-plugin-with-yama/SKILL.md +4 -1
  4. {python_yama-0.10.7 → python_yama-0.10.8}/skills/eval-plugin-with-yama/references/debugging.md +5 -3
  5. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_report.py +98 -0
  6. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_usage.py +135 -6
  7. {python_yama-0.10.7 → python_yama-0.10.8}/yama/llm.py +26 -4
  8. {python_yama-0.10.7 → python_yama-0.10.8}/yama/report.py +45 -6
  9. {python_yama-0.10.7 → python_yama-0.10.8}/yama/runner.py +15 -1
  10. {python_yama-0.10.7 → python_yama-0.10.8}/.github/workflows/publish.yml +0 -0
  11. {python_yama-0.10.7 → python_yama-0.10.8}/.gitignore +0 -0
  12. {python_yama-0.10.7 → python_yama-0.10.8}/CLAUDE.md +0 -0
  13. {python_yama-0.10.7 → python_yama-0.10.8}/LICENSE +0 -0
  14. {python_yama-0.10.7 → python_yama-0.10.8}/README.md +0 -0
  15. {python_yama-0.10.7 → python_yama-0.10.8}/README.zh-CN.md +0 -0
  16. {python_yama-0.10.7 → python_yama-0.10.8}/docs/case-schema-reference.md +0 -0
  17. {python_yama-0.10.7 → python_yama-0.10.8}/docs/superpowers/plans/2026-08-17-concurrency-defaults.md +0 -0
  18. {python_yama-0.10.7 → python_yama-0.10.8}/docs/superpowers/plans/2026-08-17-step-timeout-failure.md +0 -0
  19. {python_yama-0.10.7 → python_yama-0.10.8}/docs/superpowers/specs/2026-08-17-concurrency-defaults-design.md +0 -0
  20. {python_yama-0.10.7 → python_yama-0.10.8}/docs/superpowers/specs/2026-08-17-step-timeout-failure-design.md +0 -0
  21. {python_yama-0.10.7 → python_yama-0.10.8}/docs/yama-intro.html +0 -0
  22. {python_yama-0.10.7 → python_yama-0.10.8}/pyproject.toml +0 -0
  23. {python_yama-0.10.7 → python_yama-0.10.8}/skills/eval-plugin-with-yama/references/case-schema.md +0 -0
  24. {python_yama-0.10.7 → python_yama-0.10.8}/skills/eval-plugin-with-yama/references/install-upgrade.md +0 -0
  25. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_assertions.py +0 -0
  26. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_cli.py +0 -0
  27. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_cli_shim.py +0 -0
  28. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_dashboard.py +0 -0
  29. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_framework.py +0 -0
  30. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_judge_llm.py +0 -0
  31. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_llm.py +0 -0
  32. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_main_entrypoint.py +0 -0
  33. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_mocks.py +0 -0
  34. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_openai_llm.py +0 -0
  35. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_pr.py +0 -0
  36. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_preview.py +0 -0
  37. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_public_api.py +0 -0
  38. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_report_bundle.py +0 -0
  39. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_report_upload.py +0 -0
  40. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_run_plan.py +0 -0
  41. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_runner.py +0 -0
  42. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_skill_loader.py +0 -0
  43. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_skill_tool.py +0 -0
  44. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_thread_case.py +0 -0
  45. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_tool_loader.py +0 -0
  46. {python_yama-0.10.7 → python_yama-0.10.8}/tests/test_workspace.py +0 -0
  47. {python_yama-0.10.7 → python_yama-0.10.8}/uv.lock +0 -0
  48. {python_yama-0.10.7 → python_yama-0.10.8}/yama/__init__.py +0 -0
  49. {python_yama-0.10.7 → python_yama-0.10.8}/yama/__main__.py +0 -0
  50. {python_yama-0.10.7 → python_yama-0.10.8}/yama/assertions.py +0 -0
  51. {python_yama-0.10.7 → python_yama-0.10.8}/yama/cli.py +0 -0
  52. {python_yama-0.10.7 → python_yama-0.10.8}/yama/dashboard.py +0 -0
  53. {python_yama-0.10.7 → python_yama-0.10.8}/yama/judge_llm.py +0 -0
  54. {python_yama-0.10.7 → python_yama-0.10.8}/yama/judge_routing.py +0 -0
  55. {python_yama-0.10.7 → python_yama-0.10.8}/yama/loaders/__init__.py +0 -0
  56. {python_yama-0.10.7 → python_yama-0.10.8}/yama/loaders/case.py +0 -0
  57. {python_yama-0.10.7 → python_yama-0.10.8}/yama/loaders/common.py +0 -0
  58. {python_yama-0.10.7 → python_yama-0.10.8}/yama/loaders/skill_loader.py +0 -0
  59. {python_yama-0.10.7 → python_yama-0.10.8}/yama/loaders/tool_loader.py +0 -0
  60. {python_yama-0.10.7 → python_yama-0.10.8}/yama/mocks.py +0 -0
  61. {python_yama-0.10.7 → python_yama-0.10.8}/yama/models.py +0 -0
  62. {python_yama-0.10.7 → python_yama-0.10.8}/yama/openai_llm.py +0 -0
  63. {python_yama-0.10.7 → python_yama-0.10.8}/yama/pr.py +0 -0
  64. {python_yama-0.10.7 → python_yama-0.10.8}/yama/preview.py +0 -0
  65. {python_yama-0.10.7 → python_yama-0.10.8}/yama/report_bundle.py +0 -0
  66. {python_yama-0.10.7 → python_yama-0.10.8}/yama/report_upload.py +0 -0
  67. {python_yama-0.10.7 → python_yama-0.10.8}/yama/thread_case.py +0 -0
  68. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/__init__.py +0 -0
  69. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/base.py +0 -0
  70. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/bash/__init__.py +0 -0
  71. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/bash/_cli_shim.py +0 -0
  72. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/bash/bash.py +0 -0
  73. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/bash/fs.py +0 -0
  74. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/skill/__init__.py +0 -0
  75. {python_yama-0.10.7 → python_yama-0.10.8}/yama/tools/skill/skill.py +0 -0
  76. {python_yama-0.10.7 → python_yama-0.10.8}/yama/workspace.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-yama
3
- Version: 0.10.7
3
+ Version: 0.10.8
4
4
  Summary: A reproducible evaluation runner for tool-using Agent skills
5
5
  Project-URL: Repository, https://github.com/world-sim-dev/yama
6
6
  Project-URL: Issues, https://github.com/world-sim-dev/yama/issues
@@ -2442,23 +2442,34 @@ class CaseResult:
2442
2442
  def request_duration_ms(self) -> float | None: ... # 各 run request_duration_ms() 之和
2443
2443
  ```
2444
2444
 
2445
- **Token 用量统计**没有独立的 `Usage` 类,而是一条贯穿各层的普通字典字段
2446
- (`{"prompt_tokens", "completion_tokens", "total_tokens"}`,值均为 int):
2445
+ **Token 用量统计**没有独立的 `Usage` 类,而是一条贯穿各层的普通字典字段。
2446
+ `prompt_tokens`、`completion_tokens`、`total_tokens` 是始终参与聚合的三个核心 int
2447
+ 计数器;Provider 返回缓存读取数据时,字典另带可选的 `cache_read_tokens` int:
2447
2448
 
2448
2449
  - **采集**:`llm.py:_normalized_usage` 把 Provider 返回的 usage 负载规范化成上述
2449
- 三个计数器(丢弃 `prompt_tokens_details` 等 Provider 特有的明细块;缺
2450
- `total_tokens` 时用 prompt+completion 补齐;三个计数器都缺时得到 `None`),挂在
2451
- `LLMResponse.usage`。`ScriptedLLMClient` 的脚本响应可带可选的 `usage` 键,走同一
2452
- 个规范化(用于测试与回放)。
2450
+ 三个核心计数器(缺 `total_tokens` 时仍用 prompt+completion 补齐;三个核心计数器
2451
+ 都缺时得到 `None`),挂在 `LLMResponse.usage`。输入、输出、总量完全沿用 Provider
2452
+ 的 `prompt_tokens` / `completion_tokens` / `total_tokens` 语义,不反推输入、不额外
2453
+ 累加 reasoning token。Cache Read 按 `prompt_tokens_details.cached_tokens`、
2454
+ `cache_read_input_tokens`、`cached_content_token_count`、`cache_tokens`、
2455
+ `prompt_cache_hit_tokens` 的顺序取第一个有效非负值;多个别名不相加,也不把 Cache
2456
+ Read 加入 prompt/total。`ScriptedLLMClient` 的脚本响应可带可选的 `usage` 键,走
2457
+ 同一个规范化(用于测试与回放)。
2453
2458
  - **记录**:tool-loop 的每次请求把非空 usage 写进对应请求记录
2454
2459
  (`requests[].usage`,见 `runner.py:_run_step`);Judge 调用不经过 `requests`
2455
2460
  记录,`assertions.py:run_judge` 把它的 usage 补在返回的 `judge_result["usage"]`
2456
2461
  上(Provider 未报用量时两处都没有该键)。
2457
- - **聚合**:`runner.py:combine_usage` 对多个 usage 字典逐键求和,全部无数据时返回
2462
+ - **聚合**:`runner.py:combine_usage` 对三个核心计数器逐键求和,全部无数据时返回
2458
2463
  `None`(「没有数据」与「0 token」必须可区分——脚本回放或中途死掉的 run 不应显示
2459
- 成 0)。`RunResult.usage()` = 该 run 全部 `requests[].usage` + 各 Step
2460
- `judge_result["usage"]` 之和;`CaseResult.usage()` = 各 run 之和。两者的
2461
- `to_dict()` 都带 `usage` 字段,因此 `result.json` 与 `--json` 输出天然携带。
2464
+ 成 0)。可选 `cache_read_tokens` 只累加实际返回的有效值:所有调用都缺失时聚合结果
2465
+ 继续缺失该键,明确返回 0 时则保留 0。`RunResult.usage()` = 该 run 全部
2466
+ `requests[].usage` + 各 Step `judge_result["usage"]` 之和;`CaseResult.usage()` = 各
2467
+ run 之和。两者的 `to_dict()` 都带 `usage` 字段,因此 `result.json` 与 `--json`
2468
+ 输出天然携带。HTML Run 标题、comparison 表和 Case 顶部 metadata 在
2469
+ `prompt_tokens > 0` 且 `0 <= cache_read_tokens <= prompt_tokens` 时展示两位小数的
2470
+ `cache_read_tokens / prompt_tokens`;Case 级比例直接使用 `CaseResult.usage()`,即
2471
+ `所有 Run 的 cache_read_tokens 之和 / 所有 Run 的 prompt_tokens 之和`,不是逐 Run
2472
+ 百分比的平均值。缺失或异常时显示 `–`。
2462
2473
  - **耗时**:tool-loop 的每次请求都会在 `requests[].duration_ms` 记录本轮
2463
2474
  `llm.complete()` 耗时。`runner.py:combine_duration_ms` 对这些毫秒数求和,
2464
2475
  全部无数据时返回 `None`。`RunResult.request_duration_ms()` =
@@ -299,7 +299,10 @@ macOS 在 exFAT 等卷上生成的 AppleDouble `._*.yaml` 会在解析前被忽
299
299
  `LLM error` / `Framework error` 文本在对应 Case header 里看。每轮 LLM Request
300
300
  详情会展示 Method、Duration、Arguments、Response,Response 是完整 assistant
301
301
  message(含 tool_calls);Case/Run/Step 层级仍展示 tool-loop LLM Request
302
- 耗时总和。
302
+ 耗时总和。Provider 返回 Cache Read 时,Run 标题与 comparison 表还会展示输入、输出、
303
+ 总 Token 和 `Cache Read / prompt_tokens` 的缓存读取占比;Case 顶部 metadata 会按所有
304
+ Run 的 Token 总量计算同一口径的加权占比,而不是对各 Run 的百分比取平均。未返回或
305
+ 比例异常时显示 `–`。
303
306
 
304
307
  当仓库 CI 使用 `yama pr-run --report --upload-report --commit-id <sha>` 时,yama 会上传当前
305
308
  commit 报告、每个运行到的 Case 详情报告,并更新受影响 Plugin 的远端 latest 报告。
@@ -32,11 +32,11 @@
32
32
 
33
33
  | 文件 | 内容 | 什么时候看 |
34
34
  | --- | --- | --- |
35
- | `result.json` | run 是否通过、`status`(`pass`/`fail`/`error`)、`error`/`traceback`、全量数据汇总(含 run 级 `usage` token 汇总、`request_duration_ms` LLM Request 耗时汇总、`runs[].tag_summary` 与每个 Step 的 `tag_summary`,Provider 未报用量时 usage 为 null) | **永远先看**:status/error 决定排查层级 |
35
+ | `result.json` | run 是否通过、`status`(`pass`/`fail`/`error`)、`error`/`traceback`、全量数据汇总(含 run 级 `usage` token 汇总、`request_duration_ms` LLM Request 耗时汇总、`runs[].tag_summary` 与每个 Step 的 `tag_summary`,Provider 未报用量时 usage 为 null;Provider 返回缓存读取时 usage 另带可选 `cache_read_tokens`) | **永远先看**:status/error 决定排查层级 |
36
36
  | `hard-checks.json` | `{step_id: [每条断言的 type/passed/label/expected/actual/reason/detail]}` | Step 断言失败时 |
37
37
  | `mock-events.json` | 每次 Tool 调用:`tool/arguments/result/is_error/match_failed/state_diff/duration_ms`。`is_error` 是语义判定:结果带**非 null** `error`,或 bash 信封 `exit_code != 0`;成功结果里的 `error: null` 不算错误 | 怀疑 Mock 匹配 / Tool 行为时 |
38
38
  | `mock-state.json` | run 结束时的最终共享 state | 有状态 Handler / Python Checker 断言失败时 |
39
- | `requests/request-00N.json` | 每次真实 LLM 调用的完整入参(messages/tools)、完整 assistant response 与 `duration_ms`;请求失败时带 `error` 字段,Provider 报了用量时带 `usage`(prompt/completion/total tokens) | 怀疑「模型到底看到了什么」或哪一轮慢时 |
39
+ | `requests/request-00N.json` | 每次真实 LLM 调用的完整入参(messages/tools)、完整 assistant response 与 `duration_ms`;请求失败时带 `error` 字段,Provider 报了用量时带 `usage`(prompt/completion/total tokens,以及可选的 Cache Read) | 怀疑「模型到底看到了什么」或哪一轮慢时 |
40
40
  | `transcript.json` | 每个 Step 结束后的完整对话快照(含注入消息、Tool Result) | 复盘模型行为时 |
41
41
  | `rendered-system-prompt.txt` | 实际发送的 System Prompt(含 `<available_skills>`) | 怀疑 Skill 没暴露 / 渲染不对时 |
42
42
  | `case.resolved.yaml` | Case 展开后的编译产物 | 怀疑路径 / 默认值解析不符合预期时 |
@@ -45,7 +45,9 @@
45
45
 
46
46
  `--report` 生成自包含 HTML,逐 Case/Run/Step 展示 transcript、Tool Call、断言、
47
47
  LLM token 用量与 LLM Request 耗时(顶部 `CASES PASSED` / `CASES ERRORS` /
48
- `LLM TOKENS` 汇总卡片 + 各层级耗时明细)。每轮 LLM Request
48
+ `LLM TOKENS` 汇总卡片 + 各层级耗时明细;每个 Run 还会展示输入、输出、总 Token,
49
+ Provider 返回 Cache Read 时展示 `Cache Read / prompt_tokens` 的缓存读取占比;Case 顶部
50
+ metadata 按所有 Run 的 Cache Read 与 prompt token 总和计算加权占比)。每轮 LLM Request
49
51
  详情会同时展示 Method、Duration、Arguments、Response,Response 是完整 assistant
50
52
  message(含 tool_calls),适合人工浏览;省略路径时默认写带
51
53
  时间戳的 `.yama/reports/report-<ts>.html`(历史累积,`latest.html` 是指向最新
@@ -537,6 +537,7 @@ class TokenUsageReportTests(unittest.TestCase):
537
537
  "prompt_tokens": 50,
538
538
  "completion_tokens": 10,
539
539
  "total_tokens": 60,
540
+ "cache_read_tokens": 40,
540
541
  },
541
542
  },
542
543
  )
@@ -553,6 +554,7 @@ class TokenUsageReportTests(unittest.TestCase):
553
554
  "prompt_tokens": 1000,
554
555
  "completion_tokens": 200,
555
556
  "total_tokens": 1200,
557
+ "cache_read_tokens": 800,
556
558
  },
557
559
  }
558
560
  ],
@@ -575,6 +577,101 @@ class TokenUsageReportTests(unittest.TestCase):
575
577
  # Judge section shows its own call's consumption.
576
578
  self.assertIn("60 tokens (50 in / 10 out)", html)
577
579
 
580
+ def test_run_summary_shows_aggregate_cache_read_share(self) -> None:
581
+ case = _make_case("")
582
+ html = render_html_report([case], [self.make_result(case)])
583
+ self.assertIn(
584
+ "1,260 tokens (1,050 in / 210 out) · cache read 80.00%",
585
+ html,
586
+ )
587
+
588
+ def test_case_metadata_uses_weighted_cache_read_share_across_runs(self) -> None:
589
+ case = _make_case("")
590
+ result = self.make_result(case)
591
+ result.runs.append(
592
+ RunResult(
593
+ run_index=2,
594
+ passed=True,
595
+ requests=[
596
+ {
597
+ "step_id": "greet",
598
+ "request_index": 0,
599
+ "usage": {
600
+ "prompt_tokens": 150,
601
+ "completion_tokens": 10,
602
+ "total_tokens": 160,
603
+ "cache_read_tokens": 30,
604
+ },
605
+ }
606
+ ],
607
+ )
608
+ )
609
+
610
+ html = render_html_report([case], [result])
611
+
612
+ # Weighted case share: (840 + 30) / (1,050 + 150), not (80% + 20%) / 2.
613
+ self.assertIn(
614
+ "<div><span>Cache read</span><strong>72.50%</strong></div>",
615
+ html,
616
+ )
617
+
618
+ def test_run_comparison_separates_total_input_output_and_cache_read(self) -> None:
619
+ case = _make_case("")
620
+ result = self.make_result(case)
621
+ result.runs.append(replace(result.runs[0], run_index=2))
622
+
623
+ html = render_html_report([case], [result])
624
+
625
+ self.assertIn(
626
+ "<th>Total tokens</th><th>Input tokens</th>"
627
+ "<th>Output tokens</th><th>Cache read</th>",
628
+ html,
629
+ )
630
+ self.assertIn(
631
+ "<td>1,260</td><td>1,050</td><td>210</td><td>80.00%</td>",
632
+ html,
633
+ )
634
+
635
+ def test_run_cache_read_share_preserves_zero(self) -> None:
636
+ case = _make_case("")
637
+ result = self.make_result(case)
638
+ result.runs[0].requests[0]["usage"]["cache_read_tokens"] = 0
639
+ result.runs[0].steps[0].judge_result["usage"]["cache_read_tokens"] = 0
640
+
641
+ html = render_html_report([case], [result])
642
+
643
+ self.assertIn("cache read 0.00%", html)
644
+
645
+ def test_run_cache_read_share_is_unavailable_when_not_reported(self) -> None:
646
+ case = _make_case("")
647
+ result = self.make_result(case)
648
+ result.runs[0].requests[0]["usage"].pop("cache_read_tokens")
649
+ result.runs[0].steps[0].judge_result["usage"].pop("cache_read_tokens")
650
+ result.runs.append(replace(result.runs[0], run_index=2))
651
+
652
+ html = render_html_report([case], [result])
653
+
654
+ self.assertNotIn("cache read ", html)
655
+ self.assertIn(
656
+ "<td>1,260</td><td>1,050</td><td>210</td><td>–</td>",
657
+ html,
658
+ )
659
+
660
+ def test_run_cache_read_share_rejects_values_above_input(self) -> None:
661
+ case = _make_case("")
662
+ result = self.make_result(case)
663
+ result.runs[0].requests[0]["usage"]["cache_read_tokens"] = 1001
664
+ result.runs[0].steps[0].judge_result["usage"]["cache_read_tokens"] = 50
665
+ result.runs.append(replace(result.runs[0], run_index=2))
666
+
667
+ html = render_html_report([case], [result])
668
+
669
+ self.assertNotIn("cache read ", html)
670
+ self.assertIn(
671
+ "<td>1,260</td><td>1,050</td><td>210</td><td>–</td>",
672
+ html,
673
+ )
674
+
578
675
  def test_step_summaries_carry_a_step_number_prefix_and_id_chip(self) -> None:
579
676
  case = _make_case("")
580
677
  html = render_html_report([case], [self.make_result(case)])
@@ -607,6 +704,7 @@ class TokenUsageReportTests(unittest.TestCase):
607
704
  )
608
705
  html = render_html_report([case], [result])
609
706
  self.assertIn("<div><span>LLM tokens</span><strong>–</strong></div>", html)
707
+ self.assertIn("<div><span>Cache read</span><strong>–</strong></div>", html)
610
708
  self.assertIn('<div class="summary-card"><span>LLM TOKENS</span><strong>–</strong></div>', html)
611
709
 
612
710
 
@@ -29,7 +29,7 @@ class NormalizedUsageTests(unittest.TestCase):
29
29
  self.assertIsNone(_normalized_usage({"prompt_tokens_details": {"cached": 3}}))
30
30
  self.assertIsNone(_normalized_usage({"prompt_tokens": "12"}))
31
31
 
32
- def test_keeps_only_the_three_counters_as_ints(self) -> None:
32
+ def test_keeps_nested_cache_read_without_changing_core_counters(self) -> None:
33
33
  usage = _normalized_usage(
34
34
  {
35
35
  "prompt_tokens": 10,
@@ -40,16 +40,79 @@ class NormalizedUsageTests(unittest.TestCase):
40
40
  )
41
41
  self.assertEqual(
42
42
  usage,
43
- {"prompt_tokens": 10, "completion_tokens": 2, "total_tokens": 12},
43
+ {
44
+ "prompt_tokens": 10,
45
+ "completion_tokens": 2,
46
+ "total_tokens": 12,
47
+ "cache_read_tokens": 4,
48
+ },
44
49
  )
45
50
 
46
- def test_reads_attribute_style_payloads(self) -> None:
51
+ def test_cache_read_aliases_are_supported(self) -> None:
52
+ for field in (
53
+ "cache_read_input_tokens",
54
+ "cached_content_token_count",
55
+ "cache_tokens",
56
+ "prompt_cache_hit_tokens",
57
+ ):
58
+ with self.subTest(field=field):
59
+ usage = _normalized_usage(
60
+ {
61
+ "prompt_tokens": 10,
62
+ "completion_tokens": 2,
63
+ field: 4,
64
+ }
65
+ )
66
+ self.assertEqual(usage["cache_read_tokens"], 4)
67
+
68
+ def test_first_valid_cache_read_alias_wins_and_zero_is_preserved(self) -> None:
47
69
  usage = _normalized_usage(
48
- SimpleNamespace(prompt_tokens=7, completion_tokens=3, total_tokens=10)
70
+ {
71
+ "prompt_tokens": 10,
72
+ "completion_tokens": 2,
73
+ "prompt_tokens_details": {"cached_tokens": 0},
74
+ "cache_read_input_tokens": 7,
75
+ "cache_tokens": 9,
76
+ }
77
+ )
78
+ self.assertEqual(usage["cache_read_tokens"], 0)
79
+
80
+ def test_invalid_cache_read_aliases_are_skipped(self) -> None:
81
+ usage = _normalized_usage(
82
+ {
83
+ "prompt_tokens": 10,
84
+ "completion_tokens": 2,
85
+ "prompt_tokens_details": {"cached_tokens": -1},
86
+ "cache_read_input_tokens": True,
87
+ "cached_content_token_count": "8",
88
+ "cache_tokens": 3.0,
89
+ }
90
+ )
91
+ self.assertEqual(usage["cache_read_tokens"], 3)
92
+
93
+ def test_missing_cache_read_remains_absent(self) -> None:
94
+ usage = _normalized_usage(
95
+ {"prompt_tokens": 10, "completion_tokens": 2}
96
+ )
97
+ self.assertNotIn("cache_read_tokens", usage)
98
+
99
+ def test_reads_attribute_style_payloads_and_nested_cache_details(self) -> None:
100
+ usage = _normalized_usage(
101
+ SimpleNamespace(
102
+ prompt_tokens=7,
103
+ completion_tokens=3,
104
+ total_tokens=10,
105
+ prompt_tokens_details=SimpleNamespace(cached_tokens=2),
106
+ )
49
107
  )
50
108
  self.assertEqual(
51
109
  usage,
52
- {"prompt_tokens": 7, "completion_tokens": 3, "total_tokens": 10},
110
+ {
111
+ "prompt_tokens": 7,
112
+ "completion_tokens": 3,
113
+ "total_tokens": 10,
114
+ "cache_read_tokens": 2,
115
+ },
53
116
  )
54
117
 
55
118
  def test_missing_total_is_summed_from_the_parts(self) -> None:
@@ -82,6 +145,63 @@ class CombineUsageTests(unittest.TestCase):
82
145
  {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 5},
83
146
  )
84
147
 
148
+ def test_sums_only_reported_valid_cache_read_tokens(self) -> None:
149
+ combined = combine_usage(
150
+ [
151
+ {
152
+ "prompt_tokens": 10,
153
+ "completion_tokens": 2,
154
+ "total_tokens": 12,
155
+ "cache_read_tokens": 4,
156
+ },
157
+ {
158
+ "prompt_tokens": 5,
159
+ "completion_tokens": 1,
160
+ "total_tokens": 6,
161
+ },
162
+ {
163
+ "prompt_tokens": 3,
164
+ "completion_tokens": 1,
165
+ "total_tokens": 4,
166
+ "cache_read_tokens": -2,
167
+ },
168
+ {
169
+ "prompt_tokens": 2,
170
+ "completion_tokens": 1,
171
+ "total_tokens": 3,
172
+ "cache_read_tokens": True,
173
+ },
174
+ ]
175
+ )
176
+ self.assertEqual(
177
+ combined,
178
+ {
179
+ "prompt_tokens": 20,
180
+ "completion_tokens": 5,
181
+ "total_tokens": 25,
182
+ "cache_read_tokens": 4,
183
+ },
184
+ )
185
+
186
+ def test_cache_read_stays_absent_when_no_call_reports_it(self) -> None:
187
+ combined = combine_usage(
188
+ [{"prompt_tokens": 5, "completion_tokens": 1, "total_tokens": 6}]
189
+ )
190
+ self.assertNotIn("cache_read_tokens", combined)
191
+
192
+ def test_reported_zero_cache_read_is_preserved(self) -> None:
193
+ combined = combine_usage(
194
+ [
195
+ {
196
+ "prompt_tokens": 5,
197
+ "completion_tokens": 1,
198
+ "total_tokens": 6,
199
+ "cache_read_tokens": 0,
200
+ }
201
+ ]
202
+ )
203
+ self.assertEqual(combined["cache_read_tokens"], 0)
204
+
85
205
 
86
206
  class ResultUsageAggregationTests(unittest.TestCase):
87
207
  """Run usage sums the tool-loop requests plus judge calls; case usage
@@ -97,6 +217,7 @@ class ResultUsageAggregationTests(unittest.TestCase):
97
217
  "prompt_tokens": 50,
98
218
  "completion_tokens": 10,
99
219
  "total_tokens": 60,
220
+ "cache_read_tokens": 40,
100
221
  }
101
222
  )
102
223
  ],
@@ -108,6 +229,7 @@ class ResultUsageAggregationTests(unittest.TestCase):
108
229
  "prompt_tokens": 1000,
109
230
  "completion_tokens": 200,
110
231
  "total_tokens": 1200,
232
+ "cache_read_tokens": 800,
111
233
  },
112
234
  },
113
235
  {"step_id": "greet", "request_index": 1},
@@ -120,6 +242,7 @@ class ResultUsageAggregationTests(unittest.TestCase):
120
242
  "prompt_tokens": 1050,
121
243
  "completion_tokens": 210,
122
244
  "total_tokens": 1260,
245
+ "cache_read_tokens": 840,
123
246
  }
124
247
  self.assertEqual(run.usage(), expected)
125
248
  self.assertEqual(run.to_dict()["usage"], expected)
@@ -140,6 +263,7 @@ class ResultUsageAggregationTests(unittest.TestCase):
140
263
  "prompt_tokens": 2100,
141
264
  "completion_tokens": 420,
142
265
  "total_tokens": 2520,
266
+ "cache_read_tokens": 1680,
143
267
  }
144
268
  self.assertEqual(result.usage(), expected)
145
269
  self.assertEqual(result.to_dict()["usage"], expected)
@@ -171,7 +295,12 @@ class ClientUsageCaptureTests(unittest.IsolatedAsyncioTestCase):
171
295
  )
172
296
  self.assertEqual(
173
297
  response.usage,
174
- {"prompt_tokens": 11, "completion_tokens": 4, "total_tokens": 15},
298
+ {
299
+ "prompt_tokens": 11,
300
+ "completion_tokens": 4,
301
+ "total_tokens": 15,
302
+ "cache_read_tokens": 2,
303
+ },
175
304
  )
176
305
 
177
306
  async def test_litellm_response_without_usage_yields_none(self) -> None:
@@ -239,13 +239,32 @@ def _value(item: Any, key: str, default: Any = None) -> Any:
239
239
  return getattr(item, key, default)
240
240
 
241
241
 
242
+ def _cache_read_tokens(value: Any) -> int | None:
243
+ prompt_details = _value(value, "prompt_tokens_details")
244
+ candidates = [
245
+ _value(prompt_details, "cached_tokens"),
246
+ _value(value, "cache_read_input_tokens"),
247
+ _value(value, "cached_content_token_count"),
248
+ _value(value, "cache_tokens"),
249
+ _value(value, "prompt_cache_hit_tokens"),
250
+ ]
251
+ for candidate in candidates:
252
+ if (
253
+ isinstance(candidate, (int, float))
254
+ and not isinstance(candidate, bool)
255
+ and candidate >= 0
256
+ ):
257
+ return int(candidate)
258
+ return None
259
+
260
+
242
261
  def _normalized_usage(value: Any) -> dict[str, int] | None:
243
262
  """Reduce a provider usage payload to plain int token counters.
244
263
 
245
- Providers attach extra detail blocks (`prompt_tokens_details`, cache
246
- counters, ...) with shapes that vary per provider; only the three
247
- OpenAI-style counters are kept so downstream aggregation can sum
248
- them blindly. Returns None when the payload carries none of them.
264
+ The three OpenAI-style counters keep their existing semantics. A
265
+ provider-reported cache-read counter is retained under the optional
266
+ `cache_read_tokens` key; it is never added to prompt or total tokens.
267
+ Returns None when the payload carries none of the core counters.
249
268
  """
250
269
  if value is None:
251
270
  return None
@@ -260,6 +279,9 @@ def _normalized_usage(value: Any) -> dict[str, int] | None:
260
279
  "total_tokens",
261
280
  usage.get("prompt_tokens", 0) + usage.get("completion_tokens", 0),
262
281
  )
282
+ cache_read_tokens = _cache_read_tokens(value)
283
+ if cache_read_tokens is not None:
284
+ usage["cache_read_tokens"] = cache_read_tokens
263
285
  return usage
264
286
 
265
287
 
@@ -46,6 +46,24 @@ def _usage_inline(usage: Any) -> str:
46
46
  )
47
47
 
48
48
 
49
+ def _cache_read_percentage(usage: Any) -> str:
50
+ if not isinstance(usage, dict):
51
+ return ""
52
+ prompt_tokens = usage.get("prompt_tokens")
53
+ cache_read_tokens = usage.get("cache_read_tokens")
54
+ if (
55
+ isinstance(prompt_tokens, bool)
56
+ or not isinstance(prompt_tokens, (int, float))
57
+ or prompt_tokens <= 0
58
+ or isinstance(cache_read_tokens, bool)
59
+ or not isinstance(cache_read_tokens, (int, float))
60
+ or cache_read_tokens < 0
61
+ or cache_read_tokens > prompt_tokens
62
+ ):
63
+ return ""
64
+ return f"{cache_read_tokens / prompt_tokens:.2%}"
65
+
66
+
49
67
  def _duration_inline(duration_ms: Any) -> str:
50
68
  if isinstance(duration_ms, bool) or not isinstance(duration_ms, (int, float)):
51
69
  return ""
@@ -884,19 +902,35 @@ def _run_detail_cells(run: RunResult) -> str:
884
902
  overall = _run_judge_overall(run)
885
903
  judge_cell = f"{overall:.0%}" if overall is not None else "–"
886
904
  usage = run.usage()
887
- tokens_cell = f"{usage['total_tokens']:,}" if usage else "–"
888
905
  error_note = ' <span class="muted">(run error)</span>' if run.error else ""
889
906
  return (
890
907
  f"<td>{steps_passed}/{len(run.steps)}{error_note}</td>"
891
908
  f"<td>{checks_passed}/{len(checks)}</td>"
892
909
  f"<td>{_text(judge_cell)}</td>"
893
- f"<td>{_text(tokens_cell)}</td>"
910
+ f"{_usage_detail_cells(usage)}"
911
+ )
912
+
913
+
914
+ def _usage_detail_cells(usage: dict[str, int] | None) -> str:
915
+ if usage:
916
+ total_cell = f"{usage.get('total_tokens', 0):,}"
917
+ input_cell = f"{usage.get('prompt_tokens', 0):,}"
918
+ output_cell = f"{usage.get('completion_tokens', 0):,}"
919
+ cache_read_cell = _cache_read_percentage(usage) or "–"
920
+ else:
921
+ total_cell = input_cell = output_cell = cache_read_cell = "–"
922
+ return (
923
+ f"<td>{_text(total_cell)}</td>"
924
+ f"<td>{_text(input_cell)}</td>"
925
+ f"<td>{_text(output_cell)}</td>"
926
+ f"<td>{_text(cache_read_cell)}</td>"
894
927
  )
895
928
 
896
929
 
897
930
  _RUN_DETAIL_HEADERS = (
898
931
  "<th>Steps passed</th><th>Hard checks</th><th>Judge overall</th>"
899
- "<th>Tokens</th>"
932
+ "<th>Total tokens</th><th>Input tokens</th>"
933
+ "<th>Output tokens</th><th>Cache read</th>"
900
934
  )
901
935
 
902
936
 
@@ -950,7 +984,6 @@ def _render_variant_comparison(groups: dict[str, list[RunResult]]) -> str:
950
984
  ]
951
985
  judge_cell = f"{sum(overalls) / len(overalls):.0%}" if overalls else "–"
952
986
  usage = combine_usage(run.usage() for run in runs)
953
- tokens_cell = f"{usage['total_tokens']:,}" if usage else "–"
954
987
  error_note = f' <span class="muted">(+{errors} error)</span>' if errors else ""
955
988
  pass_rate_cell = f"{passed / len(evaluated):.0%}" if evaluated else "–"
956
989
  rows.append(
@@ -960,14 +993,15 @@ def _render_variant_comparison(groups: dict[str, list[RunResult]]) -> str:
960
993
  f"<td>{passed}/{len(evaluated)}{error_note}</td>"
961
994
  f"<td>{_text(pass_rate_cell)}</td>"
962
995
  f"<td>{_text(judge_cell)}</td>"
963
- f"<td>{_text(tokens_cell)}</td>"
996
+ f"{_usage_detail_cells(usage)}"
964
997
  "</tr>"
965
998
  )
966
999
  return (
967
1000
  '<div class="table-wrap"><table class="variant-compare">'
968
1001
  "<thead><tr><th>Status</th><th>Variant</th>"
969
1002
  "<th>Runs passed</th><th>Pass rate</th><th>Judge overall</th>"
970
- "<th>Tokens</th></tr></thead>"
1003
+ "<th>Total tokens</th><th>Input tokens</th>"
1004
+ "<th>Output tokens</th><th>Cache read</th></tr></thead>"
971
1005
  f'<tbody>{"".join(rows)}</tbody></table></div>'
972
1006
  )
973
1007
 
@@ -1089,6 +1123,9 @@ def _render_run(run: RunResult) -> str:
1089
1123
  meta_parts.append(f"LLM request time {_duration_inline(run_duration)}")
1090
1124
  if run_usage:
1091
1125
  meta_parts.append(_usage_inline(run_usage))
1126
+ cache_read_percentage = _cache_read_percentage(run_usage)
1127
+ if cache_read_percentage:
1128
+ meta_parts.append(f"cache read {cache_read_percentage}")
1092
1129
  if run.artifact_dir:
1093
1130
  meta_parts.append(f"Artifacts: {run.artifact_dir}")
1094
1131
  meta = (
@@ -1355,6 +1392,7 @@ def _render_case(
1355
1392
  if case_usage
1356
1393
  else "–"
1357
1394
  )
1395
+ cache_read_cell = _cache_read_percentage(case_usage) or "–"
1358
1396
  case_duration = result.request_duration_ms()
1359
1397
  duration_cell = _duration_inline(case_duration) if case_duration is not None else "–"
1360
1398
  # The description is the human-meaningful headline; the case_key only
@@ -1379,6 +1417,7 @@ def _render_case(
1379
1417
  f'<div><span>Tools</span><strong>{_text(tools)}</strong></div>'
1380
1418
  f'<div><span>Skills</span><strong>{_text(skills)}</strong></div>'
1381
1419
  f'<div><span>LLM tokens</span><strong>{_text(usage_cell)}</strong></div>'
1420
+ f'<div><span>Cache read</span><strong>{_text(cache_read_cell)}</strong></div>'
1382
1421
  f'<div><span>LLM request time</span><strong>{_text(duration_cell)}</strong></div>'
1383
1422
  f'<div><span>Updated at (Beijing time)</span><strong>{_text(updated_at_beijing or "-")}</strong></div>'
1384
1423
  "</div>"
@@ -126,9 +126,13 @@ def combine_usage(items: Iterable[Any]) -> dict[str, int] | None:
126
126
  Non-dict items (None from runs whose provider reported no usage) are
127
127
  skipped, and the sum is None when nothing carried usage at all --
128
128
  "no data" must stay distinguishable from "0 tokens" so scripted or
129
- errored runs don't render as a misleading all-zero consumption.
129
+ errored runs don't render as a misleading all-zero consumption. The
130
+ optional cache-read counter is summed only when providers reported it;
131
+ its absence stays distinguishable from a reported zero.
130
132
  """
131
133
  total: dict[str, int] | None = None
134
+ cache_read_total = 0
135
+ cache_read_seen = False
132
136
  for item in items:
133
137
  if not isinstance(item, dict):
134
138
  continue
@@ -138,6 +142,16 @@ def combine_usage(items: Iterable[Any]) -> dict[str, int] | None:
138
142
  value = item.get(key)
139
143
  if isinstance(value, (int, float)) and not isinstance(value, bool):
140
144
  total[key] += int(value)
145
+ cache_read = item.get("cache_read_tokens")
146
+ if (
147
+ isinstance(cache_read, (int, float))
148
+ and not isinstance(cache_read, bool)
149
+ and cache_read >= 0
150
+ ):
151
+ cache_read_seen = True
152
+ cache_read_total += int(cache_read)
153
+ if total is not None and cache_read_seen:
154
+ total["cache_read_tokens"] = cache_read_total
141
155
  return total
142
156
 
143
157
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes