Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix(evals): ignore negative replay usage counts (#284)
Stamina9e47a09736d4f
diff --git a/docs/research/progressive-disclosure-evals.md b/docs/research/progressive-disclosure-evals.mdindex f6d253b..4622bc2 100644--- a/docs/research/progressive-disclosure-evals.md+++ b/docs/research/progressive-disclosure-evals.md@@ -550,7 +550,7 @@ Agents update this table when a task changes status. Keep entries short; link to|---|---|---|---|---|| PD-00 | DONE | — | — | Baseline documented || PD-01 | DONE | — | `tests/evals/test_manifest.py` | Deterministic manifest, source SHA-256, budget/secret validation |-| PD-02 | DONE | — | `tests/evals/test_score.py`, `test_replay.py` | Offline trajectory scorer and replay runner |+| PD-02 | DONE | — | `tests/evals/test_score.py`, `test_replay.py`, `test_score_robustness.py` | Offline trajectory scorer and replay runner; negative usage accounting regression (#283) || PD-03 | DONE | — | `tests/evals/test_paper_flat.py` | Synthetic paper-flat baseline builder || PD-04 | BLOCKED | — | — | waits for PD-02/03 || PD-05 | BLOCKED | — | — | primary representation experiment |diff --git a/tests/evals/test_replay.py b/tests/evals/test_replay.pyindex 3c5d4c7..c9fa643 100644--- a/tests/evals/test_replay.py+++ b/tests/evals/test_replay.py@@ -37,3 +37,38 @@ def test_cli_output_is_byte_identical_on_repeat():second = subprocess.run(command, check=True, capture_output=True).stdoutassert first == secondassert json.loads(first)["fixture_version"] == "1"+++def test_cli_excludes_negative_usage_without_changing_classification(tmp_path):+ def trajectory(question_id, usage):+ return {+ "question_id": question_id,+ "expected": {"target": "book/chapter"},+ "observed": {+ "opens": ["book/chapter"], "answer_correct": True, "usage": usage,+ },+ }++ fixture = tmp_path / "negative_usage.json"+ fixture.write_text(json.dumps({+ "schema_version": replay.SCHEMA_VERSION,+ "fixture_version": "negative-usage",+ "trajectories": [+ trajectory("positive", {"input_tokens": 100, "output_tokens": 20, "calls": 2}),+ trajectory("negative", {"input_tokens": -100, "output_tokens": -20, "calls": -2}),+ ],+ }), encoding="utf-8")++ completed = subprocess.run(+ [sys.executable, str(REPLAY), str(fixture)],+ check=True, capture_output=True, text=True,+ )+ report = json.loads(completed.stdout)++ assert report["aggregate"]["recorded_usage"] == {+ "input_tokens": 100, "output_tokens": 20, "calls": 2,+ }+ assert report["aggregate"]["usage_observations"] == {+ "input_tokens": 1, "output_tokens": 1, "calls": 1,+ }+ assert [item["classification"] for item in report["questions"]] == ["correct", "correct"]diff --git a/tests/evals/test_score_robustness.py b/tests/evals/test_score_robustness.pyindex d47a74a..80e829e 100644--- a/tests/evals/test_score_robustness.py+++ b/tests/evals/test_score_robustness.py@@ -183,3 +183,62 @@ class TestUsageCountsRejectBooleans:assert result["usage"]["calls"] == UNKNOWNassert result["usage"]["input_tokens"] == UNKNOWN+++class TestUsageCountsRejectNegatives:+ @pytest.mark.parametrize("field", ["input_tokens", "output_tokens", "calls"])+ def test_negative_field_is_unknown_independently(self, field):+ usage = {"input_tokens": 100, "output_tokens": 20, "calls": 2}+ usage[field] = -1++ result = score_trajectory(+ _trajectory("q", "a", ["a"], answer_correct=True, usage=usage)+ )++ assert result["usage"][field] == UNKNOWN+ assert all(result["usage"][key] == value for key, value in usage.items()+ if key != field)+ assert result["classification"] == "correct"++ def test_negative_only_has_no_usage_observations(self):+ report = score([+ _trajectory("q", "a", ["a"], answer_correct=True,+ usage={"input_tokens": -100, "output_tokens": -20, "calls": -2})+ ])++ assert report["questions"][0]["usage"] == {+ "input_tokens": UNKNOWN, "output_tokens": UNKNOWN, "calls": UNKNOWN,+ }+ assert report["aggregate"]["recorded_usage"] == {+ "input_tokens": 0, "output_tokens": 0, "calls": 0,+ }+ assert report["aggregate"]["usage_observations"] == {+ "input_tokens": 0, "output_tokens": 0, "calls": 0,+ }++ def test_negative_does_not_cancel_positive_and_zero_is_observed(self):+ report = score([+ _trajectory("positive", "a", ["a"], answer_correct=True,+ usage={"input_tokens": 100, "output_tokens": 20, "calls": 2}),+ _trajectory("negative", "a", ["a"], answer_correct=True,+ usage={"input_tokens": -100, "output_tokens": -20, "calls": -2}),+ _trajectory("zero", "a", ["a"], answer_correct=True,+ usage={"input_tokens": 0, "output_tokens": 0, "calls": 0}),+ ])++ assert report["aggregate"]["recorded_usage"] == {+ "input_tokens": 100, "output_tokens": 20, "calls": 2,+ }+ assert report["aggregate"]["usage_observations"] == {+ "input_tokens": 2, "output_tokens": 2, "calls": 2,+ }+ assert report["aggregate"]["classifications"] == {"correct": 3}++ @pytest.mark.parametrize("value", [None, True, False, "3", 1.5])+ def test_other_invalid_values_remain_unknown(self, value):+ result = score_trajectory(+ _trajectory("q", "a", ["a"], answer_correct=True,+ usage={"calls": value})+ )++ assert result["usage"]["calls"] == UNKNOWNdiff --git a/tools/evals/score.py b/tools/evals/score.pyindex 95c414a..4889835 100644--- a/tools/evals/score.py+++ b/tools/evals/score.py@@ -13,13 +13,14 @@ def _state(value: Any) -> Any:def _count(value: Any) -> Any:- """Keep only explicit integer counts; absence remains unknown.+ """Keep only explicit non-negative integer counts; absence remains unknown.``bool`` is a subclass of ``int``, so a JSON ``true`` would otherwise pass an``isinstance(value, int)`` test and then be summed as 1 -- inventing a usage- number this module promises never to estimate.+ number this module promises never to estimate. Negative sentinels likewise+ cannot represent recorded token or call counts."""- return value if isinstance(value, int) and not isinstance(value, bool) else UNKNOWN+ return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else UNKNOWNdef score_trajectory(trajectory: Dict[str, Any]) -> Dict[str, Any]: