Policy Engine

techrevati.runtime.policy_engine

Policy Engine — Declarative rules for orchestration decisions.

Composable conditions (And/Or/QualityAt/...) evaluated against a PhaseContext, producing a flat list of PolicyActionData for the caller to dispatch. Rules are sorted by priority (lower first); all matching rules fire.

PolicyCondition

Bases: ABC

Base class for policy conditions. Override matches().

matches abstractmethod

matches(ctx)

Return whether the condition matches the current phase context.

And

And(conditions)

Bases: PolicyCondition

All conditions must match.

Or

Or(conditions)

Bases: PolicyCondition

Any condition must match.

QualityAt

QualityAt(level)

Bases: PolicyCondition

Matches if observed quality level >= required.

AgentFailed

AgentFailed(role=None)

Bases: PolicyCondition

Matches if any agent (or a specific role) failed.

GateBelow

GateBelow(threshold)

Bases: PolicyCondition

Matches if gate score is below threshold.

RetryExhausted

RetryExhausted(scenario=None)

Bases: PolicyCondition

Matches if recovery retries are exhausted for a scenario.

TimedOut

TimedOut(seconds)

Bases: PolicyCondition

Matches if elapsed time exceeds duration.

CostExceeded

CostExceeded(budget_usd)

Bases: PolicyCondition

Matches if total cost exceeds budget.

PolicyAction

Bases: str, Enum

Actions a rule can recommend. The caller is responsible for dispatch.

PolicyActionData dataclass

PolicyActionData(action, params=None)

Action with optional parameters.

PolicyRule dataclass

PolicyRule(name, condition, actions, priority=50)

A named rule with condition, action(s), and priority.

PhaseContext dataclass

PhaseContext(
    phase="",
    quality_level=None,
    gate_score=0.0,
    gate_threshold=0.0,
    completed_roles=set(),
    failed_roles=set(),
    all_roles=set(),
    elapsed_seconds=0.0,
    retry_exhausted_scenarios=set(),
    phase_completed=False,
    total_cost_usd=0.0,
)

Input to policy evaluation. Captures current phase state.

PolicyEngine

PolicyEngine(rules)

Evaluates rules against a PhaseContext.

evaluate

evaluate(ctx)

Evaluate all rules. Returns the flat list of matching actions.

evaluate_async async

evaluate_async(ctx)

Async sibling of evaluate.

Conditions whose matches is a coroutine function are awaited; sync conditions are called in place. Falls back to the sync path if every rule is sync, so existing engines run unchanged.