Skip to content

Refactor AI-credit guard into focused modules - #11949

Merged
lpcox merged 3 commits into
mainfrom
copilot/refactor-ai-credits-guard
Nov 22, 2026
Merged

lpcox merged 3 commits into
mainfrom
copilot/refactor-ai-credits-guard

Conversation

@lpcox

@lpcox lpcox commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

The 840-line AI-credit guard mixes configuration, pricing, accounting, and steering on the budget-enforcement boundary. This refactor separates those concerns without changing callers or enforcement behavior.

  • Module boundaries: Extract configuration parsing, pricing resolution, credit accounting, and steering/limit handling into four focused modules.
  • Single state owner: Retain counters and the config cache in the 108-line facade. Submodules use a live state accessor so resets cannot leave stale counters.
  • Preserved behavior: Keep public exports, exact arithmetic, lazy default pricing, hard-cap precedence, and fail-closed error handling unchanged.
  • Characterization coverage: Add focused tests for shared totals, reset isolation, configuration refreshes, warning deduplication, and the export contract.

@lpcox lpcox changed the title [WIP] Refactor api-proxy guard into focused modules Refactor AI-credit guard into focused modules Nov 22, 2026
@lpcox
lpcox marked this pull request as ready for review November 22, 2026 17:23
Copilot AI adaptive review requested due to automatic review settings November 22, 2026 17:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Verifying shared-state wiring, hard-cap precedence, and fail-closed behavior across the extracted modules requires deeper review because errors could bypass spending limits.

No open issues 🎉

What changed in this PR

Refactors the API proxy’s AI-credit budget guard into focused modules, addressing #11942 while retaining the existing public interface and enforcement logic.

Changes:

  • Separates configuration, pricing, accounting, and steering into four modules.
  • Keeps mutable state and the configuration cache in the facade, with live state access across resets.
  • Adds characterization tests for shared totals, reset isolation, configuration refreshes, warning deduplication, and public exports.
File Description
containers/​api-proxy/​guards/​ai-credits/​steering.js Extracts budget warnings, reflection, and limit handling.
containers/​api-proxy/​guards/​ai-credits/​pricing.js Extracts pricing resolution, fallbacks, and pricing warnings.
containers/​api-proxy/​guards/​ai-credits/​config.js Extracts configuration parsing and hard-cap handling.
containers/​api-proxy/​guards/​ai-credits/​accounting.js Extracts exact credit calculations and usage accumulation.
containers/​api-proxy/​guards/​ai-credits-guard.js Becomes the facade and retains shared state ownership.
containers/​api-proxy/​guards/​ai-credits-facade.test.js Adds coverage for facade compatibility and shared-state behavior.

Review effort: Max (adaptive)

@github-actions

Copy link
Copy Markdown
Contributor

✅ Copilot review passed with no inline comments.

@lpcox Add the ready-for-aw label to this PR to trigger agentic CI smoke tests.

@lpcox
lpcox merged commit b64656c into main Nov 22, 2026
38 of 41 checks passed
@lpcox
lpcox deleted the copilot/refactor-ai-credits-guard branch November 22, 2026 17:51
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.

[Refactoring] Split containers/api-proxy/guards/ai-credits-guard.js into focused modules

3 participants