Skip to content

feat(miner-governor): local rate-limit token-bucket + jittered-backoff calculator (pure) - #2416

Merged
JSONbored merged 1 commit into
JSONbored:mainfrom
GildardoDev:feat/governor-rate-limit
Jul 2, 2026
Merged

feat(miner-governor): local rate-limit token-bucket + jittered-backoff calculator (pure)#2416
JSONbored merged 1 commit into
JSONbored:mainfrom
GildardoDev:feat/governor-rate-limit

Conversation

@GildardoDev

Copy link
Copy Markdown
Contributor

Summary

Adds the Governor's pure rate-limit + jittered-backoff math to the shared engine package as packages/gittensory-engine/src/governor/rate-limit.ts. Given a rolling-window bucket and a clock reading it decides whether an event is allowed and, when blocked, how long to wait; and it computes a jittered exponential backoff from an injected random source. It computes numbers only — no storage, no scheduling, and it does not itself gate any write action (that enforcement wiring is a separate, maintainer-owned concern).

Closes #2327

Deliverables

  • LocalRateBucket / LocalRateLimitConfig / LocalRateLimitDecision types, and evaluateLocalRateLimit(bucket, config, nowMs) returning { allowed, remaining, resetAtMs, retryAfterMs }. A fully elapsed window is treated as reset, so a stale bucket at its limit is permitted again in the new window.
  • jitteredBackoffMs(baseMs, attempt, randomFn) — exponential base baseMs * 2 ** attempt scaled by a multiplicative jitter factor in the band [0.5, 1.5) drawn from an injected randomFn (never Math.random(), so tests stay deterministic). The attempt is clamped non-negative and bounded so it cannot overflow to Infinity, and an out-of-contract random draw is clamped into the band.
  • The vocabulary mirrors the server-side RateLimitConfig / RateLimitDecision in src/auth/rate-limit.ts (that one is a Cloudflare Durable Object and is not reusable in the fully-local miner); this variant is millisecond-based and state-free.
  • A doc comment stating explicitly that the module computes numbers only and gates nothing.
  • Exported from the package barrel (packages/gittensory-engine/src/index.ts).

Scope

Validation

  • Unit tests (14) cover: under-limit passes, at-limit blocks with retry timing, elapsed-window reset, sub-window boundary, negative/over-limit stored counts; backoff at attempt 0 and growth, the [0.5, 1.5) jitter band across a swept attempt range, negative/fractional attempt clamping, the exponent cap against overflow, a negative base, and out-of-contract random clamping.
  • npm run build --workspace @jsonbored/gittensory-engine succeeds.
  • Tests run green under the root Vitest suite (test/unit/governor-rate-limit.test.ts).

@GildardoDev
GildardoDev requested a review from JSONbored as a code owner July 1, 2026 20:18
@dosubot dosubot Bot added the size:M label Jul 1, 2026
@loopover-orb

loopover-orb Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Warning

🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨🟨

⏸️ Gittensory review result - manual review recommended

Review updated: 2026-07-02 05:14:15 UTC

3 files · 1 AI reviewer · no blockers · readiness 55/100 · CI green · clean

⏸️ Suggested Action - Manual Review

Review summary
This PR adds the requested pure local rate-limit decision helper and deterministic jittered backoff utility, exports them from the engine barrel, and covers the main boundary cases with focused unit tests. The core bucket math is coherent for normal positive windows, and the backoff path correctly avoids `Math.random()` and clamps numeric outputs. The only notable weakness is around invalid or future-skewed timing inputs, where the documented “always reset” behavior for non-positive windows is narrower than the implementation actually guarantees.

