Skip to content

fix: make heartbeat reporting and Prisma init build-safe#371

Open
itsmiso-ai wants to merge 2 commits into
mainfrom
saffron/fix-heartbeat-prisma-build
Open

fix: make heartbeat reporting and Prisma init build-safe#371
itsmiso-ai wants to merge 2 commits into
mainfrom
saffron/fix-heartbeat-prisma-build

Conversation

@itsmiso-ai

Copy link
Copy Markdown
Contributor

Summary

  • make Prisma initialization lazy so Next build can import route handlers without DATABASE_URL/DISPATCH_DATABASE_URL
  • keep runtime database access failing clearly when neither database env var is configured
  • report real heartbeat sync/reconcile counts instead of hardcoded zeroes
  • add regression coverage for no-DB import/build contract and heartbeat count reporting

Validation

  • npm run typecheck
  • npm run lint
  • npm run test
  • env -u DATABASE_URL -u DISPATCH_DATABASE_URL npm run build

@its-saffron its-saffron Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Automated Review

Full PR review.

Analysis engine: dsv4f@https://litellm.jory.dev/v1 (openai) — fallback (primary failed)

Recommendation: Approve

This PR fixes the build-time Prisma initialization to be lazy, resolving the crash during next build when DATABASE_URL is not set. It also corrects the heartbeat orchestration to report actual sync/reconcile counts instead of hardcoded zeros. Both changes include comprehensive regression tests.

Change-by-Change Findings

src/lib/prisma.ts

  • Replaces eager PrismaPg/PrismaClient construction (which threw at module load when DATABASE_URL was absent) with a lazy Proxy that defers initialization until first property access.
  • The initClient() function checks DATABASE_URL then DISPATCH_DATABASE_URL (alias) and throws a clear error if neither is set.
  • Exports __resetPrismaClientForTests() for test isolation.
  • Conforms to the AGENTS.md contract: "Next.js static generation runs at build time; Prisma initializes lazily."

src/lib/prisma.test.ts

  • Verifies that importing the module does not throw when DATABASE_URL is unset.
  • Verifies that accessing a property on the lazy proxy throws a clear error (/DATABASE_URL is not set/).
  • Verifies that after setting the env var and resetting the client, access succeeds.
  • Verifies that DISPATCH_DATABASE_URL is accepted as an alias.

src/lib/heartbeat.ts

  • runSyncBestEffort now returns the actual reposProcessed from the underlying syncIssuesForRepos result (was hardcoded 0).
  • runReconcileBestEffort now returns issuesReconciled, issuesChecked, and reposProcessed from the reconcileClosedIssues response (was only returning after catch block; the success path was returning nothing).

src/lib/heartbeat.test.ts

  • Mocks the external dependencies (issue-sync, config, github, prisma).
  • Tests that runReconcileBestEffort propagates the actual counts from the mock.
  • Tests that runSyncBestEffort propagates the actual reposProcessed from the mock.

Standards Compliance

  • The lazy Prisma pattern exactly matches the guidance in AGENTS.md under "DATABASE_URL not found at build time."
  • Environment variable resolution order (DATABASE_URL > DISPATCH_DATABASE_URL) matches the documented convention.
  • Error messages are clear and reference both env var names.
  • No secrets or build artifacts are committed.

Tool Harness Findings

  • The tool harness confirmed the contents of the changed files align with the diff.
  • No unexpected modifications were found.

Linked Issue Fit

  • No linked issue was provided; the PR body describes the problem and fix clearly.

Unknowns / Needs Verification

  • None. The CI checks (Docker Build, Validate) all passed.

Verdict: Approve

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