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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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"""
Base classes and mixins for gateway components.

Provides reusable patterns to reduce code duplication across the codebase.
"""

from abc import ABC, abstractmethod
from typing import Optional, TypeVar, Generic
import logging

__all__ = [
    # Base classes
    "AsyncLoadableEntity",
    # Condition matching
    "ConditionMatcher",
    "ConditionMatchResult",
    "ConditionContext",
    "match_conditions",
]

logger = logging.getLogger(__name__)


# =============================================================================
# AsyncLoadableEntity - Base class for DB-backed lazy loading
# =============================================================================

T = TypeVar("T")


class AsyncLoadableEntity(ABC, Generic[T]):
    """
    Base class for entities that load data from database asynchronously.

    Provides:
    - Lazy loading with ensure_loaded()
    - Cache invalidation
    - Error handling with fallback

    Usage:
        class PolicyEngine(AsyncLoadableEntity[list[PolicyRule]]):
            async def _load_from_db(self) -> list[PolicyRule]:
                async with get_db_context() as db:
                    ...
                    return rules
    """

    def __init__(self):
        self._data: Optional[T] = None
        self._loaded: bool = False
        self._load_error: Optional[str] = None

    @property
    def is_loaded(self) -> bool:
        """Check if data has been loaded."""
        return self._loaded

    @property
    def data(self) -> Optional[T]:
        """Get loaded data (may be None if not loaded)."""
        return self._data

    async def ensure_loaded(self) -> None:
        """Ensure data is loaded from database. Safe to call multiple times."""
        if not self._loaded:
            await self.reload()

    async def reload(self) -> None:
        """Force reload data from database."""
        try:
            self._data = await self._load_from_db()
            self._loaded = True
            self._load_error = None
        except Exception as e:
            self._load_error = str(e)
            logger.warning(f"Failed to load {self.__class__.__name__} from DB: {e}")
            # Set default value on error
            self._data = self._get_default_value()
            self._loaded = True  # Mark as loaded to avoid retry loops

    def invalidate(self) -> None:
        """Invalidate cached data, forcing reload on next access."""
        self._loaded = False
        self._data = None
        self._load_error = None

    @abstractmethod
    async def _load_from_db(self) -> T:
        """Load data from database. Implement in subclass."""
        raise NotImplementedError

    def _get_default_value(self) -> T:
        """Get default value when loading fails. Override in subclass."""
        return None  # type: ignore


# =============================================================================
# ConditionMatcher - Unified condition matching logic
# =============================================================================


