Skip to content

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

Merged
joryirving merged 5 commits into
mainfrom
saffron/fix-heartbeat-prisma-build
Jun 17, 2026
Merged

fix: make heartbeat reporting and Prisma init build-safe#371
joryirving merged 5 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[bot]

This comment was marked as outdated.

@its-saffron its-saffron Bot dismissed their stale review June 16, 2026 17:01

Superseded by a newer automated review for this pull request.

@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: MiniMax-M2.7@https://litellm.jory.dev/v1 (anthropic) — escalated (fast_low_confidence)

Recommendation: Approve

This PR fixes two production-impacting bugs with minimal, well-tested changes. The implementation is sound and follows the repository's documented conventions.

Change-by-Change Findings

src/lib/prisma.ts — Lazy Prisma initialization

The core fix: replaces eager PrismaPg/PrismaClient construction at module import with a Proxy that defers initClient() until first property access.

  • Proxy traps: get, has, ownKeys, getOwnPropertyDescriptor are all implemented. Method calls via get are correctly .bind(client) to preserve this context — important for Prisma's method chaining.
  • Error handling: initClient() throws with a clear message if neither DATABASE_URL nor DISPATCH_DATABASE_URL is set — matches the build-time contract documented in AGENTS.md: "The first actual use of the client at runtime should still surface a clear error."
  • Alias support: databaseUrl() checks DATABASE_URL first, then DISPATCH_DATABASE_URL, matching the resolution order in AGENTS.md.
  • Test helper: __resetPrismaClientForTests() is exported with a __ prefix and docstring marking it as internal — appropriate for a non-public test escape hatch.

src/lib/heartbeat.ts — Real heartbeat counts

Two specific fixes:

  1. runSyncBestEffort (line 98): reposProcessed is now assigned from result.repos instead of the hardcoded 0. This correctly reflects how many repos were processed during sync.
  2. runReconcileBestEffort (lines 168-177): Returns actual issuesReconciled, issuesChecked, and reposProcessed from the reconcileClosedIssues result — previously it was swallowing these values and returning an incomplete object.

Test coverage

Both new test files (prisma.test.ts, heartbeat.test.ts) use vi.hoisted and vi.mock patterns consistent with the existing test suite. The prisma.test.ts tests the lazy import contract directly, including:

  • No throw on import without DATABASE_URL
  • Throw on first property access
  • Environment alias (DISPATCH_DATABASE_URL) works
  • No error leak across reset cycles

Standards Compliance

  • AGENTS.md §Code Standards: Error handling uses error instanceof Error pattern where applicable; the new code correctly surfaces meaningful messages.
  • AGENTS.md §Prisma Notes: Lazy initialization resolves the documented build-time issue: "Next.js static generation runs at build time; Prisma initializes lazily."
  • AGENTS.md §Environment Variables: Resolution order (DATABASE_URL > DISPATCH_DATABASE_URL) is correctly implemented.

Linked Issue Fit

No linked issue. The PR is self-describing: it addresses a build breakage where npm run build failed without DATABASE_URL due to Next.js stripping the process.env.NODE_ENV === "production" guard during bundling, and incorrect heartbeat telemetry.

Evidence Provider Findings

No evidence providers configured for this PR.

Tool Harness Findings

No tool harness output present.

CI Check Results

CI reached terminal state with success for both "Validate" and "Docker Build" checks. The test suite and build validation completed successfully.

Unknowns / Needs Verification

  • Test suite execution: The CI result shows Validate completed but does not enumerate which commands ran. The PR body commits to npm run typecheck, npm run lint, npm run test, and env -u DATABASE_URL -u DISPATCH_DATABASE_URL npm run build. The corpus does not contain the actual test output, but the passing CI status is the authoritative signal.

Open Findings From Previous Review

None.

@joryirving joryirving enabled auto-merge (squash) June 17, 2026 03:02
@joryirving joryirving merged commit 3090eed into main Jun 17, 2026
3 checks passed
@joryirving joryirving deleted the saffron/fix-heartbeat-prisma-build branch June 17, 2026 03:15
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.

2 participants