Skip to content

fix(ai): lazily seed the usage DB on first AI use (fix #113 first-run e2e regression)#114

Merged
eliotlim merged 1 commit into
mainfrom
feat/ai-usage-lazy-seed
Jul 6, 2026
Merged

fix(ai): lazily seed the usage DB on first AI use (fix #113 first-run e2e regression)#114
eliotlim merged 1 commit into
mainfrom
feat/ai-usage-lazy-seed

Conversation

@eliotlim

@eliotlim eliotlim commented Jul 6, 2026

Copy link
Copy Markdown
Owner

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 Home invariant: the Playwright P1 gate failed on database-views.spec.ts + onboarding.spec.ts ([data-home-screen] never visible). It reached main because that gate isn't a required check — fixing forward.

Solution

Seed the usage DB lazily, on the first AI use, instead of at startup:

  • startServer now calls AiUsageLog.load() — which only adopts an already-created DB recorded in settings (from a prior run), creating nothing.
  • The DB is created on the first attribution write, via ensureSeeded() called from log().
  • A workspace that never uses AI has no usage page → a fresh workspace stays empty → lands on Home. The managed write-gate + restricted read 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 (load not ensureSeeded), store.ts, packages/server/src/aiUsage.test.ts. New deps: none.

Before / After

No visual change (backend).

Behavior Before After
Fresh workspace (no AI used) had an "AI usage" page → didn't land on Home (P1 gate red) empty → lands on Home (P1 gate green)
Usage DB creation at every server startup on the first AI attribution write
Restart with prior usage re-created/looked-up eagerly re-adopts the existing DB (managed gates resolve immediately)

Test procedure

  • pnpm verify → green.
  • Web e2e (the regressed specs): pnpm --filter @book.dev/web exec playwright test e2e/database-views.spec.ts e2e/onboarding.spec.ts → pass; full test: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


Verify: green. Gates: fix-forward to reviewed C1 — no new security surface (same managed gates + attribution; isManagedPage correctly returns false when no usage DB exists yet).

🤖 Generated with Claude Code

https://claude.ai/code/session_01X78XTAemKrFw1uJpDpCEaG

@vercel

vercel Bot commented Jul 6, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
app.book.pub Ready Ready Preview, Comment Jul 6, 2026 2:14pm

Request Review

@eliotlim eliotlim merged commit f6fbc55 into main Jul 6, 2026
7 checks passed
@eliotlim eliotlim deleted the feat/ai-usage-lazy-seed branch July 6, 2026 14:28
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.

1 participant