class ConditionMatcher:
    """
    Unified condition matching logic for policies and features.

    Centralizes all the condition checking that was duplicated across:
    - PolicyEngine._rule_matches()
    - PolicyService._check_conditions()
    - FeatureEngine.check_feature() (partial)
    - PolicySimulator.evaluate_policy()

    Usage:
        matcher = ConditionMatcher()
        result = matcher.match_all(conditions, context)
        if result.matches:
            print(f"Matched: {result.reason}")
    """

    @staticmethod
    def matches_environment(
        environment: str,
        allowed: Optional[list[str]] = None,
        denied: Optional[list[str]] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if environment matches restrictions.

        Returns:
            (matches, reason) - matches=True means condition is satisfied
        """
        if denied and environment in denied:
            return False, f"Environment '{environment}' is denied"

        if allowed and environment not in allowed:
            return False, f"Environment '{environment}' not in allowed: {allowed}"

        return True, None

    @staticmethod
    def matches_model(
        model: str,
        allowed: Optional[list[str]] = None,
        denied: Optional[list[str]] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if model matches restrictions.

        Supports:
        - Exact match: "gpt-4o"
        - Prefix match: "gpt-*" or "claude-*"

        Returns:
            (matches, reason) - matches=True means condition is satisfied
        """
        # Check denied list first
        if denied:
            for pattern in denied:
                if ConditionMatcher._model_matches_pattern(model, pattern):
                    return False, f"Model '{model}' is blocked"

        # Check allowed list
        if allowed:
            for pattern in allowed:
                if ConditionMatcher._model_matches_pattern(model, pattern):
                    return True, None
            return False, f"Model '{model}' not in allowed: {allowed}"

        return True, None

    @staticmethod
    def _model_matches_pattern(model: str, pattern: str) -> bool:
        """Check if model matches a pattern (exact or prefix with *)."""
        if pattern.endswith("*"):
            return model.startswith(pattern[:-1])
        return model == pattern

    @staticmethod
    def matches_feature(
        feature: Optional[str],
        allowed: Optional[list[str]] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if feature matches restrictions.

        Returns:
            (matches, reason) - matches=True means condition is satisfied
        """
        if not allowed:
            return True, None

        if not feature:
            return True, None  # No feature specified = no restriction

        if feature not in allowed:
            return False, f"Feature '{feature}' not allowed"

        return True, None

    @staticmethod
    def matches_tokens(
        input_tokens: Optional[int] = None,
        output_tokens: Optional[int] = None,
        max_input: Optional[int] = None,
        max_output: Optional[int] = None,
        max_total: Optional[int] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if token counts are within limits.

        Returns:
            (matches, reason) - matches=True means within limits
        """
        if max_input and input_tokens and input_tokens > max_input:
            return False, f"Input tokens ({input_tokens}) exceeds limit ({max_input})"

        if max_output and output_tokens and output_tokens > max_output:
            return (
                False,
                f"Output tokens ({output_tokens}) exceeds limit ({max_output})",
            )

        if max_total:
            total = (input_tokens or 0) + (output_tokens or 0)
            if total > max_total:
                return False, f"Total tokens ({total}) exceeds limit ({max_total})"

        return True, None

    @staticmethod
    def matches_time(
        allowed_hours: Optional[tuple[int, int]] = None,
        current_hour: Optional[int] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if current time is within allowed hours.

        Args:
            allowed_hours: (start_hour, end_hour) in 24h format
            current_hour: Current hour (0-23). If None, uses current time.

        Returns:
            (matches, reason) - matches=True means within allowed hours
        """
        if not allowed_hours:
            return True, None

        if current_hour is None:
            from datetime import datetime

            current_hour = datetime.now().hour

        start, end = allowed_hours

        # Handle overnight ranges (e.g., 22-6)
        if start <= end:
            in_range = start <= current_hour < end
        else:
            in_range = current_hour >= start or current_hour < end

        if not in_range:
            return (
                False,
                f"Current hour ({current_hour}) outside allowed hours ({start}-{end})",
            )

        return True, None

    @staticmethod
    def matches_app(
        app_id: str,
        allowed: Optional[list[str]] = None,
    ) -> tuple[bool, Optional[str]]:
        """
        Check if app_id matches restrictions.

        Supports:
        - Exact match
        - Wildcard "*" for all apps

        Returns:
            (matches, reason) - matches=True means condition is satisfied
        """
        if not allowed:
            return True, None

        if "*" in allowed or app_id in allowed:
            return True, None

        return False, f"App '{app_id}' not in allowed list"


class ConditionMatchResult:
    """Result of condition matching."""

    def __init__(self, matches: bool = True, reason: Optional[str] = None):
        self.matches = matches
        self.reason = reason
        self.matched_conditions: list[str] = []
        self.failed_conditions: list[str] = []

    def add_match(self, condition: str) -> None:
        """Record a matched condition."""
        self.matched_conditions.append(condition)

    def add_failure(self, condition: str, reason: str) -> None:
        """Record a failed condition."""
        self.failed_conditions.append(f"{condition}: {reason}")
        self.matches = False
        if not self.reason:
            self.reason = reason

    def __bool__(self) -> bool:
        return self.matches


class ConditionContext:
    """Context for condition matching."""

    def __init__(
        self,
        model: str = "",
        environment: str = "",
        feature: Optional[str] = None,
        app_id: str = "",
        input_tokens: Optional[int] = None,
        output_tokens: Optional[int] = None,
        max_tokens: Optional[int] = None,
        current_hour: Optional[int] = None,
    ):
        self.model = model
        self.environment = environment
        self.feature = feature
        self.app_id = app_id
        self.input_tokens = input_tokens
        self.output_tokens = output_tokens
        self.max_tokens = max_tokens
        self.current_hour = current_hour


def _check_environment_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check environment condition and update result."""
    if "environments" not in conditions and "allowed_environments" not in conditions:
        return

    allowed = conditions.get("environments") or conditions.get("allowed_environments")
    denied = conditions.get("denied_environments")
    ok, reason = ConditionMatcher.matches_environment(
        context.environment, allowed=allowed, denied=denied
    )

    if ok:
        result.add_match(f"environment={context.environment}")
    else:
        result.add_failure("environment", reason or "Environment not allowed")


def _check_model_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check model condition and update result."""
    if (
        "models" not in conditions
        and "allowed_models" not in conditions
        and "blocked_models" not in conditions
    ):
        return

    allowed = conditions.get("models") or conditions.get("allowed_models")
    denied = conditions.get("blocked_models")
    ok, reason = ConditionMatcher.matches_model(
        context.model, allowed=allowed, denied=denied
    )

    if ok:
        result.add_match(f"model={context.model}")
    else:
        result.add_failure("model", reason or "Model not allowed")


def _check_feature_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check feature condition and update result."""
    if "features" not in conditions:
        return

    ok, reason = ConditionMatcher.matches_feature(
        context.feature, allowed=conditions["features"]
    )

    if ok:
        result.add_match(f"feature={context.feature}")
    else:
        result.add_failure("feature", reason or "Feature not allowed")


def _check_token_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check token limits condition and update result."""
    max_input = conditions.get("max_context_tokens") or conditions.get(
        "max_input_tokens"
    )
    max_output = conditions.get("max_tokens") or conditions.get("max_output_tokens")

    if not max_input and not max_output:
        return

    ok, reason = ConditionMatcher.matches_tokens(
        input_tokens=context.input_tokens,
        output_tokens=context.output_tokens or context.max_tokens,
        max_input=max_input,
        max_output=max_output,
    )

    if ok:
        result.add_match("tokens within limits")
    else:
        result.add_failure("tokens", reason or "Token limit exceeded")


def _check_time_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check time condition and update result."""
    if "allowed_hours" not in conditions:
        return

    hours = conditions["allowed_hours"]
    if not isinstance(hours, (list, tuple)) or len(hours) != 2:
        return

    ok, reason = ConditionMatcher.matches_time(
        allowed_hours=(hours[0], hours[1]),
        current_hour=context.current_hour,
    )

    if ok:
        result.add_match("time within allowed hours")
    else:
        result.add_failure("time", reason or "Outside allowed hours")


def _check_app_condition(
    conditions: dict, context: ConditionContext, result: ConditionMatchResult
) -> None:
    """Check app_id condition and update result."""
    if "app_id" not in conditions:
        return

    ok, reason = ConditionMatcher.matches_app(
        context.app_id, allowed=[conditions["app_id"]]
    )

    if ok:
        result.add_match(f"app_id={context.app_id}")
    else:
        result.add_failure("app_id", reason or "App not allowed")


def match_conditions(
    conditions: dict, context: ConditionContext
) -> ConditionMatchResult:
    """
    Match all conditions against a context.

    This is the main entry point for unified condition matching.

    Args:
        conditions: Dict of condition name -> value(s)
            Supported conditions:
            - environments: list[str]
            - allowed_models / blocked_models: list[str]
            - features: list[str]
            - max_tokens / max_context_tokens: int
            - allowed_hours: tuple[int, int]
            - app_id: str or "*"

        context: ConditionContext with request details

    Returns:
        ConditionMatchResult with match status and details
    """
    result = ConditionMatchResult()

    _check_environment_condition(conditions, context, result)
    _check_model_condition(conditions, context, result)
    _check_feature_condition(conditions, context, result)
    _check_token_condition(conditions, context, result)
    _check_time_condition(conditions, context, result)
    _check_app_condition(conditions, context, result)

    return result