feat(metering): swap an extra target with an included one (t72) #932

Merged
sharang merged 1 commits from feat/t72-swap into main 2026-11-07 03:11:31 +00:00
Owner

Part (c), item 3 of t72: customers choose which targets are included. Built to the user's decision, option (b).

Rule

  • When: a customer can swap an extra target with an included one at any time, and the swap takes effect immediately.
  • Charge: the number of extra targets, and so the 5,99 € line, stays the same.
  • Lock: the target swapped in stays included until the end of its calendar month (Europe/Berlin), so it can't be swapped out again that month. The target swapped out becomes extra at once.

What changes

  • Core:
    • IncludedChoice (included / extra) and check_swap, with SwapRefusal values Unlimited, NotExtra, NotIncluded and Locked.
    • split_included now ranks swapped-in targets first, other targets by age, and swapped-out targets last. That keeps a swap in place while targets are added or archived.
  • Target model: included_choice, plus included_until (the start of the next Berlin month) on the target swapped in.
  • Agent:
    • metering::swap and end_of_berlin_month.
    • POST /api/v1/targets/{id}/billing/swap {"with": "<included target id>"}.
      • It returns 409 with swap_locked / swap_not_extra / swap_not_included / swap_unlimited, 404 for a target that isn't one of the tenant's active targets, and 503 when the plan can't be read.
      • It re-runs the billing split right away.
      • It's a write, so the role gate keeps read-only roles out, and API keys can't reach it.
  • Dashboard: an extra target's page shows its badge. For users who can write, it also shows the swap, with a choice of the included targets; a refusal is explained in plain words.

Tests

  • Core: a swap moves one target in and one out with the same extra count; the lock and the other refusals.
  • Agent: the Berlin month end, including December into January and summer time.
  • Mongo (tests/target_billing.rs):
    • a swap;
    • the swapped-in target is locked that month;
    • another included target can still go out;
    • the lock ends in December;
    • an unknown target returns 404.
  • Dashboard: which targets can be swapped out.

Not built locally, per the CI-only rule. This may need a rebase after #930 (both add a handler next to the scan endpoint).

🤖 Generated with Claude Code

Part (c), item 3 of t72: customers choose which targets are included. Built to the user's decision, option (b). ## Rule - **When:** a customer can swap an extra target with an included one at any time, and the swap takes effect immediately. - **Charge:** the number of extra targets, and so the 5,99 € line, stays the same. - **Lock:** the target swapped in stays included until the end of its calendar month (Europe/Berlin), so it can't be swapped out again that month. The target swapped out becomes extra at once. ## What changes - **Core:** - `IncludedChoice` (included / extra) and `check_swap`, with `SwapRefusal` values `Unlimited`, `NotExtra`, `NotIncluded` and `Locked`. - `split_included` now ranks swapped-in targets first, other targets by age, and swapped-out targets last. That keeps a swap in place while targets are added or archived. - **Target model:** `included_choice`, plus `included_until` (the start of the next Berlin month) on the target swapped in. - **Agent:** - `metering::swap` and `end_of_berlin_month`. - `POST /api/v1/targets/{id}/billing/swap` `{"with": "<included target id>"}`. - It returns 409 with `swap_locked` / `swap_not_extra` / `swap_not_included` / `swap_unlimited`, 404 for a target that isn't one of the tenant's active targets, and 503 when the plan can't be read. - It re-runs the billing split right away. - It's a write, so the role gate keeps read-only roles out, and API keys can't reach it. - **Dashboard:** an extra target's page shows its badge. For users who can write, it also shows the swap, with a choice of the included targets; a refusal is explained in plain words. ## Tests - **Core:** a swap moves one target in and one out with the same extra count; the lock and the other refusals. - **Agent:** the Berlin month end, including December into January and summer time. - **Mongo (`tests/target_billing.rs`):** - a swap; - the swapped-in target is locked that month; - another included target can still go out; - the lock ends in December; - an unknown target returns 404. - **Dashboard:** which targets can be swapped out. Not built locally, per the CI-only rule. This may need a rebase after #930 (both add a handler next to the scan endpoint). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang force-pushed feat/t72-swap from e66585dcf2 to 5cebca1ff0 2026-11-06 17:03:46 +00:00 Compare
sharang added 1 commit 2026-11-07 02:51:26 +00:00
feat(metering): swap an extra target with an included one (t72)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 3m49s
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 19m37s
42414b2bad
Decided by the user (option b): a customer can swap an extra target
with an included one at any time, effective at once; the extra-target
count, and so the 5,99 € line, stays the same. The target swapped in
stays included until the end of its calendar month (Europe/Berlin), so
it cannot be swapped out again that month; the one swapped out becomes
extra at once.

- A target stores the customer's choice (`included_choice`) and, when
  swapped in, `included_until` (the next Berlin month's start). The
  split ranks swapped-in targets before the oldest and swapped-out ones
  after the newest, so the swap holds while targets come and go.
- POST /api/v1/targets/{id}/billing/swap {"with": id}: 409 with the
  reason (locked, not extra, not included, plan includes all), 404 for
  a target that is not an active one of the tenant. A write: the role
  gate keeps read-only roles and API keys out.
- An extra target's page shows its badge and, for those who may write,
  the swap with a choice of the included targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang force-pushed feat/t72-swap from 5cebca1ff0 to 42414b2bad 2026-11-07 02:51:26 +00:00 Compare
sharang merged commit a2699a3ec3 into main 2026-11-07 03:11:31 +00:00
Sign in to join this conversation.