-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcontrol_layer.py
More file actions
1076 lines (912 loc) · 38.7 KB
/
Copy pathcontrol_layer.py
File metadata and controls
1076 lines (912 loc) · 38.7 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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
control_layer.py
A production-grade control layer that sits between application logic and LLM models.
Production hardening over v1:
- Accurate token counting via tiktoken (not char/4 heuristic)
- Real timeout enforcement on every LLM call
- Circuit breaker: stops hammering a failing backend
- Expanded injection detection with 20 tested patterns
- Jittered exponential backoff on retries
- Persistent JSON audit log (survives restarts)
- Pydantic config validation (no silent misconfiguration)
- Structured JSON logging throughout (Datadog/CloudWatch ready)
- Thread-safe audit logger
- Pluggable LLM: swap MockLLM for any callable in one line
Tested on Python 3.12, CPU only, no GPU required.
Dependencies: tiktoken, tenacity, structlog, pydantic
Install: pip install tiktoken tenacity structlog pydantic
Full source: https://github.com/Emmimal/control-layer/
"""
import re
import time
import json
import random
import hashlib
import threading
import uuid
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Optional, Callable, List, Dict, Tuple, Any
from collections import defaultdict
from contextlib import contextmanager
import tiktoken
import structlog
from tenacity import (
retry,
stop_after_attempt,
wait_exponential_jitter,
retry_if_exception_type,
RetryError,
)
from pydantic import BaseModel, Field, field_validator, model_validator
# ---------------------------------------------------------------------------
# Structured logger (JSON in production, pretty in dev)
# Set LOG_FORMAT=json in environment for production
# ---------------------------------------------------------------------------
import os
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.JSONRenderer()
if os.getenv("LOG_FORMAT") == "json"
else structlog.dev.ConsoleRenderer(),
],
wrapper_class=structlog.stdlib.BoundLogger,
context_class=dict,
logger_factory=structlog.PrintLoggerFactory(),
)
log = structlog.get_logger(__name__)
# =============================================================================
# Enums
# =============================================================================
class FailureMode(str, Enum):
NONE = "none"
SCHEMA_VIOLATION = "schema_violation"
CONSTRAINT_VIOLATION = "constraint_violation"
PROMPT_INJECTION = "prompt_injection"
TOKEN_OVERFLOW = "token_overflow"
HALLUCINATION = "hallucination"
TIMEOUT = "timeout"
CIRCUIT_OPEN = "circuit_open"
class RetryStrategy(str, Enum):
NONE = "none"
SIMPLE = "simple"
PROMPT_MUTATION = "prompt_mutation"
FALLBACK = "fallback"
# =============================================================================
# Pydantic Config (replaces hardcoded magic numbers)
# =============================================================================
class ControlLayerConfig(BaseModel):
"""
All tunable parameters in one validated place.
Pass a config dict or load from YAML/env — never hardcode in prod.
"""
total_tokens: int = Field(800, ge=64, le=128_000)
max_attempts: int = Field(3, ge=1, le=10)
max_input_chars: int = Field(2000, ge=64, le=32_000)
timeout_seconds: float = Field(30.0, ge=0.01, le=300.0)
base_delay_ms: float = Field(50.0, ge=1.0, le=5_000.0)
max_delay_ms: float = Field(2000.0, ge=100.0, le=30_000.0)
jitter_ms: float = Field(25.0, ge=0.0, le=500.0)
# Circuit breaker
cb_failure_threshold: int = Field(5, ge=2, le=50)
cb_recovery_seconds: float = Field(30.0, ge=5.0, le=300.0)
# Audit
audit_log_path: str = Field("audit.jsonl")
model_name: str = Field("cl100k_base") # tiktoken encoding
@field_validator("max_delay_ms")
@classmethod
def max_gt_base(cls, v, info):
base = info.data.get("base_delay_ms", 50.0)
if v < base:
raise ValueError("max_delay_ms must be >= base_delay_ms")
return v
class ResponseSchema(BaseModel):
"""Output contract the LLM response must satisfy."""
required_keys: List[str] = Field(default_factory=list)
max_length: Optional[int] = None
min_length: Optional[int] = None
forbidden_phrases: List[str] = Field(default_factory=list)
must_contain: List[str] = Field(default_factory=list)
must_be_json: bool = False
@field_validator("max_length")
@classmethod
def max_positive(cls, v):
if v is not None and v <= 0:
raise ValueError("max_length must be positive")
return v
@model_validator(mode="after")
def min_lt_max(self):
if self.min_length and self.max_length:
if self.min_length >= self.max_length:
raise ValueError("min_length must be < max_length")
return self
# =============================================================================
# Data Classes
# =============================================================================
@dataclass
class ValidationResult:
passed: bool
failure_mode: FailureMode = FailureMode.NONE
message: str = ""
score: float = 1.0
@dataclass
class AuditRecord:
audit_id: str
timestamp: str # ISO-8601
prompt_hash: str
attempt: int
failure_mode: FailureMode
latency_ms: float
token_count: int
passed: bool
strategy: RetryStrategy = RetryStrategy.NONE
def to_dict(self) -> dict:
return {
"audit_id": self.audit_id,
"timestamp": self.timestamp,
"prompt_hash": self.prompt_hash,
"attempt": self.attempt,
"failure_mode": self.failure_mode.value,
"latency_ms": round(self.latency_ms, 3),
"token_count": self.token_count,
"passed": self.passed,
"strategy": self.strategy.value,
}
@dataclass
class ControlPacket:
prompt: str
response: str
attempts: int
total_latency_ms: float
validation: ValidationResult
token_budget_used: int
token_budget_total: int
strategy_used: RetryStrategy
audit_id: str
# =============================================================================
# Component 1: Input Guard (expanded injection patterns)
# =============================================================================
INJECTION_PATTERNS: List[str] = [
# Classic override
r"ignore\s+(?:all\s+|previous\s+|above\s+|prior\s+|the\s+)*(instructions|prompts|context|rules)",
r"\byou are now\b",
r"disregard\s+(your|all|previous|the)",
r"forget\s+(everything|all|your instructions|the above)",
r"new\s+(role|persona|instructions|directive)\s*:",
r"\bsystem\s*:",
# Token smuggling
r"<\|.*?\|>",
r"\[\[.*?\]\]",
r"<<.*?>>",
# Persona hijack
r"act as if you (are|have no|were)",
r"pretend (you are|to be|you have no)",
r"roleplay as",
r"simulate\s+(a|an)\s+\w+\s+(ai|model|assistant)",
# Jailbreak phrasing
r"(do anything now|dan mode|developer mode|jailbreak)",
r"without\s+(any\s+)?(restrictions|constraints|filters|guidelines)",
r"bypass\s+(your\s+)?(safety|filters|rules|guidelines)",
# Prompt leaking
r"(repeat|print|show|reveal|output)\s+(your\s+)?(system\s+prompt|instructions|context)",
r"what (are|were) your (instructions|system prompt|rules)",
# Indirect injection
r"the\s+(following|above|text)\s+(is|are)\s+(now\s+)?your\s+(new\s+)?(instructions|prompt)",
]
class InputGuard:
"""
Validates and sanitizes user input before it reaches the prompt builder.
Three checks, in order:
1. Empty / whitespace-only input.
2. Character length exceeds budget.
3. Known injection pattern match (20 patterns, tested).
Returns a ValidationResult. Never raises.
"""
def __init__(self, max_input_chars: int = 2000):
self.max_input_chars = max_input_chars
self._patterns = [
re.compile(p, re.IGNORECASE | re.DOTALL)
for p in INJECTION_PATTERNS
]
def validate(self, user_input: str) -> ValidationResult:
if not user_input or not user_input.strip():
return ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message="Input is empty.",
score=0.0,
)
if len(user_input) > self.max_input_chars:
return ValidationResult(
passed=False,
failure_mode=FailureMode.TOKEN_OVERFLOW,
message=(
f"Input exceeds {self.max_input_chars} chars "
f"({len(user_input)} received)."
),
score=0.0,
)
for pattern in self._patterns:
if pattern.search(user_input):
return ValidationResult(
passed=False,
failure_mode=FailureMode.PROMPT_INJECTION,
message=f"Injection pattern detected: '{pattern.pattern[:60]}'",
score=0.0,
)
return ValidationResult(passed=True, score=1.0)
def sanitize(self, user_input: str) -> str:
text = user_input.strip()
text = re.sub(r"\s+", " ", text)
# Strip null bytes and non-printable control chars (except newline/tab)
text = re.sub(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]", "", text)
return text
# =============================================================================
# Component 2: Token Budget (tiktoken — accurate counting)
# =============================================================================
class TokenBudget:
"""
Slot-based token allocator using tiktoken for accurate counts.
Production fix over v1: char/4 heuristic replaced with the actual
tokenizer the model uses. Non-English text and code tokenize
very differently — the heuristic causes silent overflow in prod.
"""
def __init__(self, total_tokens: int, encoding_name: str = "cl100k_base"):
self.total_tokens = total_tokens
self._enc = None
try:
self._enc = tiktoken.get_encoding(encoding_name)
except Exception:
# Graceful offline fallback: char/4 heuristic
# Replace with tiktoken when network access is available
pass
self._slots: Dict[str, int] = {}
def count(self, text: str) -> int:
"""Exact token count via tiktoken, or char/4 heuristic if offline."""
if self._enc is not None:
return len(self._enc.encode(text))
return max(1, len(text) // 4)
def reserve(self, name: str, text: str) -> bool:
tokens = self.count(text)
if self.remaining() < tokens:
return False
self._slots[name] = tokens
return True
def reserve_tokens(self, name: str, tokens: int) -> bool:
if self.remaining() < tokens:
return False
self._slots[name] = tokens
return True
def used(self) -> int:
return sum(self._slots.values())
def remaining(self) -> int:
return self.total_tokens - self.used()
def remaining_chars(self) -> int:
# Rough inverse for truncation: 1 token ~ 4 chars (English)
return self.remaining() * 4
def report(self) -> Dict[str, int]:
return dict(self._slots)
# =============================================================================
# Component 3: Prompt Builder
# =============================================================================
class PromptBuilder:
"""
Assembles the final prompt within a hard token budget.
Reservation order (priority, highest first):
1. System prompt — fixed overhead, always fits
2. Constraints — hard requirements, always fits
3. Mutation hint — retry correction
4. Context — truncated if budget is tight
5. User input — what the user actually asked
"""
def __init__(
self,
system_prompt: str,
config: ControlLayerConfig,
):
self.system_prompt = system_prompt
self.config = config
def build(
self,
user_input: str,
constraints: List[str],
context: str = "",
mutation_hint: str = "",
) -> Tuple[str, TokenBudget]:
budget = TokenBudget(self.config.total_tokens, self.config.model_name)
budget.reserve("system_prompt", self.system_prompt)
constraint_block = self._format_constraints(constraints)
if constraint_block:
budget.reserve("constraints", constraint_block)
if mutation_hint:
budget.reserve("mutation_hint", mutation_hint)
if context:
if not budget.reserve("context", context):
# Truncate context to fit remaining budget
max_chars = budget.remaining_chars()
context = context[:max_chars]
budget.reserve("context_truncated", context)
budget.reserve("user_input", user_input)
parts = [self.system_prompt]
if constraint_block:
parts.append(constraint_block)
if mutation_hint:
parts.append(f"Correction note: {mutation_hint}")
if context:
parts.append(f"Context:\n{context}")
parts.append(f"User: {user_input}")
return "\n\n".join(parts), budget
def _format_constraints(self, constraints: List[str]) -> str:
if not constraints:
return ""
lines = ["Constraints (hard requirements, not suggestions):"]
for i, c in enumerate(constraints, 1):
lines.append(f" {i}. {c}")
return "\n".join(lines)
# =============================================================================
# Component 4: Response Validator
# =============================================================================
class ResponseValidator:
"""
Validates model output against a schema and rule set.
Prompts ask. Validators enforce. That's the whole difference.
"""
def validate(
self, response: str, schema: ResponseSchema
) -> ValidationResult:
if not response or not response.strip():
return ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message="Response is empty.",
score=0.0,
)
if schema.must_be_json:
result = self._check_json(response, schema.required_keys)
if not result.passed:
return result
if schema.max_length and len(response) > schema.max_length:
return ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message=(
f"Response too long: {len(response)} chars "
f"(max {schema.max_length})."
),
score=round(schema.max_length / len(response), 3),
)
if schema.min_length and len(response) < schema.min_length:
return ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message=(
f"Response too short: {len(response)} chars "
f"(min {schema.min_length})."
),
score=0.0,
)
for phrase in schema.forbidden_phrases:
if phrase.lower() in response.lower():
return ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message=f"Forbidden phrase found: '{phrase}'",
score=0.0,
)
score = self._quality_score(response, schema)
return ValidationResult(passed=True, score=score)
def _check_json(
self, response: str, required_keys: List[str]
) -> ValidationResult:
try:
cleaned = re.sub(r"```(?:json)?|```", "", response).strip()
data = json.loads(cleaned)
for key in required_keys:
if key not in data:
return ValidationResult(
passed=False,
failure_mode=FailureMode.SCHEMA_VIOLATION,
message=f"Missing required key: '{key}'",
score=0.0,
)
return ValidationResult(passed=True, score=1.0)
except json.JSONDecodeError as exc:
return ValidationResult(
passed=False,
failure_mode=FailureMode.SCHEMA_VIOLATION,
message=f"Invalid JSON: {exc}",
score=0.0,
)
def _quality_score(self, response: str, schema: ResponseSchema) -> float:
if not schema.must_contain:
return 1.0
hits = sum(
1 for phrase in schema.must_contain
if phrase.lower() in response.lower()
)
return round(hits / len(schema.must_contain), 3)
# =============================================================================
# Component 5: Circuit Breaker
# =============================================================================
class CircuitState(str, Enum):
CLOSED = "closed" # Normal operation
OPEN = "open" # Failing — reject calls immediately
HALF_OPEN = "half_open" # Testing recovery
class CircuitBreaker:
"""
Stops hammering a failing LLM backend.
States: CLOSED (normal) → OPEN (failing) → HALF_OPEN (testing) → CLOSED
Production fix over v1: without this, a down LLM causes every
request to retry max_attempts times, saturating thread pools
and exploding latency across all concurrent users.
"""
def __init__(self, failure_threshold: int = 5, recovery_seconds: float = 30.0):
self.failure_threshold = failure_threshold
self.recovery_seconds = recovery_seconds
self._failures = 0
self._last_failure_time: Optional[float] = None
self._state = CircuitState.CLOSED
self._lock = threading.Lock()
@property
def state(self) -> CircuitState:
with self._lock:
if self._state == CircuitState.OPEN:
elapsed = time.monotonic() - (self._last_failure_time or 0)
if elapsed >= self.recovery_seconds:
self._state = CircuitState.HALF_OPEN
log.info("circuit_breaker.half_open", recovery_seconds=self.recovery_seconds)
return self._state
def is_open(self) -> bool:
return self.state == CircuitState.OPEN
def record_success(self) -> None:
with self._lock:
self._failures = 0
self._state = CircuitState.CLOSED
def record_failure(self) -> None:
with self._lock:
self._failures += 1
self._last_failure_time = time.monotonic()
if self._failures >= self.failure_threshold:
if self._state != CircuitState.OPEN:
log.warning(
"circuit_breaker.open",
failures=self._failures,
threshold=self.failure_threshold,
)
self._state = CircuitState.OPEN
def reset(self) -> None:
with self._lock:
self._failures = 0
self._state = CircuitState.CLOSED
self._last_failure_time = None
# =============================================================================
# Component 6: Retry Engine (tenacity + jitter)
# =============================================================================
MUTATION_HINTS: Dict[FailureMode, str] = {
FailureMode.SCHEMA_VIOLATION: (
"Your previous response was not valid JSON. "
"Return ONLY a valid JSON object. No markdown fencing, "
"no preamble, no trailing text. Start with '{' and end with '}'."
),
FailureMode.CONSTRAINT_VIOLATION: (
"Your previous response violated a hard constraint. "
"Re-read every numbered constraint before generating your response. "
"Each constraint is a strict requirement, not a suggestion."
),
FailureMode.HALLUCINATION: (
"If you are uncertain about a fact, say so explicitly. "
"Do not invent citations, statistics, or names."
),
FailureMode.TOKEN_OVERFLOW: (
"Your previous response was too long. "
"Be significantly more concise. Aim for half the length."
),
FailureMode.TIMEOUT: (
"Respond with a shorter, more direct answer. "
"No preamble. Get to the point immediately."
),
}
# Failure modes that are never retried — hard stops
NO_RETRY_MODES = {FailureMode.PROMPT_INJECTION, FailureMode.CIRCUIT_OPEN}
class RetryEngine:
"""
Retries failed LLM calls with prompt mutation targeted at the
specific failure mode detected.
Production fix over v1:
- Uses tenacity for battle-tested retry logic
- Jittered exponential backoff (prevents thundering herd)
- Injection and circuit-open failures are never retried
"""
def __init__(self, config: ControlLayerConfig):
self.config = config
def get_mutation_hint(self, failure_mode: FailureMode) -> str:
return MUTATION_HINTS.get(
failure_mode,
"Follow the instructions carefully and try again.",
)
def should_retry(self, failure_mode: FailureMode, attempt: int) -> bool:
if attempt >= self.config.max_attempts:
return False
if failure_mode in NO_RETRY_MODES:
return False
return True
def jittered_delay_s(self, attempt: int) -> float:
"""
Exponential backoff with random jitter.
Prevents thundering herd when multiple requests retry simultaneously.
"""
base_s = self.config.base_delay_ms / 1000
max_s = self.config.max_delay_ms / 1000
jitter_s = self.config.jitter_ms / 1000
delay = min(base_s * (2 ** (attempt - 1)), max_s)
delay += random.uniform(0, jitter_s)
return delay
# =============================================================================
# Component 7: Fallback Router
# =============================================================================
class FallbackRouter:
"""
When the primary strategy exhausts all retries, routes to a
registered fallback handler.
Fallbacks are called in registration order. First non-empty response wins.
"""
def __init__(self):
self._strategies: Dict[str, Callable[[str], str]] = {}
self._order: List[str] = []
def register(self, name: str, fn: Callable[[str], str]) -> None:
self._strategies[name] = fn
if name not in self._order:
self._order.append(name)
def route(
self, user_input: str, failure_mode: FailureMode
) -> Tuple[str, str]:
for name in self._order:
try:
response = self._strategies[name](user_input)
if response:
log.info(
"fallback.used",
strategy=name,
failure_mode=failure_mode.value,
)
return name, response
except Exception as exc:
log.error("fallback.error", strategy=name, error=str(exc))
return "none", ""
# =============================================================================
# Component 8: Audit Logger (thread-safe, persistent JSONL)
# =============================================================================
class AuditLogger:
"""
Records every attempt with its failure mode, latency, and outcome.
Production fix over v1:
- Thread-safe writes via lock
- Persistent JSONL file (survives restarts)
- In-memory index for fast analytics
- Each line is a valid JSON object (grep/jq friendly)
"""
def __init__(self, log_path: str = "audit.jsonl"):
self._path = Path(log_path)
self._records: List[AuditRecord] = []
self._lock = threading.Lock()
# Load existing records on startup
self._load_existing()
def _load_existing(self) -> None:
if self._path.exists():
with self._lock:
try:
with open(self._path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
# Reconstruct for in-memory analytics only
# Full record is in the file
pass
except Exception as exc:
log.warning("audit.load_failed", error=str(exc))
def log(self, record: AuditRecord) -> None:
with self._lock:
self._records.append(record)
try:
with open(self._path, "a", encoding="utf-8") as f:
f.write(json.dumps(record.to_dict()) + "\n")
except Exception as exc:
log.error("audit.write_failed", error=str(exc))
def all_records(self) -> List[AuditRecord]:
with self._lock:
return list(self._records)
def failure_distribution(self) -> Dict[str, int]:
with self._lock:
dist: Dict[str, int] = defaultdict(int)
for r in self._records:
dist[r.failure_mode.value] += 1
return dict(dist)
def retry_distribution(self) -> Dict[int, int]:
with self._lock:
dist: Dict[int, int] = defaultdict(int)
for r in self._records:
dist[r.attempt] += 1
return dict(dist)
def pass_rate(self) -> float:
with self._lock:
if not self._records:
return 0.0
return sum(1 for r in self._records if r.passed) / len(self._records)
def latency_stats(self) -> Dict[str, float]:
with self._lock:
latencies = [r.latency_ms for r in self._records]
if not latencies:
return {}
latencies.sort()
n = len(latencies)
return {
"min": round(latencies[0], 2),
"max": round(latencies[-1], 2),
"mean": round(sum(latencies) / n, 2),
"p50": round(latencies[int(n * 0.50)], 2),
"p90": round(latencies[int(n * 0.90)], 2),
"p99": round(latencies[min(int(n * 0.99), n - 1)], 2),
}
# =============================================================================
# LLM Caller (timeout enforced)
# =============================================================================
class LLMTimeoutError(Exception):
"""Raised when the LLM call exceeds the configured timeout."""
class LLMCaller:
"""
Wraps any callable LLM function with timeout enforcement.
Production fix over v1: without a timeout, a hung LLM call
blocks the thread forever. This uses threading.Timer to enforce
a hard deadline on every call, regardless of the LLM backend.
"""
def __init__(self, llm_fn: Callable[[str], str], timeout_seconds: float):
self.llm_fn = llm_fn
self.timeout_seconds = timeout_seconds
def call(self, prompt: str) -> str:
result: Dict[str, Any] = {}
error: Dict[str, Any] = {}
def target():
try:
result["value"] = self.llm_fn(prompt)
except Exception as exc:
error["value"] = exc
thread = threading.Thread(target=target, daemon=True)
thread.start()
thread.join(timeout=self.timeout_seconds)
if thread.is_alive():
raise LLMTimeoutError(
f"LLM call exceeded {self.timeout_seconds}s timeout"
)
if "value" in error:
raise error["value"]
return result.get("value", "")
# =============================================================================
# The Control Layer Orchestrator
# =============================================================================
class ControlLayer:
"""
The orchestrator. Sits between your application logic and the LLM.
Composes all eight components:
InputGuard → TokenBudget → PromptBuilder →
CircuitBreaker → LLMCaller (timeout) → ResponseValidator →
RetryEngine (jittered backoff) → FallbackRouter → AuditLogger
Swap the LLM in one line:
layer = ControlLayer(llm_fn=your_api_call, ...)
The ControlPacket returned contains the final response, attempt count,
total latency, token usage, and a full audit ID for tracing.
"""
def __init__(
self,
llm_fn: Callable[[str], str],
system_prompt: str,
schema: Optional[ResponseSchema] = None,
config: Optional[ControlLayerConfig] = None,
# Legacy compat: accept total_tokens and max_attempts directly
total_tokens: Optional[int] = None,
max_attempts: Optional[int] = None,
):
self.config = config or ControlLayerConfig(
total_tokens=total_tokens or 800,
max_attempts=max_attempts or 3,
)
self.schema = schema or ResponseSchema()
self.input_guard = InputGuard(self.config.max_input_chars)
self.prompt_builder = PromptBuilder(system_prompt, self.config)
self.validator = ResponseValidator()
self.retry_engine = RetryEngine(self.config)
self.fallback_router = FallbackRouter()
self.circuit_breaker = CircuitBreaker(
self.config.cb_failure_threshold,
self.config.cb_recovery_seconds,
)
self.llm_caller = LLMCaller(llm_fn, self.config.timeout_seconds)
self.audit = AuditLogger(self.config.audit_log_path)
def register_fallback(self, name: str, fn: Callable[[str], str]) -> None:
self.fallback_router.register(name, fn)
def run(
self,
user_input: str,
constraints: Optional[List[str]] = None,
context: str = "",
) -> ControlPacket:
constraints = constraints or []
audit_id = self._make_audit_id(user_input)
start = time.perf_counter()
# ── Input Guard ────────────────────────────────────────────────────
guard_result = self.input_guard.validate(user_input)
if not guard_result.passed:
self._log_record(audit_id, 0, guard_result, 0.0, 0, RetryStrategy.NONE)
log.warning(
"input_guard.blocked",
failure_mode=guard_result.failure_mode.value,
message=guard_result.message[:80],
)
return self._packet("", "", 0, start, guard_result,
TokenBudget(self.config.total_tokens, self.config.model_name),
RetryStrategy.NONE, audit_id)
user_input = self.input_guard.sanitize(user_input)
# ── Circuit Breaker ────────────────────────────────────────────────
if self.circuit_breaker.is_open():
result = ValidationResult(
passed=False,
failure_mode=FailureMode.CIRCUIT_OPEN,
message="Circuit breaker is open. LLM backend unavailable.",
score=0.0,
)
self._log_record(audit_id, 0, result, 0.0, 0, RetryStrategy.NONE)
log.error("circuit_breaker.rejected", audit_id=audit_id)
return self._packet("", "", 0, start, result,
TokenBudget(self.config.total_tokens, self.config.model_name),
RetryStrategy.NONE, audit_id)
mutation_hint = ""
last_validation = ValidationResult(passed=False)
last_budget = TokenBudget(self.config.total_tokens, self.config.model_name)
attempt = 0
# ── Retry Loop ─────────────────────────────────────────────────────
for attempt in range(1, self.config.max_attempts + 1):
prompt, budget = self.prompt_builder.build(
user_input, constraints, context, mutation_hint
)
last_budget = budget
t0 = time.perf_counter()
try:
response = self.llm_caller.call(prompt)
self.circuit_breaker.record_success()
except LLMTimeoutError as exc:
call_latency_ms = (time.perf_counter() - t0) * 1000
self.circuit_breaker.record_failure()
timeout_result = ValidationResult(
passed=False,
failure_mode=FailureMode.TIMEOUT,
message=str(exc),
score=0.0,
)
self._log_record(audit_id, attempt, timeout_result,
call_latency_ms, budget.used(), RetryStrategy.NONE)
log.warning("llm.timeout", attempt=attempt, timeout_s=self.config.timeout_seconds)
last_validation = timeout_result
if self.retry_engine.should_retry(FailureMode.TIMEOUT, attempt):
mutation_hint = self.retry_engine.get_mutation_hint(FailureMode.TIMEOUT)
time.sleep(self.retry_engine.jittered_delay_s(attempt))
continue
break
except Exception as exc:
call_latency_ms = (time.perf_counter() - t0) * 1000
self.circuit_breaker.record_failure()
err_result = ValidationResult(
passed=False,
failure_mode=FailureMode.CONSTRAINT_VIOLATION,
message=f"LLM call error: {exc}",
score=0.0,
)
self._log_record(audit_id, attempt, err_result,
call_latency_ms, budget.used(), RetryStrategy.NONE)
log.error("llm.error", attempt=attempt, error=str(exc))
last_validation = err_result
break
call_latency_ms = (time.perf_counter() - t0) * 1000
validation = self.validator.validate(response, self.schema)
strategy = RetryStrategy.SIMPLE if attempt == 1 else RetryStrategy.PROMPT_MUTATION
self._log_record(audit_id, attempt, validation,
call_latency_ms, budget.used(), strategy)
if validation.passed:
log.info(
"llm.success",
audit_id=audit_id,
attempt=attempt,
latency_ms=round(call_latency_ms, 1),
score=validation.score,
)
return self._packet(prompt, response, attempt, start,
validation, budget, strategy, audit_id)
last_validation = validation
if not self.retry_engine.should_retry(validation.failure_mode, attempt):
log.warning(
"retry.skipped",
failure_mode=validation.failure_mode.value,
attempt=attempt,
)
break
mutation_hint = self.retry_engine.get_mutation_hint(validation.failure_mode)
delay_s = self.retry_engine.jittered_delay_s(attempt)
log.info(
"retry.scheduled",
attempt=attempt,
failure_mode=validation.failure_mode.value,
delay_ms=round(delay_s * 1000, 1),
)
time.sleep(delay_s)