{"id":"XE-1129","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":242,"title":"desk/services/reporters.py & desk/api/team.py: reporters.merge Raises UniqueViolationError When into.id < other.id and Payout Addresses Desynchronize Across Endpoints","area":"other","claimed_tier":"high","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-07T16:31:15.354898+00:00","updated_at":"2026-11-08T20:46:10.101925+00:00","source_created_at":null,"told_at":"2026-11-08T20:46:10.107640+00:00","status_changed_at":"2026-11-08T20:46:04.656719+00:00","url":"https://bounty.xe.network/XE-1129","body":"In `desk/desk/services/reporters.py`, `desk/desk/services/sync.py`, and `desk/desk/api/team.py`, interacting defects in reporter merging and payout-address propagation cause unhandled database `IntegrityError` crashes (`HTTP 500`), orphaned sessions, and silent loss/desynchronization of bounty payout addresses:\n\n1. **`reporters.merge()` triggers PostgreSQL `UniqueViolationError` (`HTTP 500`) on `reporters.github_id` when `into.id < other.id` and orphans `ReporterSession` rows (`desk/desk/services/reporters.py:211-229`, `desk/desk/models.py:248`):**\n`Reporter.github_id` has a non-deferrable `UNIQUE` constraint (`models.py:248`). In `merge()`, `into.github_id = into.github_id or other.github_id` and `other.github_id = None` are dirtied in the same SQLAlchemy unit of work before `ctx.events.append()` calls `await db.flush()`. When `into.id < other.id` and `into.github_id` was `None` while `other.github_id` was set (e.g., `into` was created first via email/XE key and `other` was created later via GitHub OAuth, then linked into `into`), SQLAlchemy flushes `UPDATE reporters` in primary-key order (`into` before `other`), raising `UniqueViolationError` on `reporters_github_id_key` (`HTTP 500`). When `into.id > other.id`, `merge()` leaves `ReporterSession` rows (`reporter_sessions.reporter_id == other.id`) pointing to the wiped `\"merged into ...\"` shell row, and `from_xe()` (`lines 201-202`) merges without checking for conflicting `github_id`/`email`/`xe_address`, silently wiping `other`'s identity and `payout_proof`.\n\n2. **`Ticket.payout_address` is dropped or left stale across `sync.upsert_issue()`, `reporters.attach()`, `reporters.merge()`, and `reporters.bind_payout()`, while `GET /v1/team/payouts` and `GET /v1/team/payouts.csv` diverge (`desk/desk/services/sync.py:86-100`, `desk/desk/services/reporters.py:101-144, 211-248, 422-452`, `desk/desk/api/team.py:286-305`):**\n- `sync.upsert_issue()` (`sync.py:86-100`) creates `NewTicket(..., reporter_id=reporter.id if reporter else None)` without passing `payout_address=reporter.xe_address`, so every GitHub-synced issue filed by a reporter who already bound their payout address is created with `t.payout_address = \"\"`.\n- Neither `reporters.attach()` nor `reporters.merge()` backfills `r.xe_address` / `into.xe_address` onto unpaid tickets with `t.payout_address == \"\"`.\n- `reporters.bind_payout()` (`reporters.py:241-246`) updates `r.xe_address = address` and overwrites `r.payout_proof`, but only updates tickets where `Ticket.payout_address == \"\"`, leaving any unpaid ticket that had a previously bound address pointing to the stale address.\n- `GET /v1/team/payouts` (`team.py:286-305`) returns `t.payout_address` (and omits `award_approved_at.is_not(None)` / `award_xe.is_not(None)` filters), whereas `GET /v1/team/payouts.csv` (`reporters.py:422-452`) returns `r.xe_address` and truncates sub-XE amounts via `(t.award_xe or 0) // 1_000_000`.\n\n### Remediation\n1. In `reporters.merge()`, set `other.github_id = None` and `await db.flush()` before setting `into.github_id`, re-point `ReporterSession.reporter_id == other.id` to `into.id`, and backfill `t.payout_address = into.xe_address` on unpaid bounty tickets in `merge()` and `attach()`.\n2. Pass `payout_address=reporter.xe_address if reporter else \"\"` in `sync.upsert_issue()`, update all unpaid bounty tickets (`Ticket.status != \"paid\"`) when re-binding in `bind_payout()`, and align `GET /v1/team/payouts` with `reporters.payout_rows()`.","patch_refs":[],"thread":[{"id":1233,"source":"desk","source_ref":"reply:774","author":"Joseph Denne","team":true,"body":"Thanks for the report. Part of what you describe was already on file from earlier reports (https://bounty.xe.network/XE-0710 and https://bounty.xe.network/XE-0711); the remaining point about the account-merge error is being looked at and we will update this ticket when there is more to say.","created_at":"2026-11-08T20:46:10.101925+00:00"}]}