Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pm-stack/provider-usage/reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down
52 changes: 50 additions & 2 deletions pm-stack/provider-usage/test_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")