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

# Policies

> JudgePolicy maps verdicts to actions — you own what happens on allow, review, block, and failure. Defaults are fail-closed.

**`JudgePolicy`**: verdict → `JudgeAction`. The judge decides *what the risk is*; your policy decides *what to do about it*. The SDK ships safe defaults — block becomes a tool error, review proceeds with a `tenet_review` marker until you wire a [HITL pause](https://docs.tenetlabs.com/guides/hitl.md), unavailable raises — and every handler is overridable per tool and per phase.

## The shape

```python theme={null}
from tenet_client import JudgePolicy, JudgeAction

policy = JudgePolicy(
    on_allow=...,        # (ctx, verdict) -> JudgeAction   default: allow
    on_caveat=...,       # (ctx, verdict) -> JudgeAction   default: allow + tenet_caveat marker — NON-BLOCKING advisory
    on_review=...,       # (ctx, verdict) -> JudgeAction   default: allow + tenet_review marker — does NOT pause
    on_block=...,        # (ctx, verdict) -> JudgeAction   default: tool_error(safe_message or a fixed message, plus reason)
    on_unresolved=...,   # (ctx, verdict) -> JudgeAction   default: retry_nudge_from_verdict
    on_unavailable=...,  # (ctx, error)   -> JudgeAction   default: raise_error (fail-closed)
    on_timeout=...,      # (ctx, error)   -> JudgeAction   default: raise_error (fail-closed)
)
```

Each handler receives a `JudgeContext` (`phase`, `tool_name`, `tool_input`, `tool_output`, `session_id`, `metadata`) and returns a `JudgeAction`.

## Actions

| Action | Effect | Reach for it when |
| - | - | - |
| `JudgeAction.allow()` | Proceed unchanged | — |
| `JudgeAction.block(message=...)` | Stop the call; surface `message` | Hard stops where the agent shouldn't retry |
| `JudgeAction.tool_error(message)` | Tool returns an error the model can read | Default for blocks — the agent self-corrects |
| `JudgeAction.retry_nudge(guidance)` | Tool error + explicit guidance on how to reformulate | You want the agent to fix the call, not abandon it |
| `JudgeAction.human_review()` | Marks the action for HITL — **passthrough, does not pause** | Tagging only; for a real pause, your callback must suspend the run itself — see below |
| `JudgeAction.override_input(payload)` | Replace tool args, then run | Redaction: strip the violating field, keep the call |
| `JudgeAction.override_output(payload)` | Replace tool output | Sanitize retrieved content before it hits model context |
| `JudgeAction.raise_error(err)` | Propagate an exception | Fail-closed paths |

`retry_nudge` is the action that makes guardrails feel like a collaborator instead of a wall: the model gets told *what* to change ("remove the age constraint and re-run the search"), retries, and usually succeeds. `retry_nudge_from_verdict(verdict)` builds the guidance from the judge's own findings. The middleware escalates to a hard `tool_error` after 3 consecutive nudges (configurable) so a stubborn model can't loop.

## A production policy

```python theme={null}
# Python — a recruiting agent: nudge on block, pause on review,
# fail open only for read-only tools.
READ_ONLY = {"get_job_description", "lookup_candidate_status"}

policy = JudgePolicy(
    on_block=lambda ctx, v: JudgeAction.retry_nudge_from_verdict(v),
    on_review=on_review_pause,   # a real HITL pause — see "Human in the loop" below
    on_unavailable=lambda ctx, err: (
        JudgeAction.allow(metadata={"judge_skipped": True})  # log it — don't lose the signal
        if ctx.tool_name in READ_ONLY
        else JudgeAction.raise_error(err)
    ),
    # send_email must never fail open, never nudge — block outright
    per_tool={
        "send_email": JudgePolicy(
            on_block=lambda ctx, v: JudgeAction.block(message=v.safe_message or v.reason),
            on_unavailable=lambda ctx, err: JudgeAction.raise_error(err),
        ),
    },
)
```

`per_tool` and `per_phase` are full nested policies — the most specific handler wins.

## Fail open vs. fail closed

The tradeoff: fail-closed protects compliance at the cost of availability; fail-open inverts it. Don't pick one globally — pick per tool:

* **Side effects (send, write, post): fail closed.** A missed email is recoverable; a discriminatory one is not.
* **Read-only lookups: fail open is defensible** — but tag the action's `metadata` so skipped evaluations are visible in your logs, not silent.
* **Never fail open on `agent_output`** if the response reaches an end user.

## Human in the loop

A `review` verdict is a recommendation, not a pause. To actually stop and ask a human, your `on_review` callback suspends the run itself — in a LangGraph harness, with `interrupt()`:

```python theme={null}
# Python — LangGraph HITL. interrupt() raises on the first pass and
# returns on resume, so reaching the return means the human approved.
from langgraph.types import interrupt

def on_review_pause(ctx, verdict):
    interrupt({
        "reason": verdict.reason,
        "risk_labels": [f.get("rule_id") for f in verdict.findings or []],
        "tool_name": ctx.tool_name,
    })
    return JudgeAction.allow()   # only reached after the human resumes
```

The middleware caches the verdict *before* dispatching your callback, so the `interrupt()` exception doesn't lose it — on resume, the same verdict is reused instead of re-judging. Pair with `turn_approval=True` so one approval covers repeat hits in the same turn.

> [!WARNING]
>
> Two requirements for `interrupt()`-based review: your agent needs a **checkpointer** (resume requires persisted graph state — an in-memory checkpointer loses paused turns on restart, so don't ship that to multi-worker production), and resume requests must carry the **same `thread_id`** as the paused turn.

The full wiring — rendering the review over SSE, reading the judge's streamed context inside the callback, resume semantics — is in the [HITL guide](https://docs.tenetlabs.com/guides/hitl.md).

## `on_caveat`

Reserved — the current judge doesn't return a top-level `include_with_caveats` verdict or the `caveat` field. The default handler allows, and a caveat is **never** dispatched through `on_review`.

## `on_unresolved`

With `resolution_detection` on (the middleware default), the judge classifies whether a tool's output actually resolved the request. `on_unresolved` fires when it didn't — the hook for "the search returned nothing because the only compliant interpretation was empty," where you may want to inform the user rather than let the agent spin.

## Links

* [LangChain middleware — wiring a policy in](https://docs.tenetlabs.com/guides/langchain.md)
* [Framework-free — `evaluate_phase` with a policy](https://docs.tenetlabs.com/guides/framework-free)
* [How the Judge decides — verdict vocabulary](https://docs.tenetlabs.com/concepts/decisions.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.