Repository navigation
Expand file tree
/
Copy pathcontrols.py
More file actions
973 lines (806 loc) · 32.8 KB
/
Copy pathcontrols.py
File metadata and controls
973 lines (806 loc) · 32.8 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
"""Control definition models for agent protection."""
from __future__ import annotations
import re
from collections.abc import Iterator
from dataclasses import dataclass
from typing import Annotated, Any, Literal, Self
from uuid import uuid4
import re2
from pydantic import ConfigDict, Field, ValidationInfo, field_validator, model_validator
from .actions import ActionDecision, normalize_action, validate_action
from .agent import JSONValue
from .base import BaseModel
class ControlSelector(BaseModel):
"""Selects data from a Step payload.
- path: which slice of the Step to feed into the evaluator. Optional, defaults to "*"
meaning the entire Step object.
"""
path: str | None = Field(
default="*",
description=(
"Path to data using dot notation. "
"Examples: 'input', 'output', 'context.user_id', 'name', 'type', '*'"
),
)
@field_validator("path")
@classmethod
def validate_path(cls, v: str | None) -> str:
"""Validate path; None becomes '*', empty string raises."""
if v is None:
return "*"
if v == "":
raise ValueError(
"Path cannot be empty string. Use '*' for root or omit the field."
)
# Valid root fields
valid_roots = {"input", "output", "name", "type", "context", "*"}
root = v.split(".")[0]
if root not in valid_roots:
raise ValueError(
f"Invalid path root '{root}'. "
f"Must be one of: {', '.join(sorted(valid_roots))}"
)
return v
model_config = {
"json_schema_extra": {
"examples": [
{"path": "output"},
{"path": "context.user_id"},
{"path": "input"},
{"path": "*"},
{"path": "name"},
{"path": "output"},
]
}
}
class ControlScope(BaseModel):
"""Defines when a control applies to a Step."""
step_types: list[str] | None = Field(
default=None,
description=(
"Step types this control applies to (omit to apply to all types). "
"Built-in types are 'tool' and 'llm'."
),
examples=[["llm"], ["tool"], ["llm", "tool"]],
)
step_names: list[str] | None = Field(
default=None,
description="Exact step names this control applies to",
)
step_name_regex: str | None = Field(
default=None,
description="RE2 pattern matched with search() against step name",
)
stages: list[Literal["pre", "post"]] | None = Field(
default=None,
description="Evaluation stages this control applies to",
)
@field_validator("step_types")
@classmethod
def validate_step_types(
cls, v: list[str] | None
) -> list[str] | None:
if v is None:
return v
if len(v) == 0:
raise ValueError(
"step_types cannot be an empty list. Use None/omit the field to apply to all types."
)
if any((not isinstance(x, str) or not x) for x in v):
raise ValueError("step_types must be a list of non-empty strings")
return v
@field_validator("step_names")
@classmethod
def validate_step_names(cls, v: list[str] | None) -> list[str] | None:
if v is None:
return v
if len(v) == 0:
raise ValueError(
"step_names cannot be an empty list. Use None/omit the field to apply to all steps."
)
if any((not isinstance(x, str) or not x) for x in v):
raise ValueError("step_names must be a list of non-empty strings")
return v
@field_validator("step_name_regex")
@classmethod
def validate_step_name_regex(
cls, v: str | None, info: ValidationInfo
) -> str | None:
if v is None:
return v
if info.context and info.context.get("allow_invalid_step_name_regex"):
return v
try:
re2.compile(v)
except re2.error as e:
raise ValueError(f"Invalid step_name_regex: {e}") from e
return v
@field_validator("stages")
@classmethod
def validate_stages(
cls, v: list[Literal["pre", "post"]] | None
) -> list[Literal["pre", "post"]] | None:
if v is None:
return v
if len(v) == 0:
raise ValueError(
"stages cannot be an empty list. Use None/omit the field to apply to all stages."
)
return v
model_config = {
"json_schema_extra": {
"examples": [
{"step_types": ["tool"], "stages": ["pre"]},
{"step_names": ["search_db", "fetch_user"]},
{"step_name_regex": "^db_.*"},
{"step_types": ["llm"], "stages": ["post"]},
]
}
}
# =============================================================================
# Unified Evaluator Spec (used in API)
# =============================================================================
class EvaluatorSpec(BaseModel):
"""Evaluator specification. See GET /evaluators for available evaluators and schemas.
Evaluator reference formats:
- Built-in: "regex", "list", "json", "sql"
- External: "galileo.luna" (requires agent-control-evaluators[galileo])
- Agent-scoped: "my-agent:my-evaluator" (validated in endpoint, not here)
"""
name: str = Field(
...,
min_length=1,
description="Evaluator name or agent-scoped reference (agent:evaluator)",
examples=["regex", "list", "my-agent:pii-detector"],
)
config: dict[str, Any] = Field(
...,
description="Evaluator-specific configuration",
examples=[
{"pattern": r"\d{3}-\d{2}-\d{4}"},
{"values": ["admin"], "logic": "any"},
],
)
@field_validator("name", mode="before")
@classmethod
def validate_name(cls, value: str) -> str:
if not isinstance(value, str):
return value
normalized = value.strip()
if not normalized:
raise ValueError("Evaluator name cannot be empty or whitespace-only.")
return normalized
@model_validator(mode="after")
def validate_evaluator_config(self) -> Self:
"""Validate config against evaluator's schema if evaluator is registered.
Agent-scoped evaluators (format: agent:evaluator) are validated in the
endpoint where we have database access to look up the agent's schema.
"""
# Agent-scoped evaluators: defer validation to endpoint (needs DB access)
if ":" in self.name:
return self
# Built-in evaluators: validate config against evaluator's config_model
# This import is optional - evaluators package may not be installed
try:
from agent_control_evaluators import ensure_evaluators_discovered, get_evaluator
# Ensure entry points are loaded before looking up evaluator
ensure_evaluators_discovered()
evaluator_cls = get_evaluator(self.name)
if evaluator_cls:
evaluator_cls.config_model(**self.config)
except ImportError:
# Evaluators package not installed - skip validation
pass
# If evaluator not found, allow it (might be a server-side registered evaluator)
return self
type ConditionLeafParts = tuple[ControlSelector, EvaluatorSpec]
@dataclass(frozen=True)
class ControlObservabilityIdentity:
"""Stable selector/evaluator identity derived from a condition tree."""
selector_path: str | None
evaluator_name: str | None
leaf_count: int
all_evaluators: list[str]
all_selector_paths: list[str]
class SteeringContext(BaseModel):
"""Steering context for steer actions.
This model provides an extensible structure for steering guidance.
Future fields could include severity, categories, suggested_actions, etc.
"""
message: str = Field(
...,
description="Guidance message explaining what needs to be corrected and how"
)
model_config = {
"json_schema_extra": {
"examples": [
{
"message": (
"This large transfer requires user verification. "
"Request 2FA code from user, verify it, then retry "
"the transaction with verified_2fa=True."
)
},
{
"message": (
"Transfer exceeds daily limit. Steps: "
"1) Ask user for business justification, "
"2) Request manager approval with amount and justification, "
"3) If approved, retry with manager_approved=True and "
"justification filled in."
)
}
]
}
}
type TemplateValue = str | bool | list[str]
type JsonValue = JSONValue
MAX_CONDITION_DEPTH = 12
_TEMPLATE_PARAMETER_NAME_RE = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
MAX_TEMPLATE_DEFINITION_DEPTH = MAX_CONDITION_DEPTH
MAX_TEMPLATE_DEFINITION_NODES = 1000
def _validate_re2_value(value: str, *, field_name: str) -> str:
"""Validate that a string is a valid RE2 expression."""
try:
re2.compile(value)
except re2.error as exc:
raise ValueError(f"Invalid {field_name}: {exc}") from exc
return value
def _validate_template_definition_structure(value: JsonValue) -> JsonValue:
"""Validate template definition nesting and overall size."""
stack: list[tuple[JsonValue, int]] = [(value, 1)]
node_count = 0
while stack:
node, depth = stack.pop()
node_count += 1
if depth > MAX_TEMPLATE_DEFINITION_DEPTH:
raise ValueError(
"definition_template nesting depth exceeds maximum of "
f"{MAX_TEMPLATE_DEFINITION_DEPTH}"
)
if node_count > MAX_TEMPLATE_DEFINITION_NODES:
raise ValueError(
"definition_template size exceeds maximum of "
f"{MAX_TEMPLATE_DEFINITION_NODES} JSON nodes"
)
if isinstance(node, dict):
stack.extend((nested_value, depth + 1) for nested_value in node.values())
elif isinstance(node, list):
# List elements inherit parent depth — a flat array of strings is
# not structurally deeper than a single value.
stack.extend((nested_value, depth) for nested_value in node)
return value
class TemplateParameterBase(BaseModel):
"""Base definition for a template parameter."""
label: str = Field(..., min_length=1, description="Human-readable parameter label")
description: str | None = Field(
None,
description="Optional description of what the parameter controls",
)
required: bool = Field(
True,
description="Whether the caller must provide a value when no default exists",
)
ui_hint: str | None = Field(
None,
description="Optional UI hint for rendering the parameter input",
)
class StringTemplateParameter(TemplateParameterBase):
"""String-valued template parameter."""
type: Literal["string"] = "string"
default: str | None = Field(None, description="Optional default value")
placeholder: str | None = Field(None, description="Optional placeholder text")
class StringListTemplateParameter(TemplateParameterBase):
"""List-of-strings template parameter."""
type: Literal["string_list"] = "string_list"
default: list[str] | None = Field(None, description="Optional default value")
placeholder: list[str] | None = Field(
None,
description="Optional placeholder/example list",
)
@field_validator("default", "placeholder")
@classmethod
def validate_string_lists(
cls, value: list[str] | None
) -> list[str] | None:
if value is None:
return value
if any((not isinstance(item, str)) for item in value):
raise ValueError("Values must be strings")
return value
class EnumTemplateParameter(TemplateParameterBase):
"""String enum template parameter."""
type: Literal["enum"] = "enum"
allowed_values: list[str] = Field(
...,
min_length=1,
description="Allowed string values for the parameter",
)
default: str | None = Field(None, description="Optional default value")
@field_validator("allowed_values")
@classmethod
def validate_allowed_values(cls, value: list[str]) -> list[str]:
if not value:
raise ValueError("allowed_values must not be empty")
if any(not item for item in value):
raise ValueError("allowed_values must contain non-empty strings")
if len(set(value)) != len(value):
raise ValueError("allowed_values must not contain duplicates")
return value
@model_validator(mode="after")
def validate_default(self) -> Self:
if self.default is not None and self.default not in self.allowed_values:
raise ValueError("Default must be one of allowed_values")
return self
class BooleanTemplateParameter(TemplateParameterBase):
"""Boolean template parameter."""
type: Literal["boolean"] = "boolean"
default: bool | None = Field(None, description="Optional default value")
class RegexTemplateParameter(TemplateParameterBase):
"""RE2 regex template parameter."""
type: Literal["regex_re2"] = "regex_re2"
default: str | None = Field(None, description="Optional default regex pattern")
placeholder: str | None = Field(None, description="Optional placeholder regex")
@field_validator("default", "placeholder")
@classmethod
def validate_regex_values(cls, value: str | None) -> str | None:
if value is None:
return value
return _validate_re2_value(value, field_name="regex pattern")
type TemplateParameterDefinition = Annotated[
StringTemplateParameter
| StringListTemplateParameter
| EnumTemplateParameter
| BooleanTemplateParameter
| RegexTemplateParameter,
Field(discriminator="type"),
]
class TemplateDefinition(BaseModel):
"""Reusable template with typed parameters and a JSON definition template."""
description: str | None = Field(
None,
description="Metadata describing the template itself",
)
parameters: dict[str, TemplateParameterDefinition] = Field(
default_factory=dict,
description="Typed parameter definitions keyed by parameter name",
)
definition_template: JsonValue = Field(
...,
description="Template payload containing $param binding objects",
)
@field_validator("parameters")
@classmethod
def validate_parameter_names(
cls, value: dict[str, TemplateParameterDefinition]
) -> dict[str, TemplateParameterDefinition]:
for name in value:
if not _TEMPLATE_PARAMETER_NAME_RE.fullmatch(name):
raise ValueError(
"Parameter names must match [a-zA-Z_][a-zA-Z0-9_]*"
)
return value
@field_validator("definition_template")
@classmethod
def validate_definition_template_structure(cls, value: JsonValue) -> JsonValue:
"""Limit template nesting and size to keep rendering bounded."""
return _validate_template_definition_structure(value)
class TemplateControlInput(BaseModel):
"""Template-backed input payload for control create/update requests."""
model_config = ConfigDict(extra="forbid")
template: TemplateDefinition = Field(..., description="Template definition to render")
template_values: dict[str, TemplateValue] = Field(
default_factory=dict,
description="Template parameter values keyed by parameter name",
)
class UnrenderedTemplateControl(BaseModel):
"""Stored state of a template control that hasn't been rendered yet.
An unrendered template has a template definition and possibly partial
parameter values, but no concrete condition/action/execution fields.
It is always ``enabled=False`` and excluded from evaluation.
"""
template: TemplateDefinition = Field(
..., description="Template definition awaiting parameter values"
)
template_values: dict[str, TemplateValue] = Field(
default_factory=dict,
description="Partial or empty parameter values",
)
enabled: Literal[False] = Field(
False,
description="Unrendered templates are always disabled",
)
class ControlAction(BaseModel):
"""What to do when control matches."""
decision: ActionDecision = Field(
..., description="Action to take when control is triggered"
)
steering_context: SteeringContext | None = Field(
None,
description=(
"Steering context object for steer actions. Strongly recommended when "
"decision='steer' to provide correction suggestions. If not provided, the "
"evaluator result message will be used as fallback."
)
)
@field_validator("decision", mode="before")
@classmethod
def validate_decision(cls, value: str) -> ActionDecision:
return validate_action(value)
def _validate_common_control_constraints(
condition: ConditionNode,
action: ControlAction,
) -> None:
"""Validate control constraints shared by authoring and runtime models."""
if condition.max_depth() > MAX_CONDITION_DEPTH:
raise ValueError(
f"Condition nesting depth exceeds maximum of {MAX_CONDITION_DEPTH}"
)
if (
action.decision == "steer"
and not condition.is_leaf()
and action.steering_context is None
):
raise ValueError("Composite steer controls require action.steering_context")
class ConditionNode(BaseModel):
"""Recursive boolean condition tree for control evaluation."""
selector: ControlSelector | None = Field(
default=None,
description="Leaf selector. Must be provided together with evaluator.",
)
evaluator: EvaluatorSpec | None = Field(
default=None,
description="Leaf evaluator. Must be provided together with selector.",
)
and_: list[ConditionNode] | None = Field(
default=None,
alias="and",
serialization_alias="and",
description="Logical AND over child conditions.",
)
or_: list[ConditionNode] | None = Field(
default=None,
alias="or",
serialization_alias="or",
description="Logical OR over child conditions.",
)
not_: ConditionNode | None = Field(
default=None,
alias="not",
serialization_alias="not",
description="Logical NOT over a single child condition.",
)
model_config = ConfigDict(
populate_by_name=True,
use_enum_values=True,
validate_assignment=True,
extra="ignore",
serialize_by_alias=True,
)
@model_validator(mode="after")
def validate_shape(self) -> Self:
"""Ensure each node is exactly one of leaf/and/or/not."""
has_selector = self.selector is not None
has_evaluator = self.evaluator is not None
has_leaf = has_selector and has_evaluator
if has_selector != has_evaluator:
raise ValueError("Leaf condition requires both selector and evaluator")
populated = sum(
1
for present in (
has_leaf,
self.and_ is not None,
self.or_ is not None,
self.not_ is not None,
)
if present
)
if populated != 1:
raise ValueError("Condition node must contain exactly one of leaf, and, or, not")
if self.and_ is not None and len(self.and_) == 0:
raise ValueError("'and' must contain at least one child condition")
if self.or_ is not None and len(self.or_) == 0:
raise ValueError("'or' must contain at least one child condition")
return self
def kind(self) -> Literal["leaf", "and", "or", "not"]:
"""Return the logical node type."""
if self.is_leaf():
return "leaf"
if self.and_ is not None:
return "and"
if self.or_ is not None:
return "or"
return "not"
def is_leaf(self) -> bool:
"""Return True when this node is a leaf selector/evaluator pair."""
return self.selector is not None and self.evaluator is not None
def children_in_order(self) -> list[ConditionNode]:
"""Return child conditions in evaluation order."""
if self.and_ is not None:
return self.and_
if self.or_ is not None:
return self.or_
if self.not_ is not None:
return [self.not_]
return []
def iter_leaves(self) -> Iterator[ConditionNode]:
"""Yield leaf nodes in left-to-right traversal order."""
if self.is_leaf():
yield self
return
for child in self.children_in_order():
yield from child.iter_leaves()
def iter_leaf_parts(self) -> Iterator[ConditionLeafParts]:
"""Yield leaf selector/evaluator pairs in left-to-right traversal order."""
leaf_parts = self.leaf_parts()
if leaf_parts is not None:
yield leaf_parts
return
for child in self.children_in_order():
yield from child.iter_leaf_parts()
def max_depth(self) -> int:
"""Return the maximum nesting depth of this condition tree."""
children = self.children_in_order()
if not children:
return 1
return 1 + max(child.max_depth() for child in children)
def leaf_parts(self) -> ConditionLeafParts | None:
"""Return the selector/evaluator pair for leaf nodes."""
if not self.is_leaf():
return None
selector = self.selector
evaluator = self.evaluator
if selector is None or evaluator is None:
return None
return selector, evaluator
model_config["json_schema_extra"] = {
"examples": [
{
"selector": {"path": "output"},
"evaluator": {"name": "regex", "config": {"pattern": r"\d{3}-\d{2}-\d{4}"}},
},
{
"and": [
{
"selector": {"path": "context.risk_level"},
"evaluator": {
"name": "list",
"config": {"values": ["high", "critical"]},
},
},
{
"not": {
"selector": {"path": "context.user_role"},
"evaluator": {
"name": "list",
"config": {"values": ["admin", "security"]},
},
}
},
]
},
]
}
ConditionNode.model_rebuild()
def _build_observability_identity(
condition: ConditionNode,
) -> ControlObservabilityIdentity:
"""Build a stable selector/evaluator identity for a condition tree."""
all_evaluators: list[str] = []
all_selector_paths: list[str] = []
seen_evaluators: set[str] = set()
seen_selector_paths: set[str] = set()
leaf_count = 0
for selector, evaluator in condition.iter_leaf_parts():
leaf_count += 1
selector_path = selector.path or "*"
if evaluator.name not in seen_evaluators:
seen_evaluators.add(evaluator.name)
all_evaluators.append(evaluator.name)
if selector_path not in seen_selector_paths:
seen_selector_paths.add(selector_path)
all_selector_paths.append(selector_path)
return ControlObservabilityIdentity(
selector_path=all_selector_paths[0] if all_selector_paths else None,
evaluator_name=all_evaluators[0] if all_evaluators else None,
leaf_count=leaf_count,
all_evaluators=all_evaluators,
all_selector_paths=all_selector_paths,
)
class _ConditionBackedControlMixin:
"""Shared helpers for control models backed by a condition tree."""
condition: ConditionNode
def iter_condition_leaves(self) -> Iterator[ConditionNode]:
"""Yield leaf conditions in evaluation order."""
yield from self.condition.iter_leaves()
def iter_condition_leaf_parts(self) -> Iterator[ConditionLeafParts]:
"""Yield leaf selector/evaluator pairs in evaluation order."""
yield from self.condition.iter_leaf_parts()
def observability_identity(self) -> ControlObservabilityIdentity:
"""Return a deterministic representative identity for observability."""
return _build_observability_identity(self.condition)
def canonicalize_control_payload(data: Any) -> Any:
"""Rewrite legacy selector/evaluator payloads into canonical condition shape."""
if not isinstance(data, dict):
return data
has_condition = "condition" in data
has_selector = "selector" in data
has_evaluator = "evaluator" in data
if has_condition and (has_selector or has_evaluator):
raise ValueError(
"Control definition mixes canonical condition fields "
"with legacy selector/evaluator fields."
)
if has_selector != has_evaluator:
raise ValueError(
"Legacy control definition must include both selector and evaluator."
)
if not has_condition and has_selector:
canonical = dict(data)
selector = canonical.pop("selector")
evaluator = canonical.pop("evaluator")
canonical["condition"] = {
"selector": selector,
"evaluator": evaluator,
}
return canonical
return data
class ControlDefinitionBase(_ConditionBackedControlMixin, BaseModel):
"""Runtime-relevant fields shared by authored and runtime controls."""
description: str | None = Field(None, description="Detailed description of the control")
enabled: bool = Field(True, description="Whether this control is active")
execution: Literal["server", "sdk"] = Field(
..., description="Where this control executes"
)
# When to apply
scope: ControlScope = Field(
default_factory=ControlScope,
description="Which steps and stages this control applies to",
)
# What to check
condition: ConditionNode = Field(
...,
description=(
"Recursive boolean condition tree. Leaf nodes contain selector + evaluator; "
"composite nodes contain and/or/not."
),
)
# What to do
action: ControlAction = Field(..., description="What action to take when control matches")
# Metadata
tags: list[str] = Field(default_factory=list, description="Tags for categorization")
@classmethod
def canonicalize_payload(cls, data: Any) -> Any:
"""Rewrite legacy selector/evaluator payloads into canonical condition shape."""
return canonicalize_control_payload(data)
@model_validator(mode="before")
@classmethod
def canonicalize_legacy_condition_shape(cls, data: Any) -> Any:
"""Accept legacy flat leaf payloads during condition-tree rollout."""
return canonicalize_control_payload(data)
@model_validator(mode="after")
def validate_condition_constraints(self) -> Self:
"""Validate runtime-relevant control constraints."""
_validate_common_control_constraints(self.condition, self.action)
return self
class ControlDefinition(ControlDefinitionBase):
"""A control definition to evaluate agent interactions.
This model contains only the logic and configuration.
Identity fields (id, name) are managed by the database.
"""
template: TemplateDefinition | None = Field(
None,
description="Template metadata for template-backed controls",
)
template_values: dict[str, TemplateValue] | None = Field(
None,
description="Resolved parameter values for template-backed controls",
)
@model_validator(mode="after")
def validate_template_pairing(self) -> Self:
"""Validate authoring-only template constraints."""
has_template = self.template is not None
has_template_values = self.template_values is not None
if has_template != has_template_values:
raise ValueError(
"template and template_values must both be present or both absent"
)
return self
def to_template_control_input(self) -> TemplateControlInput:
"""Extract template-backed authoring input from a stored control definition."""
if self.template is None or self.template_values is None:
raise ValueError("Control definition is not template-backed")
return TemplateControlInput(
template=self.template,
template_values=dict(self.template_values),
)
def primary_leaf(self) -> ConditionNode | None:
"""Return the single leaf node when the whole condition is just one leaf."""
if self.condition.is_leaf():
return self.condition
return None
model_config = {
"json_schema_extra": {
"examples": [
{
"description": "Block outputs containing US Social Security Numbers",
"enabled": True,
"execution": "server",
"scope": {"step_types": ["llm"], "stages": ["post"]},
"condition": {
"selector": {"path": "output"},
"evaluator": {
"name": "regex",
"config": {
"pattern": r"\b\d{3}-\d{2}-\d{4}\b",
},
},
},
"action": {
"decision": "deny",
},
"tags": ["pii", "compliance"],
}
]
}
}
class ControlDefinitionRuntime(ControlDefinitionBase):
"""Slim runtime control model that ignores template authoring metadata."""
model_config = ConfigDict(extra="ignore")
class EvaluatorResult(BaseModel):
"""Result from a control evaluator.
The `error` field indicates evaluator failures, NOT validation failures:
- Set `error` for: evaluator crashes, timeouts, missing dependencies, external service errors
- Do NOT set `error` for: invalid input, syntax errors, schema violations, constraint failures
When `error` is set, `matched` must be False (fail-open on evaluator errors).
When `error` is None, `matched` reflects the actual validation result.
This distinction allows:
- Clients to distinguish "data violated rules" from "evaluator is broken"
- Observability systems to monitor evaluator health separately from validation outcomes
"""
matched: bool = Field(..., description="Whether the pattern matched")
confidence: float = Field(
..., ge=0.0, le=1.0, description="Confidence in the evaluation"
)
message: str | None = Field(default=None, description="Explanation of the result")
metadata: dict[str, Any] | None = Field(default=None, description="Additional result metadata")
error: str | None = Field(
default=None,
description=(
"Error message if evaluation failed internally. "
"When set, matched=False is due to error, not actual evaluation."
),
)
@model_validator(mode="after")
def error_implies_not_matched(self) -> Self:
"""Ensure matched=False when error is set (fail-open on errors)."""
if self.error is not None and self.matched:
raise ValueError("matched must be False when error is set")
return self
class ControlMatch(BaseModel):
"""Represents a control evaluation result (match, non-match, or error)."""
control_execution_id: str = Field(
default_factory=lambda: str(uuid4()),
description="Unique ID for this control execution (generated by engine)",
)
control_id: int = Field(..., description="Database ID of the control")
control_name: str = Field(..., description="Name of the control")
action: ActionDecision = Field(
..., description="Action configured for this control"
)
result: EvaluatorResult = Field(
..., description="Evaluator result (confidence, message, metadata)"
)
execution_duration_ms: float | None = Field(
default=None,
ge=0,
description="Control evaluation wall-clock duration in milliseconds, if executed",
)
steering_context: SteeringContext | None = Field(
None,
description="Steering context for steer actions if configured"
)
@field_validator("action", mode="before")
@classmethod
def normalize_action_value(cls, value: str) -> ActionDecision:
return normalize_action(value)