-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat(core): add modelAvailabilityService for managing and tracking model health #13426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
packages/core/src/availability/modelAvailabilityService.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it, vi, beforeEach } from 'vitest'; | ||
| import { ModelAvailabilityService } from './modelAvailabilityService.js'; | ||
|
|
||
| describe('ModelAvailabilityService', () => { | ||
| let service: ModelAvailabilityService; | ||
| const model = 'test-model'; | ||
|
|
||
| beforeEach(() => { | ||
| service = new ModelAvailabilityService(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('returns available snapshot when no state recorded', () => { | ||
| expect(service.snapshot(model)).toEqual({ available: true }); | ||
| }); | ||
|
|
||
| it('tracks retry-once-per-turn failures', () => { | ||
| service.markRetryOncePerTurn(model); | ||
| expect(service.snapshot(model)).toEqual({ available: true }); | ||
|
|
||
| service.consumeStickyAttempt(model); | ||
| expect(service.snapshot(model)).toEqual({ | ||
| available: false, | ||
| reason: 'retry_once_per_turn', | ||
| }); | ||
|
|
||
| service.resetTurn(); | ||
| expect(service.snapshot(model)).toEqual({ available: true }); | ||
| }); | ||
|
|
||
| it('tracks terminal failures', () => { | ||
| service.markTerminal(model, 'quota'); | ||
| expect(service.snapshot(model)).toEqual({ | ||
| available: false, | ||
| reason: 'quota', | ||
| }); | ||
| }); | ||
|
|
||
| it('does not override terminal failure with sticky failure', () => { | ||
| service.markTerminal(model, 'quota'); | ||
| service.markRetryOncePerTurn(model); | ||
| expect(service.snapshot(model)).toEqual({ | ||
| available: false, | ||
| reason: 'quota', | ||
| }); | ||
| }); | ||
|
|
||
| it('selects models respecting terminal and sticky states', () => { | ||
| const stickyModel = 'stick-model'; | ||
| const healthyModel = 'healthy-model'; | ||
|
|
||
| service.markTerminal(model, 'capacity'); | ||
| service.markRetryOncePerTurn(stickyModel); | ||
|
|
||
| const first = service.selectFirstAvailable([ | ||
| model, | ||
| stickyModel, | ||
| healthyModel, | ||
| ]); | ||
| expect(first).toEqual({ | ||
| selected: stickyModel, | ||
| attempts: 1, | ||
| skipped: [ | ||
| { | ||
| model, | ||
| reason: 'capacity', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| service.consumeStickyAttempt(stickyModel); | ||
| const second = service.selectFirstAvailable([ | ||
| model, | ||
| stickyModel, | ||
| healthyModel, | ||
| ]); | ||
| expect(second).toEqual({ | ||
| selected: healthyModel, | ||
| skipped: [ | ||
| { | ||
| model, | ||
| reason: 'capacity', | ||
| }, | ||
| { | ||
| model: stickyModel, | ||
| reason: 'retry_once_per_turn', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| service.resetTurn(); | ||
| const third = service.selectFirstAvailable([ | ||
| model, | ||
| stickyModel, | ||
| healthyModel, | ||
| ]); | ||
| expect(third).toEqual({ | ||
| selected: stickyModel, | ||
| attempts: 1, | ||
| skipped: [ | ||
| { | ||
| model, | ||
| reason: 'capacity', | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it('preserves consumed state when marking retry-once-per-turn again', () => { | ||
| service.markRetryOncePerTurn(model); | ||
| service.consumeStickyAttempt(model); | ||
|
|
||
| // It is currently consumed | ||
| expect(service.snapshot(model).available).toBe(false); | ||
|
|
||
| // Marking it again should not reset the consumed flag | ||
| service.markRetryOncePerTurn(model); | ||
| expect(service.snapshot(model).available).toBe(false); | ||
| }); | ||
|
|
||
| it('clears consumed state when marked healthy', () => { | ||
| service.markRetryOncePerTurn(model); | ||
| service.consumeStickyAttempt(model); | ||
| expect(service.snapshot(model).available).toBe(false); | ||
|
|
||
| service.markHealthy(model); | ||
| expect(service.snapshot(model).available).toBe(true); | ||
|
|
||
| // If we mark it sticky again, it should be fresh (not consumed) | ||
| service.markRetryOncePerTurn(model); | ||
| expect(service.snapshot(model).available).toBe(true); | ||
| }); | ||
|
|
||
| it('resetTurn resets consumed state for multiple sticky models', () => { | ||
| const model2 = 'model-2'; | ||
| service.markRetryOncePerTurn(model); | ||
| service.markRetryOncePerTurn(model2); | ||
|
|
||
| service.consumeStickyAttempt(model); | ||
| service.consumeStickyAttempt(model2); | ||
|
|
||
| expect(service.snapshot(model).available).toBe(false); | ||
| expect(service.snapshot(model2).available).toBe(false); | ||
|
|
||
| service.resetTurn(); | ||
|
|
||
| expect(service.snapshot(model).available).toBe(true); | ||
| expect(service.snapshot(model2).available).toBe(true); | ||
| }); | ||
|
|
||
| it('resetTurn does not affect terminal models', () => { | ||
| service.markTerminal(model, 'quota'); | ||
| service.resetTurn(); | ||
| expect(service.snapshot(model)).toEqual({ | ||
| available: false, | ||
| reason: 'quota', | ||
| }); | ||
| }); | ||
| }); |
131 changes: 131 additions & 0 deletions
131
packages/core/src/availability/modelAvailabilityService.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| export type ModelId = string; | ||
|
|
||
| export type TerminalUnavailabilityReason = 'quota' | 'capacity'; | ||
| export type TurnUnavailabilityReason = 'retry_once_per_turn'; | ||
|
|
||
| export type UnavailabilityReason = | ||
| | TerminalUnavailabilityReason | ||
| | TurnUnavailabilityReason | ||
| | 'unknown'; | ||
|
|
||
| type HealthState = | ||
| | { status: 'terminal'; reason: TerminalUnavailabilityReason } | ||
| | { | ||
| status: 'sticky_retry'; | ||
| reason: TurnUnavailabilityReason; | ||
| consumed: boolean; | ||
| }; | ||
|
|
||
| export interface ModelAvailabilitySnapshot { | ||
| available: boolean; | ||
| reason?: UnavailabilityReason; | ||
| } | ||
|
|
||
| export interface ModelSelectionResult { | ||
| selected: ModelId | null; | ||
| attempts?: number; | ||
| skipped: Array<{ | ||
| model: ModelId; | ||
| reason: UnavailabilityReason; | ||
| }>; | ||
| } | ||
|
|
||
| export class ModelAvailabilityService { | ||
| private readonly health = new Map<ModelId, HealthState>(); | ||
|
adamfweidman marked this conversation as resolved.
|
||
|
|
||
| markTerminal(model: ModelId, reason: TerminalUnavailabilityReason) { | ||
| this.setState(model, { | ||
| status: 'terminal', | ||
| reason, | ||
| }); | ||
| } | ||
|
|
||
| markHealthy(model: ModelId) { | ||
| this.clearState(model); | ||
| } | ||
|
|
||
| markRetryOncePerTurn(model: ModelId) { | ||
| const currentState = this.health.get(model); | ||
| // Do not override a terminal failure with a transient one. | ||
| if (currentState?.status === 'terminal') { | ||
| return; | ||
| } | ||
|
|
||
| // Only reset consumption if we are not already in the sticky_retry state. | ||
| // This prevents infinite loops if the model fails repeatedly in the same turn. | ||
| let consumed = false; | ||
| if (currentState?.status === 'sticky_retry') { | ||
| consumed = currentState.consumed; | ||
| } | ||
|
|
||
| this.setState(model, { | ||
| status: 'sticky_retry', | ||
| reason: 'retry_once_per_turn', | ||
| consumed, | ||
| }); | ||
| } | ||
|
|
||
| consumeStickyAttempt(model: ModelId) { | ||
| const state = this.health.get(model); | ||
| if (state?.status === 'sticky_retry') { | ||
| this.setState(model, { ...state, consumed: true }); | ||
| } | ||
| } | ||
|
|
||
| snapshot(model: ModelId): ModelAvailabilitySnapshot { | ||
| const state = this.health.get(model); | ||
|
adamfweidman marked this conversation as resolved.
|
||
|
|
||
| if (!state) { | ||
| return { available: true }; | ||
| } | ||
|
|
||
| if (state.status === 'terminal') { | ||
| return { available: false, reason: state.reason }; | ||
| } | ||
|
|
||
| if (state.status === 'sticky_retry' && state.consumed) { | ||
| return { available: false, reason: state.reason }; | ||
| } | ||
|
|
||
| return { available: true }; | ||
| } | ||
|
|
||
| selectFirstAvailable(models: ModelId[]): ModelSelectionResult { | ||
| const skipped: ModelSelectionResult['skipped'] = []; | ||
|
|
||
| for (const model of models) { | ||
| const snapshot = this.snapshot(model); | ||
| if (snapshot.available) { | ||
| const state = this.health.get(model); | ||
| // A sticky model is being attempted, so note that. | ||
| const attempts = state?.status === 'sticky_retry' ? 1 : undefined; | ||
|
adamfweidman marked this conversation as resolved.
|
||
| return { selected: model, skipped, attempts }; | ||
| } else { | ||
| skipped.push({ model, reason: snapshot.reason ?? 'unknown' }); | ||
| } | ||
| } | ||
| return { selected: null, skipped }; | ||
| } | ||
|
|
||
| resetTurn() { | ||
| for (const [model, state] of this.health.entries()) { | ||
| if (state.status === 'sticky_retry') { | ||
| this.setState(model, { ...state, consumed: false }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private setState(model: ModelId, nextState: HealthState) { | ||
| this.health.set(model, nextState); | ||
| } | ||
|
|
||
| private clearState(model: ModelId) { | ||
| this.health.delete(model); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.