fix/dup-repair
safe80e0f62 · incrementalPR #306reviewed 2026-07-16 19:20 UTC0H · 0M · 1L · 4I- 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
- 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+620−0scripts/lib/repair-dup-contracts-core.ts+132−0scripts/__tests__/repair-dup-contracts-core.test.ts+203−0
- Blast
- 3 new files, +955 lines total; one-shot operational script only — no domain entity, no API handler, no DB migration, no schema change
Findings · 5
tests2
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.
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
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.
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.
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
- 80e0f62safeincremental0H · 0M · 1L2026-07-16 19:20current
- d513853needs attentionincremental0H · 2M · 3L2026-07-16 19:13
- 3de8c23safeincremental0H · 1M · 3L2026-07-14 14:23
- 8af2fabsafeincremental0H · 0M · 2L2026-07-14 14:16
- 8e6343eneeds attentionincremental0H · 1M · 3L2026-07-14 04:45
- 57eb1ebneeds attentionincremental0H · 2M · 4L2026-07-14 02:41
- a29f8f4needs attentionincremental0H · 4M · 6L2026-07-14 02:33
- 27179efneeds attentionfull5H · 4M · 3L2026-07-14 02:15