Skip to content

feat(governance): guardrail-fallback compensation#123

Open
aditik0303 wants to merge 5 commits into
feat/governance-auditfrom
feat/governance-guardrail-compensation
Open

feat(governance): guardrail-fallback compensation#123
aditik0303 wants to merge 5 commits into
feat/governance-auditfrom
feat/governance-guardrail-compensation

Conversation

@aditik0303

Copy link
Copy Markdown

Stacked PR 4/7 — part of splitting feat/governance-core into reviewable slices. Base: feat/governance-audit. One logical slice (branch is cumulative so CI is green). Merge in order #1#7 and delete each branch on merge so the next PR auto-retargets onto feat/agentic-governance. feat/governance-core kept untouched as backup.

@aditik0303 aditik0303 requested a review from a team as a code owner June 12, 2026 11:45
@aditik0303 aditik0303 force-pushed the feat/governance-audit branch from 42e1f29 to 7111d27 Compare June 16, 2026 09:28
@aditik0303 aditik0303 force-pushed the feat/governance-guardrail-compensation branch from 8812695 to 2da1f67 Compare June 16, 2026 09:30
@aditik0303 aditik0303 force-pushed the feat/governance-audit branch from 7111d27 to 2db5f2d Compare June 17, 2026 06:35
@aditik0303 aditik0303 force-pushed the feat/governance-guardrail-compensation branch from 2da1f67 to 5b119ac Compare June 17, 2026 06:37
@aditik0303 aditik0303 force-pushed the feat/governance-audit branch from 2db5f2d to bd19932 Compare June 17, 2026 08:35
@aditik0303 aditik0303 force-pushed the feat/governance-guardrail-compensation branch from 5b119ac to 2154aba Compare June 17, 2026 08:35
@aditik0303 aditik0303 force-pushed the feat/governance-audit branch from bd19932 to add4e90 Compare June 19, 2026 08:08
@aditik0303 aditik0303 force-pushed the feat/governance-guardrail-compensation branch from 2154aba to 1f7bdad Compare June 19, 2026 08:08
@viswa-uipath viswa-uipath force-pushed the feat/governance-audit branch from d500f99 to 7cdc791 Compare June 24, 2026 07:41
@viswa-uipath viswa-uipath force-pushed the feat/governance-guardrail-compensation branch from fb9756e to 0e9ad5b Compare June 24, 2026 08:13
@viswa-uipath viswa-uipath force-pushed the feat/governance-audit branch from 7cdc791 to 4da27ce Compare June 24, 2026 09:55
aditik0303 and others added 5 commits June 24, 2026 15:35
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…not uipath-platform

- guardrail_compensation.py: _resolve_trace_id reads the UIPATH_TRACE_ID env
  var via the runtime-local ENV_TRACE_ID constant instead of UiPathConfig;
  log messages no longer reference uipath-platform.
- test_guardrail_compensation: import reset helper from tests._helpers; the
  trace-id fallback test pins UIPATH_TRACE_ID via monkeypatch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ve_trace_id

Restores the conversational trace-id binding fix. Native governance audit
spans are exported under UIPATH_TRACE_ID (the platform rebinds spans to the
agent's run trace), so the /govern compensation records must bind to that
same id — not the live OTel span's id, which diverges in autonomous runs and
is absent on the conversational hook thread. Resolve UIPATH_TRACE_ID first,
then the live span, then the caller fallback.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The compensation path used to hand-roll the HTTP POST — URL composition,
auth, headers, JSON, env-backed job-context resolution. uipath-core now
exposes a GovernanceCompensationProvider protocol and uipath-platform
ships UiPathPlatformGovernanceProvider as the concrete implementation, so
the runtime no longer needs any of that wire-level code.

- submit_compensation gains a provider: GovernanceCompensationProvider
  first argument; the worker thread calls provider.compensate(request)
  with a GovernRequest built from the fired-rule metadata
- delete request_governance (urllib/JSON/headers/auth all gone — that's
  the platform service's job; folder_key/job_key/process_key/reference_id/
  agent_version are auto-filled by the provider from UiPathConfig)
- disabled_guardrails returns list[FiredRule] (uipath-core pydantic
  wire model) instead of a list of TypedDicts
- inline ENV_TRACE_ID + COMPENSATION_MAX_WORKERS — backend_client no
  longer exists on this branch and these were its only remaining users

Tests: drop the 14 HTTP/auth/URL/header/payload tests (now provider
concerns covered in uipath-platform); add provider-invocation tests
(GovernRequest assembly, validator dedup, error swallowing); guard the
evaluator-integration tests with importorskip so the file collects on
this branch — they need rewriting when the evaluator lands to match the
new provider-first signature.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…from wiring

Addresses radu's recurring PR #121 patterns applied to the guardrail
compensation slice. Resolves the post-PR-#121 ImportError in the test
file (it referenced the deleted ``uipath.runtime.governance.config`` /
``tests._helpers.reset_enforcement_mode``).

Architectural — match the AuditManager / PolicyLoader shape
- New GuardrailCompensator class. Each GovernanceRuntime instance gets
  one — owns its own ThreadPoolExecutor, BoundedSemaphore, and provider.
  uipath eval parallel runtimes no longer share workers, queue slots,
  or saturation state.
- Module globals _pool / _inflight / _INFLIGHT_CAP / @atexit.register
  decorator removed. Process cleanup via a weakref.WeakSet of live
  compensators + one process-level atexit hook (same pattern PR #122
  introduced for AuditManager): N runtimes → 1 atexit slot, no strong
  ref pinning disposed compensators.
- close() is an instance method, idempotent, logs at debug on failure.
- The free submit_compensation function is gone — callers use
  compensator.submit(...).

Boundary — env reads move to the wiring layer
- _resolve_trace_id signature changed to (supplied, fallback). It no
  longer reads UIPATH_TRACE_ID. The runtime layer is now env-free for
  this code path.
- GovernanceRuntime accepts a trace_id: str | None constructor arg and
  exposes it via the .trace_id property. The wiring layer (uipath CLI)
  reads UIPATH_TRACE_ID and passes the value in; the evaluator slice
  forwards it into GuardrailCompensator(provider, trace_id=...).
- GuardrailCompensator accepts trace_id at construction; it becomes
  the authoritative source. Per-submit trace_id is a per-call fallback.

Polish
- Replaced bare except Exception: pass in _resolve_trace_id with a
  logger.debug (bandit B110 cleared on this file).
- Removed ENV_TRACE_ID constant + the os import that backed it.

Tests
- Full rewrite of test_guardrail_compensation to drop deleted imports
  (config, reset_enforcement_mode), use GuardrailCompensator(provider),
  and mirror AuditManager's lifecycle test set (one atexit
  registration, weakref GC, idempotent close, cross-instance
  isolation, semaphore release on provider error).
