Skip to content

fix(miner): prime runLoop's ledger cursor with an indexed MAX(seq) lookup - #10259

Merged
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
bitfathers94:fix/issue-10008
Jul 31, 2026
Merged

fix(miner): prime runLoop's ledger cursor with an indexed MAX(seq) lookup#10259
loopover-orb[bot] merged 1 commit into
JSONbored:mainfrom
bitfathers94:fix/issue-10008

Conversation

@bitfathers94

Copy link
Copy Markdown
Contributor

fix(miner): prime runLoop's ledger cursor with an indexed MAX(seq) lookup

Add EventLedger.latestSeq(), backed by a COALESCE(MAX(seq), 0) statement prepared at
open time, and have runLoop read it instead of loading and JSON-parsing every row in
the ledger just to take the last one's seq.

Closes #10008

…okup

Add EventLedger.latestSeq(), backed by a COALESCE(MAX(seq), 0) statement prepared at
open time, and have runLoop read it instead of loading and JSON-parsing every row in
the ledger just to take the last one's seq.
@bitfathers94
bitfathers94 requested a review from JSONbored as a code owner July 31, 2026 14:45
@loopover-orb

loopover-orb Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Tip

✅ LoopOver review result - approve/merge recommended

Review updated: 2026-07-31 14:52:02 UTC

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

✅ Suggested Action - Approve/Merge

  • safe to merge

Review summary
This PR replaces runLoop's O(n) `readEvents({}).at(-1)?.seq` cursor priming with a new `latestSeq()` method backed by a dedicated `COALESCE(MAX(seq), 0)` prepared statement, avoiding a full table scan and JSON.parse of every row just to get the last seq. The change is correct: `latestSeq()` mirrors the existing `nextSeqStatement` pattern, is properly wired through the EventLedger interface, default-singleton export, and all mock/stub call sites in the test suite (miner-mcp-contract.test.ts, miner-loop-cli.test.ts). Tests cover the new method directly (empty ledger, after appends, singleton helper) and pin the regression via a spy that asserts `readEvents` is never called during cursor priming.

Nits — 4 non-blocking
  • nit: event-ledger.ts's `latestSeqStatement` and `nextSeqStatement` both scan the same MAX(seq) — consider whether a covering index note is worth adding since seq is UNIQUE (likely already indexed), though this is a micro-perf point given SQLite already indexes UNIQUE columns.
  • nit: the new `latestSeq()` on the EventLedger type isn't documented with a one-line comment describing its distinction from `nextSeq` (used internally for appends) versus this public read-only cursor value, though the inline comment above the implementation covers this adequately.
  • loop-cli.ts:342 — the fix is correctly scoped to the exact line flagged in issue miner(loop): runLoop reads and JSON-parses the entire event ledger to obtain one seq number #10008; no further changes needed there.
  • event-ledger.ts:238-241 — consider whether `latestSeq()` and the internal `nextSeqStatement` could share one prepared statement (nextSeq - 1) to avoid maintaining two near-identical SQL strings, though this is a minor DRY nit not worth blocking on.

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 #10008
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 ⚠️ 20/25 Preflight is ready, but the PR body does not name the validation run.
Contributor workload ✅ 10/10 Author activity: 140 registered-repo PR(s), 99 merged, 3 issue(s).
Contributor context ✅ Confirmed Gittensor contributor bitfathers94; Gittensor profile; 140 PR(s), 3 issue(s).
Improvement ✅ Minor risk: clean · value: minor · LLM: moderate
Linked issue satisfaction

Addressed
The PR adds EventLedger.latestSeq() backed by a COALESCE(MAX(seq),0) statement prepared at open time next to nextSeqStatement, exposes it on the EventLedger type and as a module-level export, updates runLoop to use it instead of readEvents({}), and updates all injected test doubles (loop-cli, mcp-contract tests) plus adds dedicated unit tests verifying 0-on-empty and equality with the appended seq

Review context
  • Author: bitfathers94
  • Role context: outside_contributor
  • Public audience mode: oss maintainer
  • Lane context: Repository is configured for direct PR review.
  • Public profile languages: TypeScript
  • Official Gittensor activity: 140 PR(s), 3 issue(s).
  • PR-specific overlap: none found.
Contributor next steps
  • Start here: Add validation command/output.
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 <question> answers contribution-quality Q&A with source citations and freshness.
  • @loopover chat <question> 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.

1 similar comment
@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 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.71%. Comparing base (e3f7b45) to head (01ebc1b).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10259      +/-   ##
==========================================
+ Coverage   80.60%   80.71%   +0.10%     
==========================================
  Files         283      285       +2     
  Lines       59101    59434     +333     
  Branches     7000     7105     +105     
==========================================
+ Hits        47641    47974     +333     
  Misses      11167    11167              
  Partials      293      293              
Flag Coverage Δ
backend 100.00% <100.00%> (?)

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

Files with missing lines Coverage Δ
packages/loopover-miner/lib/event-ledger.ts 100.00% <100.00%> (ø)
packages/loopover-miner/lib/loop-cli.ts 100.00% <100.00%> (ø)

@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 efc2c7e into JSONbored:main Jul 31, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 31, 2026
7 tasks
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.

miner(loop): runLoop reads and JSON-parses the entire event ledger to obtain one seq number

1 participant