Skip to content

Add Cloud code-run quotas: monthly cap + per-org concurrency - #1492

Merged
helpcodeai merged 1 commit into
mainfrom
helpcodeai/code-mode-cloud-run-quotas
Nov 3, 2026
Merged

helpcodeai merged 1 commit into
mainfrom
helpcodeai/code-mode-cloud-run-quotas

Conversation

@helpcodeai

Copy link
Copy Markdown
Contributor

Closes #1491. The last code-side gap before enabling code mode on Cloud (see the activation plan and PRs #1447 / #1490).

Why

Cloud shares one runner between tenants. The per-run limits (deadline, memory, tool-call cap, output size) bound a single run but not a tenant's volume or burst, so one workspace could run code without bound or saturate the shared runner for everyone. This adds two per-workspace quotas.

What

Enforced at the single run chokepoint in DynamicMcpTools.executeCodeTool and executeInlineCode, before the run is handed to the runner. Over a quota the run is refused with a clear message (QuotaExceeded) and nothing executes.

  • Monthly run cap — authoritative from the code_runs table, so it holds with or without Redis: trial 100, Starter 500, Team 5,000, Business 5,000, Enterprise unlimited. A contract can override with the licence feature maxCodeRunsPerMonth.
  • Per-org concurrency cap — best-effort via the existing Redis (incr/decr + a 120 s safety TTL), skipped when Redis is down: trial/Starter 1, Team/Business 3, Enterprise 10.
  • A card-backed trial gets the Team quotas (reuses effectiveCodeModePlan).

Cloud + self-hosted

  • Cloud: both quotas apply.
  • Self-hosted: single-tenant, not metered by runs — the guard is a no-op (isCloud gate), unchanged behaviour.

How (no schema, no ctor change)

  • license/code-run-quotas.ts (new): pure cap tables + monthlyCodeRunCapForPlan, concurrentCodeRunCapForPlan, startOfUtcMonth.
  • license/license-guard.service.ts: codeModePlanForOrg() exposes the effective plan (card-trial -> team).
  • mcp-server/dynamic-mcp-tools.ts: acquireCodeRunLease() / releaseInflight(), wired around the executor call. Reuses the already-injected prisma / redisService / deployment / licenseGuard — no constructor change, so no test construction churn.
  • Uses the existing code_runs table and Redis: no Prisma/schema change.

Tests / checks (packages/backend)

  • npx tsc --noEmit — clean; npx eslint on the touched files — clean.
  • npx jest --maxWorkers=4 src/license dynamic-mcp-tools — 394 pass (adds quota unit tests + enforcement tests: monthly cap, concurrency cap + counter release, inline path, self-hosted no-op).
  • npx jest --maxWorkers=4 mcp-endpoint code-tool — 71 pass.
  • node scripts/generate-llms-txt.mjs --check — clean.

Not in this PR (operational, out of repo per AGENTS)

Deploying the runner on Cloud (CODE_RUNNER_URL) and the pre-GA pen test — tracked in the activation plan. A global runner pool/queue is a possible follow-up to the per-org concurrency cap.

Cloud is multi-tenant, so the per-run limits (deadline, memory, tool-call cap,
output size) are not enough: one tenant could run code without bound or saturate
the shared runner. Add two per-workspace quotas, enforced at the single run
chokepoint before handing to the runner.

- Monthly run cap, authoritative from the code_runs table so it holds with or
  without Redis: trial 100, Starter 500, Team 5,000, Enterprise unlimited (a
  contract may override via features.maxCodeRunsPerMonth). A card-backed trial
  gets the Team quotas (reuses effectiveCodeModePlan).
- Per-org concurrency cap, best-effort via the existing Redis (incr/decr with a
  safety TTL), skipped when Redis is down: trial/Starter 1, Team 3, Enterprise 10.
- Cloud only: self-hosted is single-tenant and not metered by runs, so the guard
  is a no-op there. Over a quota, the run is refused with a clear message and
  nothing executes.

No schema change (uses the existing code_runs table and Redis); no DynamicMcpTools
constructor change (reuses its injected prisma/redis/deployment/licenseGuard).
@helpcodeai
helpcodeai merged commit 757de16 into main Nov 3, 2026
15 checks passed
@helpcodeai
helpcodeai deleted the helpcodeai/code-mode-cloud-run-quotas branch November 3, 2026 15:56
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code mode: Cloud run quotas (monthly cap + per-org concurrency) before Cloud enablement

1 participant