| 874 | self.fallback_router.register(name, fn) |
| 875 | |
| 876 | def run( |
| 877 | self, |
| 878 | user_input: str, |
| 879 | constraints: Optional[List[str]] = None, |
| 880 | context: str = "", |
| 881 | ) -> ControlPacket: |
| 882 | constraints = constraints or [] |
| 883 | audit_id = self._make_audit_id(user_input) |
| 884 | start = time.perf_counter() |
| 885 | |
| 886 | # ── Input Guard ──────────────────────────────────────────────────── |
| 887 | guard_result = self.input_guard.validate(user_input) |
| 888 | if not guard_result.passed: |
| 889 | self._log_record(audit_id, 0, guard_result, 0.0, 0, RetryStrategy.NONE) |
| 890 | log.warning( |
| 891 | "input_guard.blocked", |
| 892 | failure_mode=guard_result.failure_mode.value, |
| 893 | message=guard_result.message[:80], |
| 894 | ) |
| 895 | return self._packet("", "", 0, start, guard_result, |
| 896 | TokenBudget(self.config.total_tokens, self.config.model_name), |
| 897 | RetryStrategy.NONE, audit_id) |
| 898 | |
| 899 | user_input = self.input_guard.sanitize(user_input) |
| 900 | |
| 901 | # ── Circuit Breaker ──────────────────────────────────────────────── |
| 902 | if self.circuit_breaker.is_open(): |
| 903 | result = ValidationResult( |
| 904 | passed=False, |
| 905 | failure_mode=FailureMode.CIRCUIT_OPEN, |
| 906 | message="Circuit breaker is open. LLM backend unavailable.", |
| 907 | score=0.0, |
| 908 | ) |
| 909 | self._log_record(audit_id, 0, result, 0.0, 0, RetryStrategy.NONE) |
| 910 | log.error("circuit_breaker.rejected", audit_id=audit_id) |
| 911 | return self._packet("", "", 0, start, result, |
| 912 | TokenBudget(self.config.total_tokens, self.config.model_name), |
| 913 | RetryStrategy.NONE, audit_id) |
| 914 | |
| 915 | mutation_hint = "" |
| 916 | last_validation = ValidationResult(passed=False) |
| 917 | last_budget = TokenBudget(self.config.total_tokens, self.config.model_name) |
| 918 | attempt = 0 |
| 919 | |
| 920 | # ── Retry Loop ───────────────────────────────────────────────────── |
| 921 | for attempt in range(1, self.config.max_attempts + 1): |
| 922 | prompt, budget = self.prompt_builder.build( |
| 923 | user_input, constraints, context, mutation_hint |
| 924 | ) |
| 925 | last_budget = budget |
| 926 | |
| 927 | t0 = time.perf_counter() |
| 928 | try: |
| 929 | response = self.llm_caller.call(prompt) |
| 930 | self.circuit_breaker.record_success() |
| 931 | except LLMTimeoutError as exc: |
| 932 | call_latency_ms = (time.perf_counter() - t0) * 1000 |
| 933 | self.circuit_breaker.record_failure() |