Skip to content

fix(db): index decision_ledger_anchors on (row_hash, status) - #9830

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/decision-anchor-rowhash-index-9652-retry
Jul 29, 2026
Merged

fix(db): index decision_ledger_anchors on (row_hash, status)#9830
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
shin-core:fix/decision-anchor-rowhash-index-9652-retry

Conversation

@shin-core

Copy link
Copy Markdown
Contributor

What & why

anchorBackendsMissingForRowHash (#9489, src/review/ledger-anchor-persistence.ts) runs

SELECT DISTINCT backend FROM decision_ledger_anchors WHERE row_hash = ? AND status = 'ok' AND backend IN (...)

once per row being re-anchored, but decision_ledger_anchors had no index on row_hash. So each call was a full table scan whose cost grows with the anchor ledger.

The fix

Add migration 0203 creating:

CREATE INDEX IF NOT EXISTS decision_ledger_anchors_row_hash_status ON decision_ledger_anchors (row_hash, status);

row_hash leads (the equality predicate); status is included so the status = 'ok' filter is served from the index rather than a row fetch. This mirrors the decision_ledger_record_id index added in migrations/0198. The migration contains nothing else — no schema or data change.

Note: the issue names migration 0202, but that number is now taken (0202_orb_signal_rollups.sql), so this uses the next contiguous free number, 0203.

Tests (test/unit/ledger-anchor-persistence.test.ts)

  • After replaying migrations/** (via createTestEnv), the index decision_ledger_anchors_row_hash_status exists, and EXPLAIN QUERY PLAN for the exact anchorBackendsMissingForRowHash predicate SEARCHes via it, not SCANs (fails without the migration).
  • The existing anchorBackendsMissingForRowHash behaviour cases (all-missing / some-present / all-present / different-rowHash) confirm results are unchanged after the migration.

Validation

  • npm run db:migrations:check green (contiguous 0001..0203); npm run db:schema-drift:check green (index on a raw-SQL table, no Drizzle change); npm run typecheck green.
  • migrations/** is outside Codecov's include, so the migration carries no coverage obligation; the test imports createTestEnv, so shards see real src/**.
  • git diff --check <base> HEAD clean; diff is the one migration + its test, no src/** source change.

Closes #9652

`anchorBackendsMissingForRowHash` (JSONbored#9489) runs
`SELECT DISTINCT backend FROM decision_ledger_anchors WHERE row_hash = ? AND status = 'ok' AND backend IN (...)`
once per row being re-anchored, but the table had no index on row_hash — so each
call was a full table scan that grows with the anchor ledger.

Add migration 0203 creating `decision_ledger_anchors_row_hash_status (row_hash, status)`
— row_hash first (the equality predicate), status included so the `status = 'ok'`
filter is served from the index rather than a row fetch. Mirrors the
decision_ledger_record_id index added in 0198. The migration contains nothing else.
(Issue named 0202; that number is now taken, so this is the next contiguous 0203.)

A new test asserts the index exists after replaying migrations and that the real
query SEARCHes via it (not SCAN); the existing anchorBackendsMissingForRowHash
behaviour cases confirm results are unchanged.

Closes JSONbored#9652
@shin-core
shin-core requested a review from JSONbored as a code owner July 29, 2026 12:21
@loopover-orb

loopover-orb Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Warning

⏸️ LoopOver review result - manual review recommended

Review updated: 2026-07-29 12:45:12 UTC

2 files · 1 AI reviewer · no blockers · CI green · clean

⏸️ Suggested Action - Manual Review

Review summary
This PR adds a single migration (0203) creating an index on `decision_ledger_anchors(row_hash, status)` to fix the full-table-scan behind `anchorBackendsMissingForRowHash`, plus a regression test that asserts via `EXPLAIN QUERY PLAN` that the query now SEARCHes via the index rather than SCANs the table. The migration is a plain `CREATE INDEX IF NOT EXISTS`, which is D1-remote-safe (no TEMP, ATTACH, PRAGMA, or explicit transaction control), and the composite column order (row_hash leading, status included) correctly matches the equality/filter shape of the query it targets. The change is narrow, closes linked issue #9652, and the test is a real regression test (fails without the migration per the PR's own claim, and is anchored to the exact predicate the production code runs) rather than a fabricated scenario.

Nits — 3 non-blocking
  • test/unit/ledger-anchor-persistence.test.ts:258 — the `not.toContain("SCAN decision_ledger_anchors ")` string match is brittle to any change in SQLite's EXPLAIN QUERY PLAN wording/formatting across versions; consider asserting on the `detail` containing `SEARCH` instead for a more robust positive assertion.
  • migrations/0203_decision_ledger_anchors_row_hash_index.sql — worth double-checking locally that 0202 is indeed already taken and 0203 is the correct next contiguous number, since `db:migrations:check` passing in CI is the real source of truth here.
  • Nothing structural to change — this is a well-targeted, single-purpose index migration with a real regression test; merge as-is once migration numbering is reconfirmed against the latest default branch.

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 #9652
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 ❌ 5/25 Preflight is holding this PR: the review lane is unavailable, so it is not ready for automated review.
Contributor workload ✅ 10/10 Author activity: 63 registered-repo PR(s), 46 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor shin-core; Gittensor profile; 63 PR(s), 0 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The PR adds a single-purpose migration creating the exact CREATE INDEX IF NOT EXISTS decision_ledger_anchors_row_hash_status ON decision_ledger_anchors (row_hash, status) statement with a header comment naming anchorBackendsMissingForRowHash, #9489, and the migrations/0198 sibling precedent, and adds tests confirming both index existence via sqlite_master and unchanged/behavior-preserving results

Review context
  • Author: shin-core
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is registered but has no active allocation in the current snapshot.
  • Public profile languages: not available
  • Official Gittensor activity: 63 PR(s), 0 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Start here: Await review-lane availability.
  • Then work through the remaining 2 steps in the Signals table above.
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

@superagent-security

Copy link
Copy Markdown
Contributor

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

@loopover-orb loopover-orb Bot added the gittensor:bug Gittensor-scored bug fix — scores a 0.05x multiplier. label Jul 29, 2026
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.59%. Comparing base (9d7f567) to head (874ea68).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #9830   +/-   ##
=======================================
  Coverage   91.59%   91.59%           
=======================================
  Files         915      915           
  Lines      112700   112700           
  Branches    27077    27077           
=======================================
  Hits       103229   103229           
  Misses       8144     8144           
  Partials     1327     1327           
Flag Coverage Δ
backend 95.58% <ø> (ø)

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

@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 94120ef into JSONbored:main Jul 29, 2026
8 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.

db: decision_ledger_anchors has no index on `row_hash

1 participant