|
| 1 | +"""Native policy model. |
| 2 | +
|
| 3 | +Rules, checks, conditions and pack indexes consumed by |
| 4 | +:class:`uipath.runtime.governance.native.evaluator.GovernanceEvaluator`. |
| 5 | +
|
| 6 | +These are the inputs of the native evaluator. The evaluator-agnostic |
| 7 | +*output* types (``Action``, ``AuditRecord``, …) live in |
| 8 | +:mod:`uipath.core.governance.models`. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from dataclasses import dataclass, field |
| 14 | +from enum import Enum |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +from uipath.core.governance.models import Action, LifecycleHook |
| 18 | + |
| 19 | + |
| 20 | +class Severity(Enum): |
| 21 | + """Rule severity levels.""" |
| 22 | + |
| 23 | + LOW = "low" |
| 24 | + MEDIUM = "medium" |
| 25 | + HIGH = "high" |
| 26 | + CRITICAL = "critical" |
| 27 | + |
| 28 | + |
| 29 | +@dataclass |
| 30 | +class Condition: |
| 31 | + """A single condition within a rule check.""" |
| 32 | + |
| 33 | + operator: str |
| 34 | + field: str |
| 35 | + value: Any |
| 36 | + negate: bool = False |
| 37 | + |
| 38 | + |
| 39 | +@dataclass |
| 40 | +class Check: |
| 41 | + """A check within a rule - contains conditions and action.""" |
| 42 | + |
| 43 | + conditions: list[Condition] |
| 44 | + action: Action = Action.DENY |
| 45 | + message: str = "" |
| 46 | + logic: str = "all" # "all" (AND) or "any" (OR) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass |
| 50 | +class Rule: |
| 51 | + """A compliance rule with checks evaluated at a specific lifecycle hook.""" |
| 52 | + |
| 53 | + rule_id: str |
| 54 | + name: str |
| 55 | + clause: str |
| 56 | + hook: LifecycleHook |
| 57 | + action: Action |
| 58 | + severity: Severity = Severity.HIGH |
| 59 | + checks: list[Check] = field(default_factory=list) |
| 60 | + enabled: bool = True |
| 61 | + description: str = "" |
| 62 | + pack_name: str = "" |
| 63 | + |
| 64 | + # Approval configuration (for ESCALATE action) |
| 65 | + approval_config: dict[str, Any] = field(default_factory=dict) |
| 66 | + |
| 67 | + |
| 68 | +@dataclass |
| 69 | +class CheckContext: |
| 70 | + """Context passed to rule evaluation.""" |
| 71 | + |
| 72 | + hook: LifecycleHook |
| 73 | + agent_name: str |
| 74 | + runtime_id: str |
| 75 | + trace_id: str |
| 76 | + |
| 77 | + # Content fields (populated based on hook) |
| 78 | + agent_input: str = "" |
| 79 | + agent_output: str = "" |
| 80 | + model_input: str = "" |
| 81 | + model_output: str = "" |
| 82 | + model_name: str = ( |
| 83 | + "" # LLM model name (e.g., "gpt-4", "claude-3-opus") - available at agent start |
| 84 | + ) |
| 85 | + tool_name: str = "" |
| 86 | + tool_args: dict[str, Any] = field(default_factory=dict) |
| 87 | + tool_result: str = "" |
| 88 | + messages: list[dict[str, Any]] = field(default_factory=list) |
| 89 | + |
| 90 | + # Session state |
| 91 | + session_state: dict[str, Any] = field(default_factory=dict) |
| 92 | + metadata: dict[str, Any] = field(default_factory=dict) |
| 93 | + |
| 94 | + # Ring level (privilege level: 0=system, 1=admin, 2=user, 3=untrusted) |
| 95 | + ring: int = 2 |
| 96 | + |
| 97 | + |
| 98 | +@dataclass |
| 99 | +class PolicyPack: |
| 100 | + """A collection of rules for a compliance standard.""" |
| 101 | + |
| 102 | + name: str |
| 103 | + version: str |
| 104 | + description: str |
| 105 | + rules: list[Rule] |
| 106 | + enabled: bool = True |
| 107 | + |
| 108 | + |
| 109 | +@dataclass |
| 110 | +class PolicyIndex: |
| 111 | + """Index of all loaded policy packs and rules.""" |
| 112 | + |
| 113 | + packs: dict[str, PolicyPack] = field(default_factory=dict) |
| 114 | + _rules_by_id: dict[str, Rule] = field(default_factory=dict) |
| 115 | + _rules_by_hook: dict[LifecycleHook, list[Rule]] = field(default_factory=dict) |
| 116 | + |
| 117 | + def add_pack(self, pack: PolicyPack) -> None: |
| 118 | + """Add a policy pack to the index.""" |
| 119 | + self.packs[pack.name] = pack |
| 120 | + for rule in pack.rules: |
| 121 | + rule.pack_name = pack.name |
| 122 | + self._rules_by_id[rule.rule_id] = rule |
| 123 | + if rule.hook not in self._rules_by_hook: |
| 124 | + self._rules_by_hook[rule.hook] = [] |
| 125 | + self._rules_by_hook[rule.hook].append(rule) |
| 126 | + |
| 127 | + def get_rule(self, rule_id: str) -> Rule | None: |
| 128 | + """Get a rule by ID.""" |
| 129 | + return self._rules_by_id.get(rule_id) |
| 130 | + |
| 131 | + def get_rules_for_hook(self, hook: LifecycleHook) -> list[Rule]: |
| 132 | + """Get all rules for a lifecycle hook.""" |
| 133 | + return self._rules_by_hook.get(hook, []) |
| 134 | + |
| 135 | + def get_rules_for_pack(self, pack_name: str) -> list[Rule]: |
| 136 | + """Get all rules for a pack.""" |
| 137 | + pack = self.packs.get(pack_name) |
| 138 | + return pack.rules if pack else [] |
| 139 | + |
| 140 | + @property |
| 141 | + def pack_names(self) -> list[str]: |
| 142 | + """Get all pack names.""" |
| 143 | + return list(self.packs.keys()) |
| 144 | + |
| 145 | + @property |
| 146 | + def total_rules(self) -> int: |
| 147 | + """Get total number of rules.""" |
| 148 | + return len(self._rules_by_id) |
| 149 | + |
| 150 | + @property |
| 151 | + def all_rules(self) -> list[Rule]: |
| 152 | + """Get all rules.""" |
| 153 | + return list(self._rules_by_id.values()) |
0 commit comments