fix(ai): lazily seed the usage DB on first AI use (fix #113 first-run e2e regression)#114
Merged
Conversation
… fresh workspaces)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
C1 (#113) eager-seeded the admin-only "AI usage" database at server startup, so every workspace — including a freshly-wiped one — gained an "AI usage" page. That broke the
freshWorkspace → lands on Homeinvariant: thePlaywright P1 gatefailed ondatabase-views.spec.ts+onboarding.spec.ts([data-home-screen]never visible). It reachedmainbecause that gate isn't a required check — fixing forward.Solution
Seed the usage DB lazily, on the first AI use, instead of at startup:
startServernow callsAiUsageLog.load()— which only adopts an already-created DB recorded insettings(from a prior run), creating nothing.ensureSeeded()called fromlog().restrictedread still apply once the DB exists; on restart a prior DB is re-adopted so the gates resolve immediately (no wait for the next AI call).Key files:
packages/server/src/ai/usage.ts(load/tryAdopt/ensureSeeded),server.ts(loadnotensureSeeded),store.ts,packages/server/src/aiUsage.test.ts. New deps: none.Before / After
No visual change (backend).
Test procedure
pnpm verify→ green.pnpm --filter @book.dev/web exec playwright test e2e/database-views.spec.ts e2e/onboarding.spec.ts→ pass; fulltest:e2e:core(P1 gate) on CI.aiUsage.test.ts: existing C1 acceptance retained (adjusted to log-first so the lazily-created DB exists) + a new "no AI use → no usage DB/page is created" test.Operational notes
restricted"AI usage" page now appears only after the first AI call (not in AI-less workspaces) — this is the intended cleaner behavior.mainf449e68(post-feat(ai): usage + cost attribution to an admin-only, tamper-locked database #113).Verify: green. Gates: fix-forward to reviewed C1 — no new security surface (same managed gates + attribution;
isManagedPagecorrectly returns false when no usage DB exists yet).🤖 Generated with Claude Code
https://claude.ai/code/session_01X78XTAemKrFw1uJpDpCEaG