← all branches

fix/dup-repair

safe
80e0f62 · incrementalPR #306reviewed 2026-07-16 19:20 UTC0H · 0M · 1L · 4I
The branch
Purpose
One-shot operational repair for the June-2026 duplicate-contract damage, companion to #298 (the newest-evidence guard)
Goal
Identify duplicate utility contract groups, flip active status to the newest-evidence holder, re-point all site links and subscriptions, and clean up foreign bills with mismatched RPU
Sub-goals
  • Flip rule: newest-CFE-evidence holder becomes the active contract; contamination twins soft-deleted or kept as terminated history
  • Foreign-bill sweep: delete 12 #231-residue xml bills whose embedded RPU disagrees with their contract
  • Rigor: dry-run default, typed confirmation, per-group FOR UPDATE transactions, verify pass, idempotent re-runs
  • Staging rehearsal (2026-07-13): 3/3 groups repaired, verify 0 actionable
The changes (whole branch)
What
Added a self-evidencing audit log inside repairGroup() that queries post-transfer site_utility_contracts link counts for both the holder and deposed contracts after each SUC transfer block
Why
A run where ON CONFLICT silently skipped every insert (holder already linked) was previously indistinguishable from one that actually moved links — the count query closes that observability gap
Areas
scripts/repair-dup-contracts.ts+6200scripts/lib/repair-dup-contracts-core.ts+1320scripts/__tests__/repair-dup-contracts-core.test.ts+2030
Blast
3 new files, +955 lines total; one-shot operational script only — no domain entity, no API handler, no DB migration, no schema change
one-shot script — run on prod only after #298 ships (per runbook in file header)
CI checks· No CI checks found for this PRCodeRabbit· No .coderabbit.yaml in repo

Findings · 5

tests2

low

Number() coercion for pg count return type is untested

scripts/repair-dup-contracts.ts

The `Number(lc?.holder_links ?? 0)` coercion handles a postgres-js quirk where count() may return a string. A single test case asserting that `{ holder_links: '3', deposed_links: '0' }` renders as `holder=3 deposed=0` in the log line would make this airtight. Not blocking — no state mutation.

info

Audit log format not asserted in existing tests

scripts/repair-dup-contracts.ts

The console.log output format (including the conditional label) is untested. Acceptable for a diagnostic line in an operational script with no downstream consumers.

improvement3

low

Fragile dual-shape unwrapping `(linkCounts.rows ?? linkCounts)`

scripts/repair-dup-contracts.ts

Handles two different shapes of the raw SQL result. If the driver returns a third shape, the cast silently produces undefined for `lc` and logs zeros without error. A safer pattern would throw if the shape doesn't match, so a future driver change surfaces loudly rather than logging misleading zeros.

info

Two correlated subqueries can be replaced with a single aggregated query

scripts/repair-dup-contracts.ts

Each correlated subquery scans site_utility_contracts independently. A single query using `COUNT(*) FILTER (WHERE ...)` would read the table once. Negligible in a non-performance-critical script, but more readable.

info

Optional chaining on `lc?.holder_links` silently hides zero-row result

scripts/repair-dup-contracts.ts

If lc is undefined it means the count query returned no rows — which would be a bug. Defaulting to 0 via `?? 0` hides that scenario. A hard assertion would surface the issue more clearly than a silent zero.

History · 8 commits

  1. 80e0f62safeincremental0H · 0M · 1L2026-07-16 19:20current
  2. d513853needs attentionincremental0H · 2M · 3L2026-07-16 19:13
  3. 3de8c23safeincremental0H · 1M · 3L2026-07-14 14:23
  4. 8af2fabsafeincremental0H · 0M · 2L2026-07-14 14:16
  5. 8e6343eneeds attentionincremental0H · 1M · 3L2026-07-14 04:45
  6. 57eb1ebneeds attentionincremental0H · 2M · 4L2026-07-14 02:41
  7. a29f8f4needs attentionincremental0H · 4M · 6L2026-07-14 02:33
  8. 27179efneeds attentionfull5H · 4M · 3L2026-07-14 02:15