Skip to content

feat(opencode): killswitch — block requests when quota drops below threshold#35

Open
iceteaSA wants to merge 2 commits into
cortexkit:mainfrom
iceteaSA:feat/killswitch
Open

feat(opencode): killswitch — block requests when quota drops below threshold#35
iceteaSA wants to merge 2 commits into
cortexkit:mainfrom
iceteaSA:feat/killswitch

Conversation

@iceteaSA
Copy link
Copy Markdown
Contributor

@iceteaSA iceteaSA commented May 21, 2026

Dependency: Requires #34 (QuotaManager) to be merged first. Branch is based on feat/quota-manager.

Per-account request blocking when remaining quota drops below configurable thresholds. Returns synthetic 429 when all accounts (main + fallbacks) are below their thresholds.

Features:

  • Per-account threshold overrides (5h and 7d windows)
  • Eager quota refresh on first request for killswitch evaluation
  • Skip-main routing when main is killed (try surviving fallbacks)
  • Filter killed accounts from reactive fallback path
  • Retry-After header with earliest quota reset time + 60s buffer
  • /claude-killswitch slash command for runtime management (on/off/set thresholds)
  • Persisted to sidecar config

Files:

  • packages/core/src/killswitch.ts — new (164 lines)
  • packages/core/src/accounts.ts — killswitch types and policy functions
  • packages/opencode/src/index.ts — killswitch check in fetch handler, command registration
  • packages/opencode/src/tests/killswitch.test.ts — new (501 lines)
  • README.md + packages/opencode/README.md — killswitch documentation

Summary by cubic

Adds a per-account killswitch and a shared QuotaManager to OpenCode to block requests before hitting Anthropic limits and unify quota caching/backoff across main and fallback accounts. Integrated into routing and /claude-quota, with /claude-killswitch for quick runtime control.

  • New Features

    • Killswitch: per-account thresholds for 5h/7d (5h/1w aliases), skip-main routing and filtered fallbacks, synthetic 429 with retry-after (earliest reset + 60s) when all accounts are killed, eager quota refresh, and /claude-killswitch to view/toggle/set thresholds with persisted config.
    • QuotaManager in @cortexkit/anthropic-auth-core: unified main/fallback quota cache with inflight dedup, serial API gate (1s min gap), exponential backoff (60s–15m) for 429/5xx, cross-process file-lock, seeds from disk, persists main quota/backoff via callbacks; supports request-count refresh with quota.refreshEveryNRequests.
    • Integration: OpenCode routes and /claude-quota now read from QuotaManager; FallbackAccountManager seeds/uses the shared cache and staleness; fail-closed 429 (with backoff-based retry-after) when the quota API is backed off and no cached quota exists (honors failClosedOnUnknownQuota).
  • Migration

    • No breaking changes. To enable killswitch, add a killswitch block to the sidecar config (defaults off).
    • Optional: set quota.refreshEveryNRequests to force periodic quota refresh.

Written for commit f667649. Summary will update on new commits. Review in cubic

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 10 files

Tip: cubic can generate docs of your entire codebase and keep them up to date. Try it here.

Fix all with cubic | Re-trigger cubic

Comment thread packages/opencode/src/index.ts
Comment thread packages/core/src/killswitch.ts
@iceteaSA iceteaSA force-pushed the feat/killswitch branch 2 times, most recently from 65f5a21 to a4ba530 Compare May 22, 2026 16:52
Unified quota cache and API gateway for main + fallback quota state.
All consumers share one QuotaManager instance for consistent caching.

Features:
- Inflight deduplication prevents concurrent API calls
- Exponential backoff (60s-15min) for 429/5xx errors
- Persists main quota and backoff state to disk via callbacks
- Cross-process file lock guard for quota API dedup
- Seeds from persisted storage on construction
- Integrates with FallbackAccountManager for shared staleness
- Captures storage path at init to prevent test config corruption
- Request-count-based refresh trigger (refreshEveryNRequests)
…reshold

Per-account request blocking when remaining quota drops below configurable
thresholds. Returns synthetic 429 when all accounts (main + fallbacks) are
below their thresholds. Includes /claude-killswitch slash command for
runtime management.

Features:
- Per-account threshold overrides (5h and 7d windows)
- Eager quota refresh on first request for killswitch evaluation
- Skip-main routing when main is killed (try surviving fallbacks)
- Filter killed accounts from reactive fallback path
- Retry-After header with earliest quota reset time
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.

1 participant