Skip to content

refactor(prisma): tighten the $transaction adapter cast#519

Merged
joryirving merged 2 commits into
mainfrom
refactor/prisma-transaction-cast
Jul 1, 2026
Merged

refactor(prisma): tighten the $transaction adapter cast#519
joryirving merged 2 commits into
mainfrom
refactor/prisma-transaction-cast

Conversation

@joryirving

Copy link
Copy Markdown
Contributor

Summary

  • asPrFixQueueClient / asAgentWorkClient used a double as any on $transaction ((fn) => client.$transaction(fn as any) as any), silencing all typechecking on the callback + its return. Replace with a single explicit cast of the callback to (tx: Prisma.TransactionClient) => Promise<T> and drop the result as any, so the transaction's return type T is checked again.

Why

The only genuine type gap is the tx type (the adapter interfaces type it as the narrowed client; Prisma types it as Prisma.TransactionClient) — structurally compatible for the model delegates these callbacks use. The narrow, documented cast preserves that bridge without blinding the compiler to callback return types.

Verification

  • tsc → 0; eslint → clean; vitest pr-fix-queue + agent-work → 50 passing.

Closes #510 · part of #498

asPrFixQueueClient/asAgentWorkClient wrapped the interactive-transaction
callback in a double `as any`, silencing all typechecking on the callback and
its return. The only real gap is the tx type (adapter client vs Prisma's
TransactionClient), which are structurally compatible for the delegates these
callbacks use. Cast the callback once to (tx: Prisma.TransactionClient) and
drop the result `as any`, so the transaction's return type T is checked again.

Closes #510

@its-saffron its-saffron Bot left a comment

Copy link
Copy Markdown
Contributor

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 is a clean, targeted type-safety improvement that directly addresses issue PR 510. The change is minimal (10 additions, 3 deletions in a single file), all CI checks pass, and the implementation is well-reasoned.

Change-by-Change Findings

src/lib/prisma.ts

Import addition (+2): Added Prisma to the import from @prisma/client to access Prisma.TransactionClient for the explicit cast.

asPrFixQueueClient refactor:

  • Before: (fn) => client.$transaction(fn as any) as any — double as any silenced all typechecking on callback parameter and return type.
  • After: <T>(fn: (tx: PrFixQueueClient) => Promise<T>): Promise<T> => client.$transaction(fn as (tx: Prisma.TransactionClient) => Promise<T>) — single narrow cast only on the tx parameter type (PrFixQueueClientPrisma.TransactionClient). The generic return type T is now preserved and typechecked.

asAgentWorkClient refactor: Same pattern applied to the agent-work adapter.

Comments: A 6-line explanatory comment documents why the cast is safe (structural compatibility for model delegates) and why it's preferable to as any.

Standards Compliance

  • Code Standards - "Error handling": Uses error instanceof Error pattern elsewhere in the file; this PR is type-only, no runtime error paths introduced.
  • No commit of secrets: No secrets or env files touched.
  • Prisma conventions: Follows existing src/lib/prisma.ts patterns; Prisma namespace is already used in this codebase (via generated client).
  • No agent-specific names in generic docs: N/A — internal refactor.

Linked Issue Fit

Issue PR 510 specified:

  • Problem: Double as any on $transaction (lines 102, 110) silences all typechecking — addressed
  • Fix: "type the adapter's $transaction wrapper properly (or narrow the cast)" — done: single narrow cast preserves return type T
  • Effort: M — proportionate to the scope (single file, minimal changes)

This PR is part of PR 498 (dispatch audit), consistent with a systematic type-safety audit.

Evidence Provider Findings

No evidence providers configured; skipped.

Tool Harness Findings

  • read_file confirmed the final state of src/lib/prisma.ts matches the diff.
  • git_log confirmed commit d97acf0 is the PR head.
  • git_grep on asPrFixQueueClient found 16 usage sites across routes, tests, and library code — all consuming the narrowed client; none call $transaction directly, confirming the narrow cast is sufficient.
  • git_grep on asAgentWorkClient found 7 usage sites — same conclusion.

CI Verification

All five CI checks concluded success on commit d97acf0:

  • Docker Build ✓
  • Build ✓
  • Tests ✓
  • Typecheck ✓
  • Lint ✓

The PR body claims tsc → 0, eslint → clean, vitest (pr-fix-queue + agent-work) → 50 passing, consistent with CI passing.

Unknowns / Needs Verification

None. The diff is self-contained, CI green, and the change is straightforward to reason about: structural compatibility between PrFixQueueClient/AgentWorkClient (narrow client subsets) and Prisma.TransactionClient (Prisma's own tx client) is well-established — these are just subsets of the same model delegates. The single explicit cast is more expressive and safer than as any.

@joryirving joryirving enabled auto-merge (squash) July 1, 2026 21:48
@joryirving joryirving merged commit ea4e485 into main Jul 1, 2026
6 checks passed
@joryirving joryirving deleted the refactor/prisma-transaction-cast branch July 1, 2026 21:50
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.

refactor(prisma): tighten the double as any on $transaction adapter

1 participant