leave an odd-shaped cost.json alone instead of crashing

(cherry picked from commit 39c14aa550)
This commit is contained in:
smngvlkz authored and safishamsi committed 2026-10-25 17:25:12 +00:00
1 parent 968115c0cc
commit e0fb925378
2 files changed
+16 -3

No files matched your search

+4
View File
@@ -830,6 +830,10 @@ def _record_extract_cost(graphify_out: Path, input_tokens: int, output_tokens: i
cost.setdefault("runs", [])
cost.setdefault("total_input_tokens", 0)
cost.setdefault("total_output_tokens", 0)
if not isinstance(cost["runs"], list) or not all(
type(cost[k]) is int for k in ("total_input_tokens", "total_output_tokens")
):
raise ValueError("unexpected layout")
except (OSError, ValueError, AttributeError) as exc:
print(f"[graphify extract] warning: could not read {cost_path}, not updating it: {exc}", file=sys.stderr)
return
+12 -3
View File
@@ -91,14 +91,23 @@ def test_existing_skill_ledger_is_kept(monkeypatch, setup):
assert cost["total_output_tokens"] == 40
def test_unreadable_ledger_is_not_overwritten(monkeypatch, setup):
@pytest.mark.parametrize("bad", [
"{not json",
"[]",
"null",
'{"runs": {}}',
'{"runs": [], "total_input_tokens": "5"}',
'{"runs": [], "total_input_tokens": null}',
])
def test_unreadable_ledger_is_not_overwritten(monkeypatch, setup, bad):
corpus, out_dir = setup
cost_path = out_dir / "graphify-out" / "cost.json"
cost_path.parent.mkdir(parents=True)
cost_path.write_text("{not json", encoding="utf-8")
cost_path.write_text(bad, encoding="utf-8")
_run_extract(monkeypatch, corpus, out_dir)
assert cost_path.read_text(encoding="utf-8") == "{not json"
assert (out_dir / "graphify-out" / "graph.json").exists()
assert cost_path.read_text(encoding="utf-8") == bad
def test_code_only_run_with_no_tokens_writes_nothing(monkeypatch, tmp_path):