Skip to content

fix(ai-review): cap BYOK daily review usage - #683

Merged
JSONbored merged 2 commits into
mainfrom
codex/fix-byok-ai-review-spend-limit-bypass
Jun 14, 2026
Merged

fix(ai-review): cap BYOK daily review usage#683
JSONbored merged 2 commits into
mainfrom
codex/fix-byok-ai-review-spend-limit-bypass

Conversation

@JSONbored

Copy link
Copy Markdown
Owner

Motivation

  • Prevent maintainer-paid BYOK advisory calls from bypassing the shared daily Workers-AI budget and exhausting a maintainer's provider quota by adding a separate per-repo/day cap for BYOK advisory calls.
  • Keep the existing free Workers-AI budget separation while introducing a lightweight safeguard specific to BYOK provider usage.

Description

  • Enforce a per-repository/day BYOK advisory cap in runGittensoryAiReview before any external provider request is made, using an environment-configurable AI_BYOK_DAILY_REPO_LIMIT and a DEFAULT_BYOK_DAILY_REPO_LIMIT fallback.
  • Add countByokAiReviewEventsForRepoSince in src/db/repositories.ts to count successful BYOK ai_review_pr events for a repo since UTC day start and import it into src/services/ai-review.ts to enforce the cap.
  • Add AI_BYOK_DAILY_REPO_LIMIT to the typed Env (src/env.d.ts) and populate the default in wrangler.jsonc, and update unit tests in test/unit/ai-review.test.ts to cover the per-repo BYOK quota behavior.
  • Update inline comments to clarify that Workers-AI and maintainer-paid BYOK calls are budgeted separately and that BYOK calls have a dedicated per-repo/day quota.

Testing

  • Ran git diff --check which completed with no issues.
  • Ran npm run typecheck which failed due to missing local type dependencies (typecheck blocked by absent dev deps in this environment).
  • Ran npx vitest run test/unit/ai-review.test.ts which could not run because the environment could not fetch dev dependencies from the npm registry (403 Forbidden), though tests were updated to assert the new BYOK quota behavior.

Codex Task

@ghost

ghost commented Jun 14, 2026

Copy link
Copy Markdown

Note

Gittensory Gate skipped

PR closed before full evaluation. No late first comment was created.

Signal Result Evidence Action
Gate result ⚠️ Skipped #683 is no longer open. No action.

💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by Gittensory, a quiet PR intelligence layer for OSS maintainers.

@ghost ghost added the gittensory:reviewed label Jun 14, 2026
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

@ghost

ghost commented Jun 14, 2026

Copy link
Copy Markdown

gittensory · advisory review

Reviewed 5 changed file(s) — two independent AI reviewers.

Suggested action:Safe to merge — both reviewers found no blocking issues.

Reviewer A · gpt-oss-120b — recommends ✅ merge
The PR introduces a per‑repository daily cap for maintainer‑paid BYOK AI review calls, adds the supporting DB query, env var, default, and tests. The changes are well‑scoped, compile‑time safe, and include coverage for the new quota logic.

Suggestions

  • Verify that clampNumber is imported in ai-review.ts (or defined) to avoid a runtime error.
  • Consider adding a unit test for the default limit when AI_BYOK_DAILY_REPO_LIMIT is unset, ensuring the fallback works.
  • Document the new env var in the README or configuration docs for operators.

Worth double-checking

  • Potential race condition where concurrent BYOK calls could briefly exceed the daily limit before the count is updated.
  • The SQL count query relies on json_extract of metadataJson; ensure existing records store repoFullName consistently.

Reviewer B · nemotron-3-120b-a12b — recommends ✅ merge
The PR adds a per-repository daily usage cap for BYOK (Bring Your Own Key) AI advisory calls to prevent excessive usage. It introduces a new database function to count BYOK events, integrates the limit check into the AI review flow, adds environment variable support, and includes tests. The changes are focused, follow existing patterns, and maintain the public/private boundary by only affecting internal metering.

Suggestions

  • Consider adding a test for the edge case where AI_BYOK_DAILY_REPO_LIMIT is set to '0' to ensure it blocks all BYOK calls for a repo.
  • In the repository function, the comment about V8 ignoring the next line is acceptable but could be clarified to explain why the fallback is needed (D1 driver anomalies).
  • Ensure the utcDayStartIso() function used in the new query matches the timestamp format stored in the database (likely ISO 8601 UTC).

Worth double-checking

  • The SQL query uses json_extract which is D1/SQLite-specific; confirm this aligns with the project's database assumptions.
  • Verify that the BYOK event recording correctly sets feature='ai_review_pr', status='ok', and model with 'byok:' prefix for the new counter to work.
  • Check that the clampNumber utility properly handles non-numeric env values (though the existing test shows it's used with string env).

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 14, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
gittensory-ui 64a6723 Commit Preview URL

Branch Preview URL
Jun 14 2026, 02:15 PM

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

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant