Repository navigation
fix(routing): correct capacity-rate window accounting - #527
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This PR contains only comment/documentation rewording of internal capacity-rate tracking logic; no security-relevant code paths, trust boundary enforcement, or cryptographic operations are modified. Trust Boundaries Touched
Threat Assessment
New Attack SurfaceNone introduced. The diff is three comment blocks describing internal denominator-accounting semantics; no control flow, data handling, or security-relevant logic changed. SEC-* Findings ResolvedNone. No open findings are addressed by this PR. 🔐 Threat model: |
ethenotethan
left a comment
There was a problem hiding this comment.
Automated Code Review — Layr-Labs/d-inference#
Verdict: REQUEST_CHANGES
Security — ✅ No issues found
Performance — 2 finding(s) (2 blocking)
- 🟡 [MEDIUM]
coordinator/registry/capacity_rate.go:89-110— Map growth without bounded cleanup on hot path- Suggestion: Move sweepCapacityRateMapLocked call outside the !existed condition to ensure cleanup runs on every reject, not just new keys
- 🟡 [MEDIUM]
coordinator/registry/capacity_rate.go:125-130— Map growth without bounded cleanup on hot path- Suggestion: Move sweepCapacityRateMapLocked call outside the !existed condition to ensure cleanup runs on every accept, not just new keys
Type_diligence — ✅ No issues found
Additive_complexity — ✅ No issues found
2 finding(s) total, 2 blocking. Verdict: REQUEST_CHANGES.
🤖 Automated review by Centaur · DAR-186
| if r.capacityRateCfg.PenaltyMs <= 0 { | ||
| return | ||
| } | ||
| sweepCapacityRateMapLocked(r.capacityRateRejects, now) | ||
| _, existed := r.capacityRateRejects[key] | ||
| r.capacityRateRejects[key] = appendWindowedOutcome(r.capacityRateRejects[key], now) | ||
| if !existed { | ||
| sweepCapacityRateMapLocked(r.capacityRateRejects, now) | ||
| } | ||
|
|
||
| // A pair may have gone quiet long enough for its accept-only history to | ||
| // expire before this first/new reject. Prune it now so repeated routing reads | ||
| // do not keep scanning stale timestamps and the first rate uses only the same | ||
| // five-minute window as the reject side. | ||
| if accepts, ok := r.capacityRateAccepts[key]; ok { | ||
| accepts = pruneWindowedOutcomes(accepts, now) | ||
| if len(accepts) == 0 { | ||
| delete(r.capacityRateAccepts, key) | ||
| } else { | ||
| r.capacityRateAccepts[key] = accepts | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
🟡 [MEDIUM] ⚡ Map growth without bounded cleanup on hot path
💡 Suggestion: Move sweepCapacityRateMapLocked call outside the !existed condition to ensure cleanup runs on every reject, not just new keys
📊 Score: 3×4 = 12 · Category: unbounded_allocations
| r.capacityRateAccepts[key] = appendWindowedOutcome(r.capacityRateAccepts[key], now) | ||
| if !existed { | ||
| sweepCapacityRateMapLocked(r.capacityRateAccepts, now) | ||
| } | ||
| return true | ||
| } |
There was a problem hiding this comment.
🟡 [MEDIUM] ⚡ Map growth without bounded cleanup on hot path
💡 Suggestion: Move sweepCapacityRateMapLocked call outside the !existed condition to ensure cleanup runs on every accept, not just new keys
📊 Score: 3×4 = 12 · Category: unbounded_allocations
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e2b96982cc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // skip a redundant read-lock probe. Completion calls that already counted | ||
| // their rate outcome retain the old healthy fast path: use the read lock to | ||
| // avoid write acquisition when there is no cooldown/clamp state to clear. | ||
| shouldRecordRate := countRateOutcome && r.capacityRateCfg.PenaltyMs > 0 |
There was a problem hiding this comment.
Avoid serializing first-token accepts on r.mu
With the default rate tracker enabled, every successful request reaches RecordCapacityAccept(..., true) from the first-content paths before the first chunk is written. This makes shouldRecordRate true even for pairs with no reject history, so the old healthy fast path is skipped and the function takes the registry-wide write lock below; concurrent first tokens, ReserveProviderEx, and heartbeat updates all serialize on r.mu. On healthy high-QPS traffic this can inflate TTFT and reduce routing throughput just to append dormant accept history, so consider a sharded/per-pair recording path or another way to avoid the global write lock for reject-free pairs.
Useful? React with 👍 / 👎.
| r.capacityRateAccepts[key] = appendWindowedOutcome(r.capacityRateAccepts[key], now) | ||
| if !existed { | ||
| sweepCapacityRateMapLocked(r.capacityRateAccepts, now) | ||
| } | ||
| return true |
There was a problem hiding this comment.
Refresh long-stream accepts at completion
When a stream emits first content and then runs longer than capacityRateWindow, this unconditional append/true return causes the API to mark RateOutcomeCounted at commit time. If capacity rejects arrive near the end of that long stream, the commit timestamp has already aged out and noteInferenceSuccess/parked completion will not re-offer the served request, so the active stream disappears from the denominator and the pair can be derated as reject-only despite serving. Keep a completion-time refresh path when the commit-time accept is outside the window.
Useful? React with 👍 / 👎.

Summary
Follow-up to #523. This corrects two capacity-rate accounting defects before the v0.7.5 coordinator stack is merged:
The same chronological merge helper is applied to inference-error and capacity-cooldown strike histories because they had the identical migration and tail-sweep invariant.
Before
flowchart LR subgraph Behavior["Behavior before"] A[Recent successful dispatches] --> B[Accept history discarded] C[First capacity 503 burst] --> D[Reject-only rate] D --> E[False high routing penalty] F[Identity rebind with destination history] --> G[Older source appended last] G --> H[Large-map sweep can delete fresh state] end subgraph Code["Code before"] I[RecordCapacityAcceptOutcome] --> J{Reject already present} J -- No --> K[Return without rate outcome] J -- Yes --> L[Append accept] M[migrateFaultStateLocked] --> N[Append destination and source] N --> O[Sweep trusts final timestamp] endAfter
flowchart LR subgraph Behavior["Behavior after"] A[Every served dispatch] --> B[Retained for five-minute window] C[Capacity 503 burst] --> D[Rejects divided by all recent outcomes] D --> E[Penalty only above sample and rate thresholds] F[Identity rebind] --> G[Histories merged oldest to newest] G --> H[Large-map sweep preserves fresh state] end subgraph Code["Code after"] I[RecordCapacityAcceptOutcome] --> J[Append and reslice accept history] K[recordCapacityRateRejectLocked] --> L[Prune accepts and append reject] J --> M[Binary-search window counts] L --> M N[migrateFaultStateLocked] --> O[mergeChronologicalTimestamps] O --> P[Newest timestamp remains at tail] endRegression coverage
8/108, not8/8.The two principal regressions were verified failing against
3220bd9ebefore the implementation.Verification
go test ./... -race -count=1(28 packages)go vet ./...go build -o /tmp/d-inference-coordinator-capacity-rate-fast-follow ./coordinator/cmd/coordinatorgofmt -lon every changed Go filegit diff --checkThe race suite emitted only the recurring macOS
LC_DYSYMTABlinker warnings and exited successfully.Rollout
Merge this before rebasing and completing #524. Per the v0.7.5 release policy, merging this PR does not authorize a partial coordinator deployment; it becomes part of the single complete coordinator release candidate.
Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.