Skip to content

fix(web): follow the token-sharing usage-limit contract - #6971

Merged
iscekic merged 2 commits into
mainfrom
kwf/chatgpt-sharing-quota
Sep 29, 2026
Merged

iscekic merged 2 commits into
mainfrom
kwf/chatgpt-sharing-quota

Conversation

@iscekic

@iscekic iscekic commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

Kilo stores the ChatGPT plan limit only to show the partner usage-limit notice. The record never gated a request: routing checks only is_enabled and status === 'connected' (routing.ts:178). Three gaps made the notice wrong, and one gap made recovery wrong.

  • No notice for a sharing limit. OpenAI returns the code subscription_sharing_usage_limit_exceeded for this integration. The reader knew only usage_limit_reached and insufficient_quota, so a sharing limit wrote no record. The person saw a raw 429 with no "Manage usage" action.
  • Wrong destination. The notice opened the Codex dashboard. An app-specific weekly limit can be exhausted while the plan still has allowance, and only ChatGPT usage settings list it. The guideline requires https://chatgpt.com/settings/usage.
  • The record outlived the limit. OpenAI reports no reset time for an app limit, and a reset a person applies in ChatGPT restores the allowance early. Only a reconnect or the recorded window cleared the record. A request that succeeds now clears it, after the response.
  • token_expired was retryable. OpenAI's "Errors and recovery" reference names it with the unusable refresh tokens. Kilo retried it and served the request on another billing path instead of showing the reconnect message.

The clear-on-success write runs in after(). A guard stops a scheduling failure from changing the outcome of a request that succeeded.

Verification

No manual test. A manual test needs a ChatGPT Plus or Pro subscription in the exhausted app-limit state, and no such account was available. Automated checks instead:

  • pnpm exec jest src/lib/ai-gateway/openai-chatgpt — 7 suites, 118 tests, including a real-database round trip for record and clear.
  • pnpm exec jest src/lib/ai-gateway src/routers/openai-chatgpt-router.test.ts src/components/chatgpt — 668 tests.
  • pnpm typecheck, pnpm lint, and the format check.

Visual Changes

N/A

Reviewer Notes

  • The record is display state only. usage_limit_reached_at has one reader, openAiChatGpt.status.
  • The gateway does not pause requests while a limit stands, against the errors doc's wording. A pause would refuse requests after OpenAI restored the allowance. OpenAI decides each request, and the notice follows the answer.
  • insufficient_quota stays in the marker list. Our own partner key can answer with it, which would mis-attribute a platform quota to the person's plan. Not fixed here.

The gateway stores the ChatGPT plan limit to show the partner notice. The
record never gated a request. Three gaps made the notice wrong.

Read the documented sharing code. `subscription_sharing_usage_limit_exceeded`
is the code OpenAI returns for this integration. The reader knew only
`usage_limit_reached` and `insufficient_quota`, so a sharing limit wrote no
record and the person saw a raw error with no way forward.

Open the ChatGPT usage settings page. The notice opened the Codex dashboard.
That page does not list the weekly limit a person sets for one app. An app
limit and the plan allowance are separate meters.

Clear the record when a request through the connection succeeds. OpenAI
reports no reset time for an app limit, and a reset a person applies in
ChatGPT restores the allowance early. The record outlived the limit, and only
a reconnect or the recorded window cleared it.
Guard the `after` call that clears a recorded limit. `after` throws outside
a request scope. The unguarded call could reject the sync and fail a request
that already succeeded.

Treat `token_expired` as a terminal refresh failure. OpenAI's "Errors and
recovery" reference names it with the unusable refresh tokens. Kilo retried
it and moved a request to another billing path instead of showing the
reconnect message.
@iscekic iscekic self-assigned this Sep 29, 2026
@iscekic
iscekic marked this pull request as ready for review September 29, 2026 18:32
@kilo-code-bot

kilo-code-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (8 files)
  • apps/web/src/lib/ai-gateway/openai-chatgpt/refresh.test.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/refresh.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/shared-services.test.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/store.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/usage-limit.test.ts
  • apps/web/src/lib/ai-gateway/openai-chatgpt/usage-limit.ts
  • apps/web/src/lib/ai-gateway/providers/upstream-attempt.ts
  • packages/db/src/schema.ts

Verified the new sharing-limit marker, the settings URL, the success-path clear via after(), and the new terminal token_expired refresh code against the changed lines. The after() guard, the isNotNull filter, and the marker lookups are behavior-preserving and safe for untrusted input. No blocking issues found.


Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

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.

2 participants