Skip to content

spec(admin): v0.1.25.26 — bulk-action budget endpoint - #55

Merged
amavashev merged 2 commits into
mainfrom
spec/admin-0.1.25.26-bulk-action-budgets
Apr 18, 2026
Merged

amavashev merged 2 commits into
mainfrom
spec/admin-0.1.25.26-bulk-action-budgets

Conversation

@amavashev

@amavashev amavashev commented Apr 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add POST /v1/admin/budgets/bulk-action to resolve cycles-server-admin#99 — operators rolling over a billing period can now issue one filtered bulk request instead of iterating listBudgets + per-row fundBudget.

Single operation-discriminated endpoint following the existing tenant/webhook bulk-action pattern. The action enum (CREDIT | DEBIT | RESET | RESET_SPENT | REPAY_DEBT) mirrors BudgetFundingRequest.operation so any per-row fundBudget mutation is also expressible in bulk; the action determines which payload fields are required.

New schemas (all additive)

  • BudgetBulkFilter — mirrors listBudgets query params (scope_prefix, unit, status, over_limit, has_debt, utilization_min/max, search). tenant_id is required (cross-tenant safety: no all-tenant fan-out from a single bulk call). Cascading reset across a scope subtree uses scope_prefix — the hierarchy is encoded in the scope path; no separate cascade flag.
  • BudgetBulkActionRequest — filter + action + amount (required for all current actions) + optional spent (RESET_SPENT only) + optional reason for audit trail + expected_count safety gate + idempotency_key.
  • BudgetBulkActionResponse — per-row outcome envelope reusing BulkActionRowOutcome (succeeded[] / failed[] / skipped[]).

Safety semantics (mirrors tenant/webhook bulk-action)

  • 500-row hard cap → HTTP 400 LIMIT_EXCEEDED
  • expected_count preflight → HTTP 409 COUNT_MISMATCH with no writes on drift
  • 15-minute idempotency_key replay window
  • HTTP 200 even when rows fail; per-row codes BUDGET_EXCEEDED (DEBIT would take remaining negative) and INVALID_TRANSITION (unit mismatch, ledger CLOSED) cover the common cases
  • AdminKeyAuth only — tenants cannot bulk-mutate their own budgets via this endpoint; per-budget mutation remains via fundBudget

Audit log

One AuditLogEntry per invocation (not per row), actor_type=ADMIN_ON_BEHALF_OF, embedding the full per-row outcome so security review can reconstruct exactly what changed without replaying the bulk request.

Why one endpoint, not five

