-
Notifications
You must be signed in to change notification settings - Fork 28
feat(platform): add GovernanceService for agenticgovernance_ ingress #1738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
packages/uipath-core/src/uipath/core/governance/providers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| """Provider protocols for governance backend interactions. | ||
|
|
||
| The runtime needs two backend interactions to function: | ||
|
|
||
| - Fetching the policy pack at startup. | ||
| - Firing the compensating ``/runtime/govern`` POST when a | ||
| ``guardrail_fallback`` rule matches so the server can run the disabled | ||
| centralised guardrail and write the per-rule LLMOps audit records. | ||
|
|
||
| Both have wire formats owned by the ``agenticgovernance_`` ingress. | ||
| Defining the contracts here — alongside :class:`EvaluatorProtocol` — | ||
| lets runtime consumers depend on stable protocols and receive a | ||
| concrete provider via constructor injection. Concrete providers live | ||
| outside this package; ``uipath-core`` does not import them. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Protocol, runtime_checkable | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field, field_validator | ||
|
|
||
| from .models import EnforcementMode | ||
|
|
||
| # ---------------------------------------------------------------------- | ||
| # Wire-format models | ||
| # ---------------------------------------------------------------------- | ||
|
|
||
|
|
||
| class PolicyContext(BaseModel): | ||
| """Caller-supplied selectors for the policy fetch. | ||
|
|
||
| Wrapping the selectors in a model keeps the protocol surface stable | ||
| when the server grows new selector dimensions — adding a field here | ||
| doesn't change :meth:`GovernancePolicyProvider.get_policy`. | ||
|
|
||
| Today carries only :attr:`is_conversational`; future selectors land | ||
| here. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(extra="ignore") | ||
|
|
||
| is_conversational: bool | None = None | ||
|
|
||
|
|
||
| class PolicyResponse(BaseModel): | ||
| """Parsed governance backend response. | ||
|
|
||
| Wire envelope:: | ||
|
|
||
| { | ||
| "mode": "audit" | "enforce" | "disabled", | ||
| "policies": "<YAML string>" | ||
| } | ||
|
|
||
| Attributes: | ||
| mode: Platform-controlled enforcement mode for the tenant. May | ||
| be ``None`` when the backend omits it. A wire value the SDK | ||
| doesn't know about parses as ``None`` rather than raising, | ||
| so a server-side mode addition can't break agent startup. | ||
| policies: Policy pack YAML the caller compiles into its policy | ||
| index. May be an empty string when no rules are configured. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(extra="ignore") | ||
|
|
||
| mode: EnforcementMode | None = Field(default=None) | ||
| policies: str = Field(default="") | ||
|
|
||
| @field_validator("mode", mode="before") | ||
| @classmethod | ||
| def _coerce_mode(cls, value: object) -> EnforcementMode | None: | ||
| if value is None or isinstance(value, EnforcementMode): | ||
| return value | ||
| try: | ||
| return EnforcementMode(value) | ||
| except ValueError: | ||
| return None | ||
|
|
||
|
|
||
| class FiredRule(BaseModel): | ||
| """Per-rule metadata carried in the ``/runtime/govern`` payload. | ||
|
|
||
| One entry per matching ``guardrail_fallback`` condition. The server | ||
| writes one LLMOps trace record per entry, so callers must include | ||
| every fired rule even when multiple share the same ``validator``. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| rule_id: str = Field(alias="ruleId") | ||
| rule_name: str = Field(alias="ruleName") | ||
| pack_name: str = Field(alias="packName") | ||
| validator: str | ||
|
|
||
|
|
||
| class GovernRequest(BaseModel): | ||
| """Request body for the ``/runtime/govern`` compensating governance POST. | ||
|
|
||
| Field aliases match the on-the-wire JSON keys. ``src_timestamp`` is | ||
| snake_case on the wire (intentional — preserved verbatim); every | ||
| other key is camelCase. | ||
|
|
||
| Job-context fields (``folder_key`` / ``job_key`` / ``process_key`` / | ||
| ``reference_id`` / ``agent_version``) are optional; callers omit | ||
| them by leaving them ``None``. How unset fields are resolved (e.g. | ||
| auto-filled from environment) is the concrete provider's concern, | ||
| not part of this wire contract. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(populate_by_name=True) | ||
|
|
||
| validators: list[str] = Field(alias="type") | ||
| rules: list[FiredRule] | ||
| data: dict[str, Any] | ||
| hook: str | ||
| trace_id: str = Field(alias="traceId") | ||
| src_timestamp: str # wire key is intentionally snake_case | ||
| agent_name: str = Field(alias="agentName") | ||
| runtime_id: str = Field(alias="runtimeId") | ||
|
|
||
| folder_key: str | None = Field(default=None, alias="folderKey") | ||
| job_key: str | None = Field(default=None, alias="jobKey") | ||
| process_key: str | None = Field(default=None, alias="processKey") | ||
| reference_id: str | None = Field(default=None, alias="referenceId") | ||
| agent_version: str | None = Field(default=None, alias="agentVersion") | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------- | ||
| # Provider protocols | ||
| # ---------------------------------------------------------------------- | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class GovernancePolicyProvider(Protocol): | ||
| """Contract for fetching the governance policy pack. | ||
|
|
||
| Any object exposing a ``get_policy(context) -> PolicyResponse`` | ||
| method satisfies this protocol. | ||
| """ | ||
|
|
||
| def get_policy(self, context: PolicyContext) -> PolicyResponse: | ||
|
viswa-uipath marked this conversation as resolved.
|
||
| """Fetch the policy pack for the active org/tenant.""" | ||
| ... | ||
|
|
||
|
|
||
| @runtime_checkable | ||
| class GovernanceCompensationProvider(Protocol): | ||
| """Contract for firing the compensating ``/runtime/govern`` POST.""" | ||
|
|
||
| def compensate(self, request: GovernRequest) -> None: | ||
| """Fire the compensating governance POST. Fire-and-forget.""" | ||
| ... | ||
149 changes: 149 additions & 0 deletions
149
packages/uipath-core/tests/governance/test_providers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| """Tests for the governance provider protocols + wire-format models.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from uipath.core.governance import ( | ||
| EnforcementMode, | ||
| FiredRule, | ||
| GovernanceCompensationProvider, | ||
| GovernancePolicyProvider, | ||
| GovernRequest, | ||
| PolicyContext, | ||
| PolicyResponse, | ||
| ) | ||
|
|
||
|
|
||
| class _FakePolicyProvider: | ||
| def __init__(self) -> None: | ||
| self.calls: list[PolicyContext] = [] | ||
|
|
||
| def get_policy(self, context: PolicyContext) -> PolicyResponse: | ||
| self.calls.append(context) | ||
| return PolicyResponse(mode=EnforcementMode.ENFORCE, policies="rules: []") | ||
|
|
||
|
|
||
| class _FakeCompensationProvider: | ||
| def __init__(self) -> None: | ||
| self.calls: list[GovernRequest] = [] | ||
|
|
||
| def compensate(self, request: GovernRequest) -> None: | ||
| self.calls.append(request) | ||
|
|
||
|
|
||
| def _make_request() -> GovernRequest: | ||
| return GovernRequest( | ||
| validators=["pii_detection"], | ||
| rules=[ | ||
| FiredRule( | ||
| rule_id="ASI-01", | ||
| rule_name="Block PII in flight", | ||
| pack_name="agent-safety", | ||
| validator="pii_detection", | ||
| ) | ||
| ], | ||
| data={"prompt": "hi"}, | ||
| hook="before_model", | ||
| trace_id="0123456789abcdef0123456789abcdef", | ||
| src_timestamp="2026-06-22T10:00:00Z", | ||
| agent_name="my-agent", | ||
| runtime_id="runtime-1", | ||
| ) | ||
|
|
||
|
|
||
| class TestPolicyContext: | ||
| def test_defaults(self) -> None: | ||
| ctx = PolicyContext() | ||
| assert ctx.is_conversational is None | ||
|
|
||
| def test_ignores_unknown_fields(self) -> None: | ||
| ctx = PolicyContext.model_validate( | ||
| {"is_conversational": True, "future_selector": "x"} | ||
| ) | ||
| assert ctx.is_conversational is True | ||
|
|
||
|
|
||
| class TestPolicyResponse: | ||
| def test_defaults(self) -> None: | ||
| response = PolicyResponse() | ||
| assert response.mode is None | ||
| assert response.policies == "" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("wire_value", "expected"), | ||
| [ | ||
| ("audit", EnforcementMode.AUDIT), | ||
| ("enforce", EnforcementMode.ENFORCE), | ||
| ("disabled", EnforcementMode.DISABLED), | ||
| ], | ||
| ) | ||
| def test_parses_known_modes( | ||
| self, wire_value: str, expected: EnforcementMode | ||
| ) -> None: | ||
| response = PolicyResponse.model_validate({"mode": wire_value}) | ||
| assert response.mode is expected | ||
|
|
||
| def test_unknown_mode_falls_back_to_none(self) -> None: | ||
| # Forward-compat: a server-added mode the SDK doesn't know about | ||
| # must not break agent startup. Parses as None so the runtime | ||
| # falls back to its safe default rather than raising. | ||
| response = PolicyResponse.model_validate({"mode": "ludicrous"}) | ||
| assert response.mode is None | ||
|
|
||
|
|
||
| class TestGovernRequest: | ||
| def test_serializes_wire_aliases(self) -> None: | ||
| payload = _make_request().model_dump(by_alias=True, exclude_none=True) | ||
| assert payload["type"] == ["pii_detection"] | ||
| assert payload["traceId"] == "0123456789abcdef0123456789abcdef" | ||
| assert payload["agentName"] == "my-agent" | ||
| assert payload["runtimeId"] == "runtime-1" | ||
| # src_timestamp is intentionally snake_case on the wire. | ||
| assert payload["src_timestamp"] == "2026-06-22T10:00:00Z" | ||
| # Optional job-context fields left None → excluded. | ||
| for absent in ( | ||
| "folderKey", | ||
| "jobKey", | ||
| "processKey", | ||
| "referenceId", | ||
| "agentVersion", | ||
| ): | ||
| assert absent not in payload | ||
|
|
||
|
|
||
| class TestProtocolConformance: | ||
| """`runtime_checkable` Protocols should accept structurally-matching objects.""" | ||
|
|
||
| def test_fake_policy_provider_satisfies_protocol(self) -> None: | ||
| provider = _FakePolicyProvider() | ||
| assert isinstance(provider, GovernancePolicyProvider) | ||
|
|
||
| def test_fake_compensation_provider_satisfies_protocol(self) -> None: | ||
| provider = _FakeCompensationProvider() | ||
| assert isinstance(provider, GovernanceCompensationProvider) | ||
|
|
||
| def test_object_without_methods_rejected(self) -> None: | ||
| class _NotAProvider: | ||
| pass | ||
|
|
||
| assert not isinstance(_NotAProvider(), GovernancePolicyProvider) | ||
| assert not isinstance(_NotAProvider(), GovernanceCompensationProvider) | ||
|
|
||
|
|
||
| class TestEndToEndDispatch: | ||
| """Caller passes a provider directly to the consumer (no global registry).""" | ||
|
|
||
| def test_policy_round_trip(self) -> None: | ||
| provider = _FakePolicyProvider() | ||
| response = provider.get_policy(PolicyContext(is_conversational=True)) | ||
|
|
||
| assert response.mode is EnforcementMode.ENFORCE | ||
| assert provider.calls == [PolicyContext(is_conversational=True)] | ||
|
|
||
| def test_compensation_round_trip(self) -> None: | ||
| provider = _FakeCompensationProvider() | ||
| request = _make_request() | ||
| provider.compensate(request) | ||
|
|
||
| assert provider.calls == [request] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.