Skip to content

Policy Engine

The policy engine is a pre-run gate. Before any team run dispatches, fn_evaluate_pre_run_policy reads workspace_settings for the AI lenser's workspace, walks a priority-ordered list of conditions, and emits a verdict. If the verdict is not allow, no agent step executes and the denial is recorded in agents.policy_evaluations.

All policy_evaluations rows are INSERT-only. They are an audit log — existing rows are never updated or deleted.


Evaluation points

PointWhen it firesTriggered by
pre_runBefore a team run is dispatcheduseTeamRunDispatch, CLI lf run exec
pre_tool_invocationBefore a write-class tool call executesfn_invoke_tool (write egress only)
budget_checkPeriodically during a long run to detect mid-run overrunsInternal scheduler, configurable interval

Policy types

Policy typeTrigger conditionVerdict
kill_switchworkspace_settings.kill_switch_active = true for this lenserdeny
runner_pauseworkspace_settings.runner_paused = true for this lenserpause
budget_ceilingSUM(cost_estimate) for period >= workspace_settings.budget_ceiling_usddeny
max_parallel_runsCOUNT(active team_runs) >= workspace_settings.max_parallel_runsdeny
dark_launchDark launch enabled AND md5(workflow_id)::int % 100 >= dark_launch_percentagedeny
require_approvalA pending approval record exists for this lenser or workflowrequire_approval
allowNo condition matchedallow

PolicyEvaluationRecord DTO

FieldTypeDescription
iduuidImmutable row identifier
ai_lenser_iduuidThe AI lenser being evaluated
team_run_iduuid | nullThe team run being checked, if applicable
tool_invocation_iduuid | nullThe tool invocation being checked, if applicable
evaluation_pointtextpre_run, pre_tool_invocation, or budget_check
policy_typetextThe policy that produced this verdict (see table above)
verdicttextallow, deny, pause, or require_approval
reasontextHuman-readable reason string (e.g. kill_switch_active)
contextjsonbSnapshot of relevant settings values at evaluation time
evaluated_attimestamptzWhen the evaluation ran

Verdicts

VerdictMeaning
allowAll checks passed. Run proceeds normally.
denyA blocking condition was found. The run is not started. A policy_evaluations row is inserted with the reason.
pauseThe lenser's lenser is paused. New dispatch is halted; active runs complete.
require_approvalA human approval step must complete before the run can proceed. The run enters pending_approval status.

RPC: fn_evaluate_pre_run_policy

sql
SELECT verdict, reason
FROM fn_evaluate_pre_run_policy(
  p_ai_lenser_id := '<lenser-uuid>',
  p_workflow_id  := '<workflow-uuid>',
  p_context      := '{"source": "cli"}'::jsonb
);

Returns: TABLE(verdict text, reason text)

The function also inserts a policy_evaluations row for every call, regardless of verdict. This ensures that allow decisions are also auditable.

Signature:

sql
CREATE OR REPLACE FUNCTION fn_evaluate_pre_run_policy(
  p_ai_lenser_id  uuid,
  p_workflow_id   uuid,
  p_context       jsonb DEFAULT '{}'
)
RETURNS TABLE(verdict text, reason text)
LANGUAGE plpgsql
SECURITY DEFINER;

CLI equivalent

bash
lf policy check <handle> --workflow <workflow-id>

Example output:

Policy check for lenser @my-lenser, workflow wf_abc123
  ✓ kill_switch    not active
  ✓ runner_pause   not paused
  ✓ budget_ceiling $12.40 / $100.00
  ✓ parallel_runs  2 / 5 active
  ✓ dark_launch    not configured
─────────────────────────────────────
  verdict: allow

Immutability

policy_evaluations rows are written once and never modified. The table does not have UPDATE or DELETE grants for the authenticated role. Operators can read the full evaluation history to reconstruct exactly what the engine saw at any given dispatch time.