Skip to content

fix(selfhost): add the ams_signals conflict key so Postgres AMS ingest stops throwing - #8398

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
kai392:fix/critical-issue-ams-signals-conflict-key
Jul 24, 2026
Merged

fix(selfhost): add the ams_signals conflict key so Postgres AMS ingest stops throwing#8398
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
kai392:fix/critical-issue-ams-signals-conflict-key

Conversation

@kai392

@kai392 kai392 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add ams_signals to REPLACE_CONFLICT_KEYS in src/selfhost/pg-dialect.ts. Without it, translateInsertOr threw pg_dialect: INSERT OR REPLACE into 'ams_signals' has no known conflict key on every self-host Postgres deployment, so the first AMS telemetry-ingest write (src/ams/ingest.ts, reached via POST /v1/ams/ingest) failed the request outright.

The key-shape evaluation the issue asked for — the 2-column key is correct here

Per the issue's second branch: the orb_signals collision reasoning does not apply to ams_signals, so no migration is added and the entry matches the table's existing constraint.

  • Both tables are fed by the same exporter, packages/loopover-miner/lib/orb-export.ts (DEFAULT_AMS_COLLECTOR_URL = ".../v1/ams/ingest"), which derives:
    • repoHash = hmacAnonymize(repoFullName, secret)
    • prHash = hmacAnonymize(${repoFullName}:${prNumber}, secret)
  • The repo is therefore inside the pr_hash input: within one instance_id (one secret), a pr_hash already identifies (repo, PR), and repo_hash is functionally determined by it. Two different repos can never collide on (instance_id, pr_hash) — exactly the "pr_hash's hash input already disambiguates by repo" case the issue names.
  • Widening would also be actively wrong to ship alone: the map must name the table's real constraint (UNIQUE (instance_id, pr_hash), migrations/0148_ams_signals.sql), or Postgres rejects the generated ON CONFLICT with "no unique or exclusion constraint matching the ON CONFLICT specification". A 3-column target would need a table recreate for zero uniqueness gain.
  • A comment at the entry records this reasoning (and the one condition that would reopen it: pr_hash ceasing to be repo-scoped at the producer), so it isn't silently re-litigated later.

Test plan

  • test/unit/selfhost-pg-dialect.test.ts — new regression test translating the verbatim statement src/ams/ingest.ts issues: no throw, ON CONFLICT (instance_id, pr_hash) DO UPDATE SET, every non-key column upserted (repo_hash, decision, reason_bucket, closed_at, received_at), and both key columns excluded from the SET list
  • Suite green (23/23); local coverage on src/selfhost/pg-dialect.ts: 41/41 lines, 14/14 branches
  • npm run typecheck clean
  • No migration and no src/ams/ingest.ts change needed (see the evaluation above), so db:migrations:check/db:schema-drift:check are unaffected
  • CI validate

Closes #8382

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

Copy link
Copy Markdown
Contributor

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

@codecov

codecov Bot commented Jul 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.56%. Comparing base (d8f706c) to head (94a7e63).
⚠️ Report is 38 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8398      +/-   ##
==========================================
- Coverage   92.12%   89.56%   -2.57%     
==========================================
  Files         782       98     -684     
  Lines       78531    22663   -55868     
  Branches    23720     3872   -19848     
==========================================
- Hits        72347    20298   -52049     
+ Misses       5062     2187    -2875     
+ Partials     1122      178     -944     
Flag Coverage Δ
shard-1 60.97% <ø> (+1.02%) ⬆️
shard-2 51.21% <ø> (+4.00%) ⬆️
shard-3 100.00% <ø> (+46.47%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/selfhost/pg-dialect.ts 100.00% <ø> (ø)

... and 684 files with indirect coverage changes

@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 11:31:02 UTC

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

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This PR adds a single map entry `ams_signals: ["instance_id", "pr_hash"]` to `REPLACE_CONFLICT_KEYS` in pg-dialect.ts, fixing a real bug where every self-host Postgres AMS ingest write threw `no known conflict key`. The chosen 2-column key correctly matches the table's actual `UNIQUE (instance_id, pr_hash)` constraint from migration 0148 (verified in the provided migration file), unlike orb_signals' 3-column key which matches a different constraint shape. The regression test translates the verbatim INSERT statement from ams/ingest.ts and asserts the exact ON CONFLICT target and SET list, which is a real, non-fabricated test of the fixed path.

Nits — 4 non-blocking
  • The inline comment in pg-dialect.ts is quite long (10 lines) relative to the one-line map entry it documents; could be trimmed or moved to a doc reference, though the reasoning is genuinely useful for future maintainers.
  • The regression test hard-codes column names/order matching ams/ingest.ts's INSERT statement; if that statement's column list changes, the test will need manual updating (no shared source of truth), same pattern as other tests in this file so consistent with convention.
  • Consider a short comment-length convention check across REPLACE_CONFLICT_KEYS entries so future additions don't grow entries this large; not necessary here since the reasoning materially prevents a re-litigation risk called out in the PR description.
  • The integration test file (selfhost-pg.test.ts) doesn't appear to test ams_signals against a real Postgres instance the way it does for system_flags — consider adding a real-PG integration case for parity, though the unit test already exercises the translation logic directly.

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 #8382
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: 107 registered-repo PR(s), 58 merged, 4 issue(s).
Contributor context ✅ Confirmed Gittensor contributor kai392; Gittensor profile; 107 PR(s), 4 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The PR adds ams_signals to REPLACE_CONFLICT_KEYS with the correct (instance_id, pr_hash) key matching the existing migration constraint, and explicitly performs and documents the collision-risk evaluation the issue requested (concluding widening is unnecessary since repo is embedded in pr_hash), plus adds a regression test on the verbatim ingest statement.

Review context
  • Author: kai392
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: JavaScript, Kotlin, Perl, Python, TypeScript, Vue
  • Official Gittensor activity: 107 PR(s), 4 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 fab2fce into JSONbored:main Jul 24, 2026
13 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.

fix(selfhost): REPLACE_CONFLICT_KEYS omits ams_signals, crashing AMS telemetry ingest on the self-host Postgres backend

2 participants