- New test_resolve_trace_id_does_not_read_env pins the boundary rule:
  even with UIPATH_TRACE_ID set, the runtime layer ignores it.
- New test_compensator_trace_id_overrides_caller_supplied_value pins
  the construction-supplied value winning over per-submit.
- New test_governance_runtime_stashes_trace_id +
  test_governance_runtime_default_trace_id_is_none cover the new
  GovernanceRuntime kwarg + property.

238 passed, ruff/mypy clean; bandit clean on the touched files (one
pre-existing B101 in _yaml_to_index.py is unchanged and out of scope).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@viswa-uipath viswa-uipath force-pushed the feat/governance-guardrail-compensation branch from 0e9ad5b to 470533e Compare June 24, 2026 10:57
viswa-uipath added a commit that referenced this pull request Jun 24, 2026
…orts

Closes radu's recurring boundary objection for the evaluator slice and
makes the post-rebase stack actually import. The evaluator was the
last place where everything PR #121-#123 instance-scoped collapsed
back to process globals.

Architectural
- GovernanceEvaluator gains constructor injection:
  GovernanceEvaluator(policy_index, *, enforcement_mode=AUDIT,
                      audit_manager=None, compensator=None)
- Drop get_audit_manager() / get_enforcement_mode() / submit_compensation
  free-function lookups. The evaluator now consults zero process-globals
  on the hot path.
- mode property is read-only (drop the setter); no two-writer race
  between the loader and evaluator.
- audit_manager=None and compensator=None short-circuit cleanly so
  tests + minimal wirings work without injecting every dep.
- Drop unused is_enforce_mode() public method (dead code; no caller in
  src/ or tests/).

Post-rebase plumbing
- _dispatch_compensation uses self._compensator.submit(...) instead of
  the deleted free function; reads r.validator (Pydantic attribute)
  instead of the old r["validator"] TypedDict access.
- _emit_audit passes policy_id (PR #122 trace-contract field, was
  rule_id) and enforcement_mode=mode enum (PR #122 required arg).
- Import EnforcementMode from uipath.core.governance (governance.config
  deleted in PR #121); import AuditManager from _audit.base (audit/ is
  _audit/ post-PR-#122).

native/__init__.py
- Drop the four module-level loader-function re-exports
  (get_policy_index / load_policy_index / prefetch_policy_index /
  reset_policy_index) — all deleted in PR #121's PolicyLoader refactor.
- Export PolicyLoader instead.

Tests
- test_evaluator: full rewrite. Drop deleted-import paths
  (tests._helpers.reset_enforcement_mode, governance.config). Replace
  the global-manager fixture with a per-test AuditManager that uses
  register_default_sinks=False + a capturing sink. Every
  GovernanceEvaluator() call routes through a _build_evaluator helper
  with explicit mode + manager. New test_no_audit_manager_short_circuits
  replaces the previous test that mocked the global to raise.
- test_evaluator_operators: drop the autouse mode-isolating fixture
  (no globals to isolate); DISABLED-mode test passes
  enforcement_mode=EnforcementMode.DISABLED via constructor.
- test_guardrail_compensation: rebase-conflict resolution dropped the
  stale incoming-side imports (Action/LifecycleHook, backend_client,
  unguarded GovernanceEvaluator) since none of them are referenced in
  the rest of the file.

357 passed, 1 skipped (pre-existing wrapper skip). Ruff clean. Mypy
clean (11 source files). Bandit shows only the pre-existing B101 in
_yaml_to_index.py (out of scope).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants