forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromotion_flex.py
More file actions
291 lines (242 loc) · 10 KB
/
Copy pathpromotion_flex.py
File metadata and controls
291 lines (242 loc) · 10 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""One live switch for bounded OpenAI Flex routing in scheduled background work.
Once the owning jobs are deployed capable, operators can move every eligible
scheduled call between Flex and the legacy Standard path without a redeploy by
updating the single environment-scoped Firestore document for that stage.
The dev and prod jobs can share the customer Firestore project, so the control
document is deliberately stage-scoped. A missing, unsupported, or malformed
``OMI_ENV_STAGE`` fails closed to Standard rather than risking a cross-stage
toggle.
"""
from __future__ import annotations
import logging
import os
import time
from dataclasses import dataclass
from typing import Any, Callable, Optional, cast
from utils.llm.clients import feature_auto_lane_id, get_or_create_omi_gateway_llm
from utils.llm.prompt_cache import EXPLICIT_CACHE_OPTIONS
logger = logging.getLogger(__name__)
BACKGROUND_FLEX_CAPABLE_ENV = "OMI_BACKGROUND_FLEX_CAPABLE"
BACKGROUND_FLEX_CONTROL_COLLECTION = "llm_runtime_controls"
BACKGROUND_FLEX_CONTROL_DOCUMENT_PREFIX = "background_flex"
BACKGROUND_FLEX_TIMEOUT_SECONDS = 900.0
BACKGROUND_FLEX_LEASE_SECONDS = 1_200
BACKGROUND_FLEX_JOB_BUDGET_SECONDS = 3_600.0
BACKGROUND_FLEX_JOB_SAFETY_SECONDS = 300.0
# The maintenance cron still imports this lease constant while its call sites
# migrate to the shared Flex router.
MEMORY_PROMOTION_FLEX_LEASE_SECONDS = BACKGROUND_FLEX_LEASE_SECONDS
_TRANSIENT_FLEX_STATUS_CODES = frozenset({408, 409, 429, 500, 502, 503, 504})
_TRANSIENT_FLEX_EXCEPTION_NAMES = frozenset(
{"APIConnectionError", "APITimeoutError", "InternalServerError", "RateLimitError"}
)
CONSOLIDATION_FLEX_CACHE_KEY = "omi-canonical-consolidation-v1"
def _get_gateway_flex_llm(feature: str, *, request_timeout: float) -> Any:
llm = get_or_create_omi_gateway_llm(
feature_auto_lane_id(feature),
options={"request_timeout": request_timeout, "max_retries": 0},
feature=feature,
)
if feature != "memory_conflict_flex":
return llm
return llm.bind(
prompt_cache_key=CONSOLIDATION_FLEX_CACHE_KEY,
extra_body={"prompt_cache_options": EXPLICIT_CACHE_OPTIONS},
)
class PromotionFlexDeferred(RuntimeError):
"""Flex could not serve now; durable scheduled work should retry later."""
class PromotionFlexControlChanged(PromotionFlexDeferred):
"""The shared live control changed while a Flex result was in flight."""
@dataclass(frozen=True)
class PromotionFlexControl:
enabled: bool = False
generation: int = 0
def promotion_flex_capable() -> bool:
return os.getenv(BACKGROUND_FLEX_CAPABLE_ENV, "false").strip().lower() in {"1", "true", "yes"}
def background_flex_control_path() -> Optional[str]:
"""Return the strict stage-scoped control document path.
Dev and prod intentionally use separate Firestore documents because their
jobs may read the same customer-data project. Other stages are not allowed
to read or write a live Flex switch; callers must use the Standard path.
"""
stage = os.getenv("OMI_ENV_STAGE", "").strip().lower()
if stage not in {"dev", "prod"}:
return None
return f"{BACKGROUND_FLEX_CONTROL_COLLECTION}/{BACKGROUND_FLEX_CONTROL_DOCUMENT_PREFIX}_{stage}"
def _standard_control() -> PromotionFlexControl:
return PromotionFlexControl()
def read_promotion_flex_control(*, db_client: Any) -> PromotionFlexControl:
"""Read the strict shared control, failing safely to Standard."""
control_path = background_flex_control_path()
if not promotion_flex_capable() or control_path is None:
return _standard_control()
try:
snapshot = db_client.document(control_path).get()
if not getattr(snapshot, "exists", False):
return _standard_control()
payload = snapshot.to_dict()
if not isinstance(payload, dict) or set(payload) != {"enabled", "generation"}:
raise ValueError("invalid fields")
enabled = payload["enabled"]
generation = payload["generation"]
if not isinstance(enabled, bool):
raise ValueError("invalid enabled flag")
if not isinstance(generation, int) or isinstance(generation, bool) or generation < 1:
raise ValueError("invalid generation")
return PromotionFlexControl(enabled=enabled, generation=generation)
except Exception as exc:
logger.warning("background_flex_control_invalid error=%s", type(exc).__name__)
return _standard_control()
def _response_text(response: Any) -> str:
return cast(str, getattr(response, "content", str(response)))
class _BackgroundFlexModel:
def __init__(
self,
router: "PromotionFlexRunRouter",
*,
uid: str,
standard_feature: str,
flex_feature: str,
workload: str,
) -> None:
self._router = router
self._uid = uid
self._standard_feature = standard_feature
self._flex_feature = flex_feature
self._workload = workload
def invoke(self, payload: Any) -> Any:
return self._router.invoke_flex(
uid=self._uid,
payload=payload,
standard_feature=self._standard_feature,
flex_feature=self._flex_feature,
workload=self._workload,
)
class PromotionFlexRunRouter:
"""Own one job run's snapshot of the shared background Flex switch."""
def __init__(
self,
*,
db_client: Any,
control_reader: Callable[..., PromotionFlexControl] = read_promotion_flex_control,
flex_llm_factory: Callable[..., Any] = _get_gateway_flex_llm,
monotonic: Callable[[], float] = time.monotonic,
started_at: Optional[float] = None,
force_enabled: bool = False,
) -> None:
self._db_client = db_client
self._control_reader = control_reader
self._flex_llm_factory = flex_llm_factory
self._monotonic = monotonic
self._started_at = monotonic() if started_at is None else started_at
self.control = control_reader(db_client=db_client)
self._forced = bool(force_enabled) and promotion_flex_capable()
if self._forced:
generation = self.control.generation if self.control.generation >= 1 else 1
self.control = PromotionFlexControl(enabled=True, generation=generation)
self._flex_calls_started = 0
def llm_for_uid(
self,
uid: str,
*,
standard_feature: str,
flex_feature: str,
workload: str,
) -> Optional[Any]:
if not self.control.enabled:
return None
return _BackgroundFlexModel(
self,
uid=uid,
standard_feature=standard_feature,
flex_feature=flex_feature,
workload=workload,
)
def llm_invoke_for_uid(self, uid: str) -> Optional[Callable[[Any], str]]:
model = self.llm_for_uid(
uid,
standard_feature="memory_conflict",
flex_feature="memory_conflict_flex",
workload="memory_promotion",
)
if model is None:
return None
return lambda prompt: _response_text(model.invoke(prompt))
def _read_current_control(self) -> PromotionFlexControl:
return self._control_reader(db_client=self._db_client)
def assert_control_current(self) -> None:
if self._forced:
return
if self._read_current_control() != self.control:
raise PromotionFlexControlChanged("background Flex control changed before result apply")
def assert_result_current(self) -> None:
if self.control.enabled:
self.assert_control_current()
@staticmethod
def _is_transient_flex_error(exc: Exception) -> bool:
status_code = getattr(exc, "status_code", None)
return (
status_code in _TRANSIENT_FLEX_STATUS_CODES
or type(exc).__name__ in _TRANSIENT_FLEX_EXCEPTION_NAMES
or isinstance(exc, (ConnectionError, TimeoutError))
)
def job_budget_fits(self) -> bool:
"""Whether one more Flex reservation still fits the one-hour job budget."""
elapsed = max(self._monotonic() - self._started_at, 0.0)
return (
elapsed + BACKGROUND_FLEX_TIMEOUT_SECONDS
<= BACKGROUND_FLEX_JOB_BUDGET_SECONDS - BACKGROUND_FLEX_JOB_SAFETY_SECONDS
)
def require_job_budget(self) -> None:
"""Stop in-UID TTL/outbox work when the same one-hour clock is gone."""
if not self.job_budget_fits():
raise PromotionFlexDeferred("job_budget")
def invoke_flex(
self,
*,
uid: str,
payload: Any,
standard_feature: str,
flex_feature: str,
workload: str,
) -> Any:
if not self.job_budget_fits():
raise PromotionFlexDeferred("job_budget")
self.assert_control_current()
self._flex_calls_started += 1
logger.info(
"background_flex_request workload=%s uid=%s generation=%d ordinal=%d",
workload,
uid,
self.control.generation,
self._flex_calls_started,
)
try:
response = (
self._flex_llm_factory(flex_feature, request_timeout=BACKGROUND_FLEX_TIMEOUT_SECONDS)
.bind(service_tier="flex")
.invoke(payload)
)
except Exception as exc:
if self._is_transient_flex_error(exc):
raise PromotionFlexDeferred(type(exc).__name__) from exc
raise
self.assert_control_current()
return response
__all__ = [
"BACKGROUND_FLEX_CAPABLE_ENV",
"BACKGROUND_FLEX_CONTROL_COLLECTION",
"BACKGROUND_FLEX_CONTROL_DOCUMENT_PREFIX",
"BACKGROUND_FLEX_JOB_BUDGET_SECONDS",
"BACKGROUND_FLEX_JOB_SAFETY_SECONDS",
"BACKGROUND_FLEX_LEASE_SECONDS",
"BACKGROUND_FLEX_TIMEOUT_SECONDS",
"MEMORY_PROMOTION_FLEX_LEASE_SECONDS",
"PromotionFlexControl",
"PromotionFlexControlChanged",
"PromotionFlexDeferred",
"PromotionFlexRunRouter",
"background_flex_control_path",
"promotion_flex_capable",
"read_promotion_flex_control",
]