Skip to content

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

Closed
runcycles/cycles-protocol
#55

Description

@Prikis

Repository

cycles-server-admin

Problem or use case

Currently, the cycles protocol requires an individual reset call for every budget within a tenant. In a complex multi-scope, multi-budget system, this creates significant operational overhead. When a new billing period or execution cycle begins, a user must:

  1. Query all existing budgets under a tenant.
  2. Iterate through them to issue separate reset commands.

Proposed solution

All budgets are already connected to a tenant-level root scope. Since the hierarchy is already established, the system should ideally support a top-down command that respects these existing links.

I propose adding a Bulk Reset capability to the Admin API. This could be implemented in two ways:

Tenant-Level Reset: A single operation that resets all budgets associated with that tenant ID.

Cascading Scope Reset: An optional flag on the budget management endpoint that recursively resets all child budgets nested under that scope.

Alternatives considered

Client-Side Iteration: While budgets can be reset individually, a native recursive reset is more efficient. It reduces network overhead and updates the entire hierarchy through a single protocol action.

Scope

Protocol change (requires spec update)

Activity

  1. added
    enhancement
    on Apr 15, 2026
  2. amavashev commented on Apr 16, 2026

    @amavashev
    Collaborator

    Thanks for raising this — it's a real gap and the operational overhead gets worse fast at 1k+ budgets per tenant.

    One shape worth considering before we commit to a dedicated resetBudgets endpoint: a unified filter-spec bulk-ops endpoint that subsumes reset and the other per-budget operations we already expose (CREDIT, DEBIT, RESET, RESET_SPENT, REPAY_DEBT).

    Rough shape:

    POST /v1/admin/budgets:bulk
    {
      "filter": {
        "tenant_id": "tenant_abc",        // or
        "scope_id":  "scope_xyz",         // with optional cascade
        "cascade":   true,
        "utilization_min": 0.9,           // composable with other filters
        "...": "..."
      },
      "operation": "RESET_SPENT",
      "amount":    null,                  // operation-specific fields
      "spent":     0
    }
    

    Why unified vs. a dedicated reset endpoint:

    • Covers this use case and future asks ("credit all budgets in scope X," "repay debt across a subtree," etc.) without a new endpoint each time.
    • One place to put the authorization, rate-limit, audit-log, and idempotency contract — vs. duplicating them per operation.
    • Matches the cross-cutting "bulk ops by filter spec" pattern the dashboard is already building for tenant suspend/resume and webhook pause/resume (see the Phase 4 rate-limited bulk runner).

    Tradeoffs to call out:

    • Slightly more surface to validate on the server (operation-discriminated payload). Mitigable with a tagged-union schema in the YAML spec.
    • Cascading semantics across a scope tree still need to be specified carefully (ordering, partial-failure behavior, whether child budgets of a different tenant are ever in scope — probably not).
    • Response shape needs to handle long-running cascades gracefully: async job id + status endpoint vs. synchronous counts. For 10k-budget tenants, async is safer.

    If this direction resonates, happy to open a spec PR that pins down the YAML and the failure/ordering semantics. If you'd rather keep a dedicated budgets:reset for simplicity, that works too — just wanted to flag the reuse opportunity before we lock it in.

  3. amavashev commented on Apr 17, 2026

    @amavashev
    Collaborator

    Status update — foundational bulk-action infrastructure shipped in v0.1.25.26

    The admin server just shipped two bulk-action endpoints in PR #109 (spec v0.1.25.21 → server v0.1.25.26):

    • POST /v1/admin/tenants/bulk-action — SUSPEND | REACTIVATE | CLOSE
    • POST /v1/admin/webhooks/bulk-action — PAUSE | RESUME | DELETE

    Both use the filter-spec shape discussed above (status / parent_tenant_id / search on tenants; tenant_id / status / event_type / search on webhooks) and share a common envelope:

    • expected_count preflight → 409 COUNT_MISMATCH (no writes)
    • 500-row hard cap → 400 LIMIT_EXCEEDED
    • 15-minute idempotency replay via shared IdempotencyStore (Redis)
    • Per-row outcome envelope: succeeded[] / failed[] / skipped[] with BulkActionRowOutcome
    • AdminKeyAuth only

    What this unlocks for this issue: the reusable pieces (BulkActionRowOutcome, IdempotencyStore, filter→count→gate→apply pattern, matchForBulk cap+1 repo sentinel) now exist and are test-covered (≥95% JaCoCo, 646/646 tests). Extending to /v1/admin/budgets/bulk-action for RESET / RESET_SPENT / CREDIT / DEBIT / REPAY_DEBT is mostly a matter of:

    1. Adding the spec in cycles-protocol (schemas for BudgetBulkFilter, operation-discriminated request, reusing BulkActionRowOutcome).
    2. Cloning the tenant/webhook bulk-action pattern onto BudgetController, backed by a BudgetRepository.matchForBulk.
    3. Deciding cascade semantics across scope trees (the main spec-design question flagged in the earlier comment — still open).

    Remaining design questions (unchanged from the earlier comment — calling out so this doesn't get lost):

    • Cascade ordering + partial-failure behavior across scope subtrees.
    • Cross-tenant safety on scope-cascade: child budgets under a different tenant MUST NOT be in scope.
    • Sync counts vs. async job-id for large cascades (10k+ budgets/tenant).

    Keeping this issue open — the infrastructure is ready, but the budget-specific spec and cascade semantics haven't been designed yet. When the spec PR lands on cycles-protocol, the server implementation will slot into the same pattern as tenants/webhooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions