Skip to content

fix(selfhost): make PgStatement.first() coalesce to null like the D1 adapter#8411

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
RealDiligent:fix/critical-issue-pg-first-null-8361
Jul 24, 2026
Merged

fix(selfhost): make PgStatement.first() coalesce to null like the D1 adapter#8411
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
RealDiligent:fix/critical-issue-pg-first-null-8361

Conversation

@RealDiligent

Copy link
Copy Markdown
Contributor

Summary

  • PgStatement.first(colName) (src/selfhost/pg-adapter.ts) returned (colName ? row[colName] : row) as T with no coalesce, so when the requested key is absent from the row (or a driver represents SQL NULL as undefined) it returned undefined cast as T — breaking its own documented Promise<T | null> contract and diverging from the D1 sibling that callers rely on when treating the two backends interchangeably via backend-contracts.ts.
  • Applies the exact coalesce d1-adapter.ts:57-61 already uses: return ((colName != null ? row[colName] : row) ?? null) as T | null;. d1-adapter.ts is untouched (it is the correct reference), and no other PgStatement method is changed.
  • Closes the test asymmetry the issue calls out: selfhost-d1-adapter.test.ts has had a "returns null when the row exists but the column value is NULL" test all along, while the pg suite never exercised a NULL/absent-column case. Adds both parity cases.

Closes #8361

Scope

  • The PR title follows type(scope): short summary Conventional Commit format, for example fix(api): restore profile access checks.
  • This PR is focused and does not mix unrelated backend, UI, MCP, docs, dependency, and deploy changes.
  • This follows CONTRIBUTING.md and does not reintroduce GitHub Pages, VitePress, site/, or CNAME.
  • I linked a currently open issue this PR resolves (e.g. Closes #123) — a linked open issue is required for every contributor PR.

Validation

  • git diff --check
  • npm run actionlint
  • npm run typecheck
  • npm run test:coverage locally; codecov/patch requires ≥99% coverage of the lines AND branches you changed (aim for 100% on your diff so CI variance does not fail near the threshold). Global coverage is a non-blocking trend with a loose 90% backstop, not the gate.
  • npm run test:workers
  • npm run build:mcp
  • npm run test:mcp-pack
  • npm run ui:openapi:check
  • npm run ui:lint
  • npm run ui:typecheck
  • npm run ui:build
  • npm audit --audit-level=moderate
  • New or changed behavior has unit/integration tests for new branches, fallback paths, and sanitizer boundaries

If any required check was skipped, explain why:

  • This diff touches only src/selfhost/pg-adapter.ts (one return statement) and test/unit/selfhost-pg-adapter.test.ts, so actionlint (no workflow change), build:mcp/test:mcp-pack (no MCP change), ui:* (no UI/OpenAPI change), test:workers (no worker change), and npm audit (no dependency change) are not exercised by it.
  • Diff coverage verified at 100%, lines and branches, via the scoped simulation CI runs (vitest run --coverage --coverage.all=false): the changed line is hit and every branch on it is taken — the colName != null true arm (first("name")), its false arm (the existing no-arg first() test), the ?? null defined arm (a real value), and its nullish arm (the two new tests). test/unit/selfhost-pg-adapter.test.ts passes in full (11 tests) and root tsc --noEmit is clean.
  • I also confirmed the new tests genuinely pin the fix: reverting the coalesce makes the absent-column test fail, restoring it makes it pass.

Safety

  • No secrets, wallet details, hotkeys, coldkeys, user PATs, private keys, raw trust scores, private rankings, or private maintainer evidence are exposed.
  • Public GitHub text stays sanitized, low-noise, and does not imply compensation guarantees or optimization tactics.
  • Auth, cookie, CORS, GitHub App, Cloudflare, or session changes include negative-path tests.
  • API/OpenAPI/MCP behavior is updated and tested where needed.
  • UI changes use live API data or real empty/error/loading states, not production mock/demo fallbacks.
  • Visible UI changes include a UI Evidence section below with JPG/JPEG or PNG screenshots arranged as organized, captioned, clickable thumbnails. SVG screenshots are not used as review evidence. Review-only screenshots or recordings are not committed to the repository.
  • Public docs/changelogs are updated where needed; changelogs are only edited for release-prep PRs.

UI Evidence

Not applicable — backend adapter contract fix in src/selfhost/; no visible UI, frontend, docs, or extension change.

Notes

  • The condition is colName != null rather than a bare truthiness check specifically to match the D1 implementation character-for-character, as the issue requires — the two adapters' first() bodies are now identical in behavior.
  • Of the two added tests, the absent-column one is the true regression pin (it fails without the fix); the SQL-NULL one passes either way with the current pg driver and is kept as an explicit contract/parity assertion mirroring the D1 suite's existing test.

@RealDiligent
RealDiligent requested a review from JSONbored as a code owner July 24, 2026 11:53
@superagent-security

Copy link
Copy Markdown
Contributor

Superagent didn't find any vulnerabilities or security issues in this PR.

…adapter

PgStatement.first(colName) returned row[colName] uncoalesced, so an absent
column (or a driver representing SQL NULL as undefined) leaked undefined
through the documented Promise<T | null> contract, diverging from
d1-adapter.ts's first() -- the reference implementation callers rely on when
treating the two backends interchangeably via backend-contracts.ts.

Applies the same ?? null coalesce the D1 sibling already uses. Adds the
NULL-value and absent-column parity tests the pg suite was missing (the D1
suite has had the equivalent NULL test all along).

Closes JSONbored#8361
@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 24, 2026
@loopover-orb

loopover-orb Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-24 12:11:23 UTC

2 files · 1 AI reviewer · no blockers · readiness 93/100 · CI green · clean

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This is a small, correct fix: PgStatement.first() now coalesces undefined to null via `?? null`, exactly matching d1-adapter.ts's existing implementation, closing the contract divergence described in #8361. The change is a single-line logic fix plus two new parity tests (NULL value, absent column) that exercise the exact previously-broken paths, and the fix is verified against the real d1-adapter.ts reference implementation shown in context. Both new tests correctly assert null (not undefined) for the previously-broken cases.

Nits — 2 non-blocking
  • The inline comment in pg-adapter.ts:42-44 is fairly long for a one-line fix; could be trimmed since d1-adapter.ts's equivalent line has no comment at all.
  • Consider also adding this case to the shared contract test file (test/contract/selfhost-d1-database.test.ts) so future adapters automatically get this parity check instead of relying on each adapter's own unit test file.

Decision drivers

  • ✅ Code review — No blockers (1 reviewer)
  • ✅ Gate result — Passing (No configured blocker found.)
Context & advisory signals — never blocks the verdict
Signal Result Evidence
Linked issue ✅ Linked #8361
Related work ✅ No active overlap found No same-issue or scoped active PR overlap found.
Change scope ✅ 20/20 Low review scope from cached public metadata (1 linked issue).
Validation posture ✅ 25/25 PR body includes validation/test evidence.
Contributor workload ✅ 10/10 Author activity: 354 registered-repo PR(s), 137 merged, 37 issue(s).
Contributor context ✅ Confirmed Gittensor contributor RealDiligent; Gittensor profile; 354 PR(s), 37 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: minor
Linked issue satisfaction

Addressed
The diff applies the exact D1-matching coalesce to PgStatement.first(), leaving d1-adapter.ts and other PgStatement methods untouched, and adds two new tests covering both the NULL-value and absent-column cases mirroring the D1 test.

Review context
  • Author: RealDiligent
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: Python, Ruby, JavaScript, Svelte, TypeScript, Cuda, Markdown, MDX
  • Official Gittensor activity: 354 PR(s), 37 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Start here: Triage stale or unlinked PRs.
Signal definitions
  • Related work = same linked issue, overlapping active PRs, or title/path similarity.
  • Change scope = cached public metadata such as size labels, draft state, and review-burden hints.
  • Validation posture = whether the PR provides enough public validation/test evidence for maintainer review.
  • Contributor workload = public contributor activity and cleanup pressure, not a repo-wide quality failure.
  • Contributor context = public GitHub/Gittensor identity context; non-Gittensor status is not a blocker.
🧪 Chat with LoopOver

Ask LoopOver a question about this PR directly in a comment — grounded only in the same cached, public-safe facts shown above, never a new claim.

  • @loopover ask &lt;question&gt; answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat &lt;question&gt; answers in natural prose from cached decision-pack facts via local inference (maintainer/collaborator; read-only).
  • A plain-language @loopover mention with a real question is routed to the closest matching read-only command automatically — no exact syntax required.

Full command reference: https://loopover.ai/docs/loopover-commands

🧪 Experimental — new and may change.

🟩 Safe / merged · 🟦 Advisory · 🟨 Held for review · 🟥 Blocked / closed


💰 Earn for open-source contributions like this. Gittensor lets GitHub contributors earn for the work they already do — register to start earning →.

Checked by LoopOver, a quiet PR intelligence layer for OSS maintainers.

  • Re-run LoopOver review

@loopover-orb loopover-orb 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.

LoopOver approves — the gate is satisfied and CI is green.

@loopover-orb
loopover-orb Bot merged commit eff6fa4 into JSONbored:main Jul 24, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PgStatement.first() returns undefined instead of null, diverging from the D1 adapter's contract

1 participant