feat(metering): the quota, Prüftage, the daily limit and the cap (t72) #928

Merged
sharang merged 1 commits from feat/t72-counters into main 2026-11-06 14:48:08 +00:00
Owner

Second part of t72, Prüfwerk metering (LB §2 Abs. 5–6). Builds on #927 (merged); rebased onto main.

A counted Prüfung (the chargeable flag from #927) is checked against the plan when it starts, and charged when it completes.

Rules

  • Plan: read from the registry entitlement limits: monthly_pruefungen and prueftag_cap, alongside included_targets (tenant-registry #114). A missing or null key means no limit, as for Enterprise. If the registry doesn't answer, counted Prüfungen are neither checked nor charged.
  • Quota first. Once it is used up, the first counted Prüfung on a target on a calendar day (Europe/Berlin, via chrono-tz) books a Prüftag. That Prüftag allows 6 Prüfungen from then on; Prüfungen charged to the quota earlier that day don't use it up. A 7th that day does not run. Nor does one that would need a Prüftag beyond the customer's cap (a cap of 0 means no Prüftage).
  • Refused runs. The run ends without scanning and records why, using two new skip codes (prueftag_limit_reached, prueftag_cap_reached) worded in the dashboard. It leaves the commit and upload watermarks alone, so the change still gets scanned once the plan allows it. It emits no scan.completed.
  • Charging at completion:
    • A run that fails because of us (internal error, unreachable repository, interruption) is never charged, so nothing ever has to be refunded.
    • A run that fails on the customer's input (archive password missing or wrong) is charged.
    • Trade-off: if several targets' scans finish at the same moment, they can push the quota or a Prüftag over its limit by the runs that raced.
  • Counters: two collections in the tenant DB: metering_months (_id YYYY-MM: pruefungen, prueftage) and metering_days (_id target:YYYY-MM-DD: prueftag_booked, prueftag_pruefungen). Each run records what it was charged to in charge (quota / prueftag).
  • Usage event: prueftag.booked, quantity 1, idempotency key prueftag:{tenant}:{target}:{YYYY-MM-DD}. Properties: target id and day only, no names.

Tests

  • Core: the admission rules (quota first, a Prüftag of six, the cap stops new Prüftage but not a booked one, no quota means no limit).
  • Agent: Berlin month/day boundaries in winter and summer, counter reading, the registry limits, and a Mongo integration test (tests/metering_counters.rs) that runs quota → Prüftag → daily limit → cap → next Berlin day.

Not built locally, per the CI-only rule. Still to come in (c): target.extra, the customer's choice of included targets, and showing usage in the dashboard.

🤖 Generated with Claude Code

Second part of t72, Prüfwerk metering (LB §2 Abs. 5–6). Builds on #927 (merged); rebased onto main. A counted Prüfung (the `chargeable` flag from #927) is checked against the plan when it starts, and charged when it completes. ## Rules - **Plan:** read from the registry entitlement `limits`: `monthly_pruefungen` and `prueftag_cap`, alongside `included_targets` (tenant-registry #114). A missing or null key means no limit, as for Enterprise. If the registry doesn't answer, counted Prüfungen are neither checked nor charged. - **Quota first.** Once it is used up, the first counted Prüfung on a target on a calendar day (Europe/Berlin, via `chrono-tz`) books a Prüftag. That Prüftag allows 6 Prüfungen from then on; Prüfungen charged to the quota earlier that day don't use it up. A 7th that day does not run. Nor does one that would need a Prüftag beyond the customer's cap (a cap of 0 means no Prüftage). - **Refused runs.** The run ends without scanning and records why, using two new skip codes (`prueftag_limit_reached`, `prueftag_cap_reached`) worded in the dashboard. It leaves the commit and upload watermarks alone, so the change still gets scanned once the plan allows it. It emits no `scan.completed`. - **Charging at completion:** - A run that fails because of us (internal error, unreachable repository, interruption) is never charged, so nothing ever has to be refunded. - A run that fails on the customer's input (archive password missing or wrong) is charged. - Trade-off: if several targets' scans finish at the same moment, they can push the quota or a Prüftag over its limit by the runs that raced. - **Counters:** two collections in the tenant DB: `metering_months` (`_id` YYYY-MM: `pruefungen`, `prueftage`) and `metering_days` (`_id` target:YYYY-MM-DD: `prueftag_booked`, `prueftag_pruefungen`). Each run records what it was charged to in `charge` (quota / prueftag). - **Usage event:** `prueftag.booked`, quantity 1, idempotency key `prueftag:{tenant}:{target}:{YYYY-MM-DD}`. Properties: target id and day only, no names. ## Tests - **Core:** the admission rules (quota first, a Prüftag of six, the cap stops new Prüftage but not a booked one, no quota means no limit). - **Agent:** Berlin month/day boundaries in winter and summer, counter reading, the registry limits, and a Mongo integration test (`tests/metering_counters.rs`) that runs quota → Prüftag → daily limit → cap → next Berlin day. Not built locally, per the CI-only rule. Still to come in (c): `target.extra`, the customer's choice of included targets, and showing usage in the dashboard. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang changed target branch from feat/t72-attribution to main 2026-11-06 14:30:59 +00:00
sharang added 1 commit 2026-11-06 14:30:59 +00:00
feat(metering): the quota, Prüftage, the daily limit and the cap (t72)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 4m8s
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 16m57s
4fa0a1f10d
The second part of Prüfwerk metering (LB §2 Abs. 5-6): a counted
Prüfung is checked against the plan when it starts and charged when it
completes.

- The plan comes from the registry entitlement: `monthly_pruefungen`
  and `prueftag_cap` next to `included_targets`; a missing key is no
  limit (Enterprise).
- Charged to the month's quota first. Once it is used up, the first
  counted Prüfung on a target on a calendar day (Europe/Berlin) books
  a Prüftag, which allows six from then on; the next one that day does
  not run, and neither does one needing a Prüftag beyond the cap.
- A refused run ends without scanning, records why (two new skip
  codes, worded in the dashboard), and leaves the target's commit and
  upload watermarks alone, so the change is scanned once it may be.
- A run that fails because of us is never charged; one that fails on
  the customer's input (archive password) is. Charging at completion
  means nothing is ever refunded; scans finishing at the same moment
  can take the quota over by the ones that raced.
- A booked Prüftag goes to the registry as `prueftag.booked`, keyed
  prueftag:{tenant}:{target}:{day}.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang merged commit a7355ffdc8 into main 2026-11-06 14:48:08 +00:00
Sign in to join this conversation.