Skip to content

fix(ai-search): return the remaining searches with the search itself - #33

Merged
viclafouch merged 2 commits into
mainfrom
fix/ai-search-quota-counter
Aug 23, 2026
Merged

viclafouch merged 2 commits into
mainfrom
fix/ai-search-quota-counter

Conversation

@viclafouch

Copy link
Copy Markdown
Owner

Closes #22

The issue, checked

Half true, so worth stating precisely.

True: waitUntil carries the AiSearchLog past the response, so the write the counter reads lands after the client already has its answer. Refetching the Quota on success races that write.

False: the title. invalidateQueries overrides staleTime and refetches immediately, so there is no systematic one minute lag. And the insert starts before the response is serialised, so it has a full HTTP round trip of head start over the refetch: in practice it wins nearly every time.

What is left is a real defect on a paid counter: it is right by luck, and a lost race then sticks for the minute the query stays fresh.

The fix

The search already computed the figure. It hands it back, the client writes it into the cache with setQueryData. The race goes, and with it a round trip and a count on every AI search: the database sleeps a little longer.

buildAiSearchQuota is now the only place the cap rule lives, shared by aiSearchMemes and checkAiSearchQuota.

Proof

  • 28 unit tests, including 4 on the Quota arithmetic (Premium, fresh free User, cap reached, and an ex Premium past the cap who would otherwise see a negative count).
  • The 27 e2e of the AI search journey pass, including a new assertion that the counter goes 3 → 2 without a reload. The old test only checked it after page.reload().
  • Honest caveat: that assertion would not reliably have gone red before this change, since the refetch usually won the race. It is a guard, not a proof of the bug.

Watch after deploy

Nothing schema related, nothing to migrate. If the waitUntil write fails, the counter now shows one search too few until the query goes stale, against one too many before. The error is still captured in Sentry.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
petit-meme Ready Ready Preview Aug 23, 2026 9:32am

Request Review

viclafouch and others added 2 commits August 23, 2026 11:32
The AiSearchLog the counter reads is written with waitUntil, so it lands
after the response has left. Refetching the quota on success races that
write, and a lost race leaves the old count on screen for the minute the
query stays fresh.

The search already computed the figure, so it hands it back and the
client writes it into the cache. The round trip and the count query it
cost on every search go with it.

Closes #22

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The word was already in the code and in no glossary. It names what is
left of a Plan's cap, not the entitlement, and the distinction is the
one that gets blurred.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@viclafouch
viclafouch force-pushed the fix/ai-search-quota-counter branch from 1d84964 to 64c5f6b Compare August 23, 2026 09:32
@viclafouch
viclafouch merged commit 1bcec93 into main Aug 23, 2026
5 checks passed

This branch was successfully deployed

1 active deployment
Preview — 64c5f6be Deployed Aug 23, 2026 by vercel[bot]
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.

fix(ai-search): the remaining searches counter lags a full minute

1 participant