From 0784f180625454a8cacabf973010cb12922bd1c0 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 09:18:02 -0600 Subject: [PATCH] fix(quota-plane): canonicalise fee_period vocabulary in reconcile.py diff (#13375) The plan-store leg's schema admits monthly/yearly/once while the tracker's approved plan-facts schema uses month/year, so the raw-string comparison reported 9 permanent fee_period findings that were pure vocabulary noise. Canonicalise fee_period to the tracker vocabulary for comparison only; findings keep each leg's raw value and winner. Fixture tests: the store fixture now carries the schema-valid "monthly" (the old "month" value is one the real store schema rejects and masked the bug), plus a planted genuine period mismatch (monthly vs year, caught with raw values reported) and a yearly/year agreement case. --- pm-stack/provider-usage/reconcile.py | 19 ++++++++- pm-stack/provider-usage/test_reconcile.py | 52 ++++++++++++++++++++++- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/pm-stack/provider-usage/reconcile.py b/pm-stack/provider-usage/reconcile.py index 78163d07ff..a9f7d84b97 100644 --- a/pm-stack/provider-usage/reconcile.py +++ b/pm-stack/provider-usage/reconcile.py @@ -19,6 +19,12 @@ edits any source: corrections flow through normal edit/PR paths with the diff attached. +fee_period vocabularies differ by leg: the plan store's schema admits +monthly/yearly/once, the tracker's approved plan-facts schema uses +month/year. The diff canonicalises fee_period to the tracker vocabulary +for comparison only (see PERIOD_ALIASES); findings always report each +leg's raw value. + Exit codes: 0 = ran (findings or no-drift printed); 2 = missing plan store (failure finding); 3 = unreadable input. """ @@ -28,6 +34,17 @@ FIELDS = ("fee_amount", "fee_period", "window", "limit", "reset", "status") +# Store-leg fee_period values mapped to the tracker vocabulary. `once` +# has no tracker equivalent and passes through unchanged, so a one-off +# fee still mismatches any recurring tracker period. +PERIOD_ALIASES = {"monthly": "month", "yearly": "year"} + + +def _canon(field, value): + if field == "fee_period" and isinstance(value, str): + return PERIOD_ALIASES.get(value, value) + return value + def load_store(path): p = Path(path) @@ -106,7 +123,7 @@ def diff(store, tracker, measured): for n, r in (("measured", m), ("plan-store", s), ("tracker", t)) if r and r.get(f) is not None } - if len(set(map(str, vals.values()))) > 1: + if len({str(_canon(f, v)) for v in vals.values()}) > 1: winner = next( n for n in ("measured", "plan-store", "tracker") if n in vals ) diff --git a/pm-stack/provider-usage/test_reconcile.py b/pm-stack/provider-usage/test_reconcile.py index 8ebafbc877..e5552b3cd8 100644 --- a/pm-stack/provider-usage/test_reconcile.py +++ b/pm-stack/provider-usage/test_reconcile.py @@ -44,7 +44,7 @@ def tracker(rows): "id": "go-personal", "providerID": "opencode-go", "plan": "go", - "fee": {"amount": 10, "period": "month"}, + "fee": {"amount": 10, "period": "monthly"}, "quotas": [ { "name": "monthly", @@ -72,7 +72,9 @@ def tracker(rows): ] ) -# 1. agreeing legs -> no-drift +# 1. agreeing legs -> no-drift. Also the vocabulary case (#13375): the +# store fixture carries the schema-valid "monthly" while the tracker +# carries "month" — same fact, no fee_period finding. r = run(STORE, TRACK_OK, {"providers": {}}) out = json.loads(r.stdout) assert r.returncode == 0 and out["result"] == "no-drift", out @@ -108,4 +110,50 @@ def tracker(rows): out = json.loads(r.stdout) assert any(x["field"] == "presence" for x in out["findings"]), out +# 5. PLANTED PERIOD MISMATCH (#13375): store "monthly" vs tracker "year" +# is a genuine period disagreement -> caught, store wins, and the finding +# reports each leg's RAW value (not the canonicalised form). +bad_period = tracker( + [ + { + "plan": "go-personal", + "provider": "opencode-go", + "fee_amount": 10, + "fee_period": "year", + "window": 720, + "limit": 100, + "reset": "calendar", + "status": "active", + } + ] +) +r = run(STORE, bad_period, {"providers": {}}) +out = json.loads(r.stdout) +assert out["result"] == "findings" and len(out["findings"]) == 1, out +f = out["findings"][0] +assert f["field"] == "fee_period" and f["winner"] == "plan-store", f +assert f["values"] == {"plan-store": "monthly", "tracker": "year"}, f + +# 6. yearly vocabulary agreement (#13375): store "yearly" vs tracker +# "year" -> no-drift. +STORE_YEARLY = json.loads(json.dumps(STORE)) +STORE_YEARLY["plans"][0]["fee"]["period"] = "yearly" +track_year = tracker( + [ + { + "plan": "go-personal", + "provider": "opencode-go", + "fee_amount": 10, + "fee_period": "year", + "window": 720, + "limit": 100, + "reset": "calendar", + "status": "active", + } + ] +) +r = run(STORE_YEARLY, track_year, {"providers": {}}) +out = json.loads(r.stdout) +assert r.returncode == 0 and out["result"] == "no-drift", out + print("all reconcile fixture tests pass")