Nits — 5 non-blocking
  • nit: packages/gittensory-engine/src/governor/rate-limit.ts:63 treats `windowMs <= 0` as elapsed only when `nowMs >= windowStartMs`; if the stored bucket start is in the future, the documented/tested “always-reset” behavior no longer holds, so either clamp future starts or tighten the comment/test to the intended contract.
  • nit: packages/gittensory-engine/src/governor/rate-limit.ts:91 lets `randomFn()` exceptions propagate even though the surrounding docs emphasize the function always returns a non-negative integer for any numeric input; that is acceptable if a throwing RNG is out of contract, but the doc should make that boundary explicit.
  • packages/gittensory-engine/src/governor/rate-limit.ts:63 could make the zero-window contract explicit with `const windowElapsed = windowMs === 0 || now - windowStartMs >= windowMs;` and add a test where `windowStartMs > nowMs`.
  • packages/gittensory-engine/src/governor/rate-limit.ts:91 should either document that `randomFn` must not throw or wrap it and treat failures like a non-finite draw if the intended contract is truly total.
  • Readiness score is below the configured threshold — Use the readiness panel as advisory maintainer context; the score does not block this PR.
Signal Result Evidence
Code review ✅ No blockers 1 reviewer
Linked issue ✅ Linked #2327
Related work ⚠️ 3 scoped overlaps Top overlaps are listed below; lower-confidence bulk is hidden.
Change scope ❌ 8/20 High review scope from cached public metadata (size label size:L; 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: 226 registered-repo PR(s), 168 merged, 0 issue(s).
Contributor context ✅ Confirmed Gittensor contributor GildardoDev; Gittensor profile; 226 PR(s), 0 issue(s).
Gate result ✅ Passing No configured blocker found.
Review context
Contributor next steps
  • Review top overlaps.
  • Add a concise scope and risk note.
  • Await review-lane availability.
  • Triage stale or unlinked PRs.
  • Refresh registry data or choose a registered active repo.
  • Check active issues and PRs before submitting.
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.

🟩 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 Gittensory, a quiet PR intelligence layer for OSS maintainers.

  • Re-run Gittensory review

@loopover-orb loopover-orb Bot added gittensor gittensor:feature Gittensor-scored feature linked to a feature issue — scores a 0.25x multiplier. labels Jul 1, 2026
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.93%. Comparing base (5e9fa39) to head (60f7ffd).
⚠️ Report is 14 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2416   +/-   ##
=======================================
  Coverage   95.93%   95.93%           
=======================================
  Files         225      226    +1     
  Lines       25337    25359   +22     
  Branches     9218     9228   +10     
=======================================
+ Hits        24307    24329   +22     
  Misses        417      417           
  Partials      613      613           
Files with missing lines Coverage Δ
...kages/gittensory-engine/src/governor/rate-limit.ts 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from cec8a61 to b52c154 Compare July 1, 2026 20:23
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from b52c154 to 04e6b13 Compare July 1, 2026 20:45
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from 04e6b13 to d0d0a9f Compare July 1, 2026 21:02
@GildardoDev
GildardoDev marked this pull request as draft July 1, 2026 21:08
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from d0d0a9f to 22e1256 Compare July 1, 2026 21:12
@GildardoDev
GildardoDev marked this pull request as ready for review July 1, 2026 21:12
@dosubot dosubot Bot added size:L and removed size:M labels Jul 1, 2026
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from 22e1256 to 9f06ab0 Compare July 1, 2026 21:24
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from 9f06ab0 to f8a4506 Compare July 1, 2026 21:34
@GildardoDev
GildardoDev force-pushed the feat/governor-rate-limit branch from def4940 to e973f2a Compare July 1, 2026 22:11
@GildardoDev
GildardoDev marked this pull request as draft July 1, 2026 22:15
@GildardoDev
GildardoDev marked this pull request as ready for review July 1, 2026 22:16
@GildardoDev
GildardoDev marked this pull request as draft July 1, 2026 22:19
@GildardoDev
GildardoDev marked this pull request as ready for review July 1, 2026 22:20
@dosubot dosubot Bot added the lgtm label Jul 2, 2026
@JSONbored
JSONbored merged commit 8b64bf3 into JSONbored:main Jul 2, 2026
10 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in gittensory - v1 roadmap Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gittensor:feature Gittensor-scored feature linked to a feature issue — scores a 0.25x multiplier.

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

feat(miner-governor): local rate-limit token-bucket + jittered-backoff calculator (pure)

2 participants