The earlier issue thread proposed a unified bulk-ops surface (vs. a dedicated bulk-reset). Rationale, locked in here: one auth/audit/idempotency contract, the same envelope works for future asks ("credit all budgets in scope X", "repay debt across a subtree"), and matches the tenant/webhook bulk-action pattern already shipped in cycles-server-admin v0.1.25.26 (PR #109 there). Tradeoff acknowledged: slightly more validation surface (action-discriminated payload), mitigated by the action enum + per-action required-field rules called out in the schema descriptions.

Why scope_prefix, not a cascade flag

The scope hierarchy is already encoded in the canonical scope string (e.g., tenant:acme/workspace:eng). A prefix match on tenant:acme/workspace:eng matches that scope and all descendants — that is the cascade. Adding a cascade: bool flag would be redundant and would invite the question "cascade from what root?" that scope_prefix already answers unambiguously.

Why not async for 10k+ budgets

The existing 500-row hard cap (carried over from tenant/webhook bulk-action) sidesteps this entirely. Operators with > 500 budgets in a rollover batch partition by scope_prefix (the natural partition for cascading resets) and issue multiple bulk requests. No async job runner needed in the spec; if 10k+ tenant scenarios become real, async can be added later as a separate *_async shape without breaking the sync contract.

Backward compatibility

Purely additive — one new path, three new schemas. No existing endpoint or schema changes. No ErrorCode enum additions (BUDGET_EXCEEDED, INVALID_TRANSITION, COUNT_MISMATCH, LIMIT_EXCEEDED already present from v0.1.25.23). semantic_base unchanged at 0.1.25.9.

Test plan

  • python scripts/merge_specs.py — admin merged regenerated; merge idempotent (verified via md5 snapshot)
  • npx spectral lint cycles-governance-admin-v0.1.25.yaml — 0 errors (warnings only on pre-existing schemas, none in the new ones)
  • npx spectral lint merged/cycles-openapi-admin-merged.yaml — 0 errors
  • python scripts/validate_changelogs.py — passes; info.version=0.1.25.26 matches latest changelog heading
  • All 3 new schemas (BudgetBulkFilter, BudgetBulkActionRequest, BudgetBulkActionResponse) and the new path appear in the merged admin artifact
  • CI passes
  • Server-side implementation in cycles-server-admin can clone the existing bulkActionTenants / bulkActionWebhooks controller pattern (per the comment from amavashev on issue build(deps): bump github/codeql-action from 4.35.5 to 4.36.0 #99)

Add `POST /v1/admin/budgets/bulk-action` to resolve cycles-server-admin
issue #99 ("Bulk Budget Reset at Tenant or Parent-Scope Level").
Operators rolling over a billing period can now issue one filtered
bulk request instead of iterating listBudgets + per-row fundBudget.

Single operation-discriminated endpoint following the existing
tenant/webhook bulk-action pattern. The `action` enum
(`CREDIT | DEBIT | RESET | RESET_SPENT | REPAY_DEBT`) mirrors
BudgetFundingRequest.operation so any per-row fundBudget mutation is
also expressible in bulk; the action determines which payload fields
are required.

New schemas (all additive):
- `BudgetBulkFilter` — mirrors listBudgets query params; tenant_id
  REQUIRED for cross-tenant safety. Cascading reset across a scope
  subtree uses `scope_prefix` (no separate cascade flag).
- `BudgetBulkActionRequest` — `filter` + `action` + `amount`
  (required for all current actions) + optional `spent`
  (RESET_SPENT only) + optional `reason` + `expected_count` +
  `idempotency_key`.
- `BudgetBulkActionResponse` — per-row outcome envelope reusing
  `BulkActionRowOutcome` (succeeded[] / failed[] / skipped[]).

Safety semantics match the existing tenant/webhook bulk-action
contract: 500-row hard cap (HTTP 400 LIMIT_EXCEEDED), expected_count
preflight (HTTP 409 COUNT_MISMATCH with no writes on drift), 15-min
idempotency_key replay window. HTTP 200 even when rows fail; per-row
codes BUDGET_EXCEEDED and INVALID_TRANSITION cover the common cases.
AdminKeyAuth only.

semantic_base unchanged at 0.1.25.9. Backward compatible (purely
additive: one new path, three new schemas; no enum or existing
schema changes). Merged admin artifact regenerated.

Refs: runcycles/cycles-server-admin#99
…es, filter consistency

Review-round fixes on `POST /v1/admin/budgets/bulk-action` (PR #55).
No semantic change to the endpoint; consistency and accuracy only.

- Action enum now truly mirrors BudgetFundingRequest.operation
  ordering: [CREDIT, DEBIT, RESET, REPAY_DEBT, RESET_SPENT]. The
  commit message on the parent claimed "mirrors BudgetFundingRequest"
  but the last two were swapped.
- Added BUDGET_EXCEEDED to BulkActionRowOutcome.error_code known-codes
  list (was: INVALID_TRANSITION, NOT_FOUND, PERMISSION_DENIED,
  INTERNAL_ERROR). Budget bulk-ops can legitimately fail a row with
  BUDGET_EXCEEDED when DEBIT would take remaining negative; clients
  need to switch on it without treating it as INTERNAL_ERROR. Also
  clarified that this code set is intentionally broader than the
  top-level ErrorCode enum — row-level vs. request-level.
- BudgetBulkFilter: added minProperties: 1 for consistency with
  TenantBulkFilter / WebhookBulkFilter. (Always vacuously satisfied
  because tenant_id is required; the schema shape now matches its
  siblings.)
- Changelog entry corrected: previously implied INVALID_TRANSITION
  was a top-level ErrorCode enum value (it's a row-level outcome).
  Rewrote the backward-compat paragraph to distinguish request-level
  ErrorCode enum additions (none) from the row-level known-codes
  update (BUDGET_EXCEEDED added).

Merged admin artifact regenerated. Idempotent. All checks pass
(spectral: 0 errors, validate_changelogs: OK).
@amavashev
amavashev merged commit d986cac into main Apr 18, 2026
1 check passed
@amavashev
amavashev deleted the spec/admin-0.1.25.26-bulk-action-budgets branch June 18, 2026 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Bulk Budget Reset at Tenant or Parent-Scope Level

1 participant