Skip to content

fix(ai): refresh the entitlement and retry once on a 401 from the proxy - #56

Merged
PAMulligan merged 1 commit into
mainfrom
22-proxy-401-refresh
Aug 21, 2026
Merged

fix(ai): refresh the entitlement and retry once on a 401 from the proxy#56
PAMulligan merged 1 commit into
mainfrom
22-proxy-401-refresh

Conversation

@PAMulligan

Copy link
Copy Markdown
Contributor

Summary

Closes #22. The issue's acceptance criteria were all long since shipped (hosted requests to POST /ai/generate with per-env API_BASE, X-Optia-Entitlement for Pro / installId for free, quota feeding the entitlement layer, full error mapping, BYO-key direct-to-Anthropic, mocked-backend tests) — except one: 401 → entitlement refresh, one retry. This PR adds it:

  • runProxy catches an unauthorized proxy error on an authenticated call, runs refreshNow(), and retries the request exactly once with the fresh token
  • A refresh that returns null (revoked/unknown license — local state already cleared by refreshNow) rehydrates the entitlement store so the UI degrades to Free instead of retrying into the same 401
  • Free-tier 401s and 401s on the retry itself are never re-attempted

Testing

  • 4 new tests: successful refresh-and-retry, failed refresh → rehydrate + rethrow, no second retry, free tier never refreshes
  • Full suite 518/518, typecheck and lint clean

🤖 Generated with Claude Code

A 401 on a Pro proxy call usually means the cached 24h entitlement just
expired between alarm refreshes. Instead of surfacing an error toast,
refresh the entitlement with the stored license key and retry the request
once. A refresh that comes back empty (revoked license) rehydrates the
entitlement store so the UI degrades to Free rather than looping.

This was the last open acceptance criterion of #22 — everything else
(hosted routing, entitlement header, per-env API base, quota feedback,
error mapping, BYO-key direct routing, mocked-backend tests) already
shipped in the entitlement/gating/QA work.

Closes #22.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@PAMulligan
PAMulligan merged commit 6324a21 into main Aug 21, 2026
5 checks passed
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.

Point the extension's AI calls at the backend proxy

1 participant