Repository navigation
fix(ai): [NSoC_2026] wire abort signal and enforce semaphore VRAM concurrency queue (#1753) - #1790
Merged
GauravKarakoti merged 1 commit intoNov 15, 2026
Conversation
|
@Myparadox-creator is attempting to deploy a commit to the Gaurav's projects Team on Vercel. A member of the Team first needs to authorize it. |
| import { getModelId } from "@/lib/explanation-cache"; | ||
| import { isRateLimitError, isTimeoutError } from "./security-helpers"; | ||
| import { getModelCircuitBreaker, CircuitBreakerError, CircuitState } from "@/ai/resilience"; | ||
| import { getModelCircuitBreaker, CircuitBreakerError, CircuitState, isVramConcurrencyError } from "@/ai/resilience"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Summary of Changes
Addresses issue #1753 where concurrent streaming requests to local LLM models (e.g. Ollama, vLLM) could exhaust GPU VRAM or hold open dangling streaming connections when clients disconnect mid-generation.
Local Model Abort Propagation (
src/ai/local-model.ts):streamLocalModelGenerationandgenerateLocalModelwith directAbortSignallisteners./chat/completionsHTTP request and reader stream, canceling the stream reader immediately upon client disconnect to stop GPU token generation and release VRAM.VRAM Concurrency Semaphore & Failover (
src/ai/resilience.ts):Semaphoresupporting FIFO queuing, timeout thresholds (timeoutMs), capacity resizing (setCapacity), reset cleanup, and cancellation on abort signal.VRAMConcurrencyErrorandgetLocalModelSemaphore()configured byLOCAL_AI_MAX_CONCURRENCY(defaulting to 1).executeWithFallbackAndRetry. When concurrency limits or queue wait deadlines are exceeded, it cleanly fails over to cloud fallback models (e.g. Groq) with failover reason code"vram_concurrency_exceeded".config.signal?.aborted).Fallback Telemetry Reason Tracking (
src/ai/telemetry.ts):FallbackReasonCodetype ("vram_concurrency_exceeded" | "concurrency_limit" | "rate_limit" | "timeout" | "insufficient_memory" | "circuit_breaker_open" | "model_exhausted" | "unknown").recordFailoverEventto persistreasonCodeandreasonin both memory and Redis stores with automatic classification.Security Explanation Model Chain Integration (
src/ai/flows/security-explanation-core.ts):FallbackReasonunion to include"vram_concurrency_exceeded".runModelChainto catchisVramConcurrencyError(error)and cleanly switch to fallback models.SSE Stream Route Abort Wiring (
src/app/api/findings/[id]/explain-stream/route.ts):request.signal) andstreamManagerabort signals directly into the SSE stream lifecycle to guarantee client disconnects immediately terminate server-side generation.Changeset:
.changeset/local-model-vram-abort-1753.md.Verification & Tests Passed
src/ai/local-model.test.ts(42 passed)src/ai/resilience.test.ts(27 passed)src/ai/telemetry.test.ts(10 passed)src/app/api/findings/[id]/explain-stream/route.test.ts(11 passed)src/ai/flows/heist-message-stream.test.ts(16 passed)