File size: 4,492 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

export type ModelId = string;

type TerminalUnavailabilityReason = 'quota' | 'capacity';
export type TurnUnavailabilityReason = 'retry_once_per_turn';

export type UnavailabilityReason =
  | TerminalUnavailabilityReason
  | TurnUnavailabilityReason
  | 'unknown';

export type ModelHealthStatus = 'terminal' | 'sticky_retry';

type HealthState =
  | {
      status: 'terminal';
      reason: TerminalUnavailabilityReason;
      markedAt?: number;
    }
  | {
      status: 'sticky_retry';
      reason: TurnUnavailabilityReason;
      consumed: boolean;
      attempts: number;
    };

export interface ModelAvailabilitySnapshot {
  available: boolean;
  reason?: UnavailabilityReason;
}

export interface ModelSelectionResult {
  selectedModel: ModelId | null;
  attempts?: number;
  skipped: Array<{
    model: ModelId;
    reason: UnavailabilityReason;
  }>;
}

import { normalizeModelId } from '../utils/modelUtils.js';

export class ModelAvailabilityService {
  private readonly health = new Map<ModelId, HealthState>();

  private getHealth(
    model: ModelId,
    ttlMs: number = 30000,
  ): HealthState | undefined {
    const state = this.health.get(model);
    if (
      state &&
      state.status === 'terminal' &&
      state.reason === 'capacity' &&
      state.markedAt !== undefined
    ) {
      const elapsed = Date.now() - state.markedAt;
      if (elapsed >= ttlMs) {
        this.clearState(model);
        return undefined;
      }
    }
    return state;
  }

  markTerminal(modelId: ModelId, reason: TerminalUnavailabilityReason) {
    const model = normalizeModelId(modelId);
    this.setState(model, {
      status: 'terminal',
      reason,
      markedAt: Date.now(),
    });
  }

  markHealthy(modelId: ModelId) {
    const model = normalizeModelId(modelId);
    this.clearState(model);
  }

  markRetryOncePerTurn(modelId: ModelId, attempts: number = 1) {
    const model = normalizeModelId(modelId);
    const currentState = this.getHealth(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,
      attempts,
    });
  }

  consumeStickyAttempt(modelId: ModelId) {
    const model = normalizeModelId(modelId);
    const state = this.getHealth(model);
    if (state?.status === 'sticky_retry') {
      this.setState(model, { ...state, consumed: true });
    }
  }

  snapshot(modelId: ModelId, ttlMs: number = 30000): ModelAvailabilitySnapshot {
    const model = normalizeModelId(modelId);
    const state = this.getHealth(model, ttlMs);

    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(modelIds: ModelId[]): ModelSelectionResult {
    const skipped: ModelSelectionResult['skipped'] = [];

    for (const modelId of modelIds) {
      const model = normalizeModelId(modelId);
      const snapshot = this.snapshot(model);
      if (snapshot.available) {
        const state = this.getHealth(model);
        // A sticky model is being attempted, so note that.
        const attempts =
          state?.status === 'sticky_retry' ? state.attempts : undefined;
        return { selectedModel: model, skipped, attempts };
      } else {
        skipped.push({ model, reason: snapshot.reason ?? 'unknown' });
      }
    }
    return { selectedModel: null, skipped };
  }

  resetTurn() {
    for (const [model, state] of this.health.entries()) {
      if (state.status === 'sticky_retry') {
        this.setState(model, { ...state, consumed: false });
      }
    }
  }

  reset() {
    this.health.clear();
  }

  private setState(model: ModelId, nextState: HealthState) {
    this.health.set(model, nextState);
  }

  private clearState(model: ModelId) {
    this.health.delete(model);
  }
}