> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firstwork.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Compliance Engine

> Build rule-based compliance workflows with AI-powered evaluation and real-time monitoring

## Overview

The **Compliance Engine** is a rule evaluation system that lets you define conditions, evaluate candidate and employee data against those conditions, and trigger actions based on the results. It's the decision-making brain behind automated hiring, onboarding, and workforce compliance.

## How Rules Work

A **Rule** is a condition tree that evaluates to `true` or `false`. Rules are composed of **Rule Nodes** — atomic conditions connected with AND/OR logic.

```mermaid theme={null}
graph TD
    R[Rule: Eligible to Work] --> AND[AND]
    AND --> N1["Age >= 18"]
    AND --> OR[OR]
    OR --> N2["Has US Citizenship"]
    OR --> N3["Has Valid Work Visa"]
```

### Rule Nodes

Each **Rule Node** contains:

| Field      | Description                                                                     |
| ---------- | ------------------------------------------------------------------------------- |
| Field Path | The data field to evaluate (using [Object Graph](/knowledge-base/object-graph)) |
| Operator   | The comparison operator                                                         |
| Value      | The expected value                                                              |
| Logic      | AND / OR connection to sibling nodes                                            |

### Operators

| Operator       | Description             | Example                           |
| -------------- | ----------------------- | --------------------------------- |
| equals         | Exact match             | `status == "ACTIVE"`              |
| not\_equals    | Not equal               | `status != "REJECTED"`            |
| greater\_than  | Numeric comparison      | `age > 18`                        |
| less\_than     | Numeric comparison      | `experience < 5`                  |
| contains       | String contains         | `email contains "@gmail"`         |
| not\_contains  | String does not contain | `name not_contains "test"`        |
| in             | Value in list           | `state in ["CA", "NY", "TX"]`     |
| not\_in        | Value not in list       | `country not_in ["restricted"]`   |
| is\_empty      | Field is empty          | `phone is_empty`                  |
| is\_not\_empty | Field has value         | `resume is_not_empty`             |
| regex          | Regular expression      | `ssn regex "^\d{3}-\d{2}-\d{4}$"` |

### Object Graph Integration

Rules reference data using the [Object Graph](/knowledge-base/object-graph) path system:

```
{{application.candidate.date_of_birth}}
{{form_submission.elements.work_authorization.value}}
{{contract.legal_entity.state}}
{{application.hiring_flow.name}}
```

This allows rules to evaluate any data point in the system without hardcoding field references.

## Rule Instances

A **Rule Instance** is a rule configured for a specific context. The same rule definition can be reused across multiple contexts with different parameters.

**Contexts where rules are used:**

* **Hiring Flow stages** — Gate stage transitions
* **Automations** — Conditional action execution
* **Form compliance** — Validate form submissions
* **Document requirements** — Verify document data

### Rule Instance Values

**Rule Instance Values** provide context-specific parameters:

```
Rule: "Field >= Threshold"
Instance 1: field = "age", threshold = 18 (for US hiring)
Instance 2: field = "age", threshold = 16 (for UK hiring)
```

## Rule Executions

Every rule evaluation is recorded as a **Rule Execution**:

| Field         | Description                   |
| ------------- | ----------------------------- |
| Rule Instance | Which rule was evaluated      |
| Result        | Pass or fail                  |
| Input Data    | The data that was evaluated   |
| Timestamp     | When the evaluation occurred  |
| Trigger       | What triggered the evaluation |

This provides a complete audit trail of all compliance decisions.

## AI-Powered Rules

Firstwork supports **AI-powered rule evaluation** where complex conditions can be assessed by LLM:

* Natural language conditions ("Is this document a valid government-issued ID?")
* Document content analysis
* Sentiment analysis on text responses
* Complex multi-factor assessments

AI rules use LangChain and LiteLLM to integrate with multiple AI providers (OpenAI, Anthropic, Google).

## Using Rules in Automations

Rules serve as the conditional logic in [Automations](/features/automations):

```mermaid theme={null}
graph LR
    T[Trigger: Form Submitted] --> R{Rule: Eligible?}
    R -->|Pass| A1[Action: Advance Stage]
    R -->|Fail| A2[Action: Send Rejection Email]
```

When an automation fires:

1. The trigger event occurs
2. The rule is evaluated against current data
3. If the rule passes, the associated actions execute
4. If the rule fails, alternative actions can execute (or nothing happens)

## Best Practices

Create small, focused rules that check one thing. Combine them using AND/OR logic for complex conditions. This makes rules easier to debug and reuse.
Name rules descriptively: "US Work Authorization Check" is better than "Rule 1". This helps when reviewing rule executions and debugging compliance issues.
Use the rule evaluation preview to test rules against sample data before applying them to active hiring flows.
Regularly review rule execution logs to identify:

* Rules that always pass or fail (may need adjustment)
* Unexpected evaluation results
* Performance bottlenecks in complex rule trees
