@gate decorator is the primary user-facing API for protecting function calls with Attesta approval. It intercepts every invocation, scores the risk, presents the appropriate challenge, and either allows or blocks execution.
Calling Styles
The decorator supports three equivalent calling styles:Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
risk | RiskLevel | str | None | None | Explicit risk level override. Bypasses the risk scorer entirely. Accepts enum values or strings like "high", "critical". |
risk_hints | dict[str, Any] | None | None | Hints forwarded to the risk scorer. Common keys: production, destructive, pii. |
risk_scorer | RiskScorer | None | None | Override the default risk scorer for this gate. |
renderer | Renderer | None | None | Override the default renderer for this gate. |
audit_logger | AuditLogger | None | None | Override the default audit logger for this gate. |
challenge_map | dict[RiskLevel, ChallengeType] | None | None | Override the default risk-level-to-challenge mapping. |
min_review_seconds | float | 0.0 | Minimum wall-clock time the review must take. Prevents rubber-stamping. |
agent_id | str | None | None | Identifier for the AI agent making the call. Used by the trust engine. |
session_id | str | None | None | Session identifier for grouping related actions in audit logs. |
environment | str | "development" | Environment tag. Production environments receive higher base risk scores. |
metadata | dict[str, Any] | None | None | Arbitrary metadata attached to every ActionContext created by this gate. |
trust_engine | Any | None | None | Trust engine for adaptive risk adjustment. Requires agent_id to be set. |
sync_timeout | float | 300.0 | Maximum seconds to wait when bridging async evaluation from a synchronous context (e.g., Jupyter). Set to 0 for no timeout. |
allow_hint_override | bool | False | Whether to honor ctx.hints["risk_override"] at runtime. Keep False unless hints are trusted server-side input. |
mode | str | "enforce" | Gate mode: enforce, shadow, or audit_only. |
fail_mode | "deny" | "allow" | "escalate" | "deny" | Timeout policy for non-auto-approved actions: deny -> TIMED_OUT, allow -> APPROVED, escalate -> ESCALATED. |
approval_timeout_seconds | float | 600.0 | Maximum time to wait for a challenge response before fail_mode is applied. |
Sync and Async Behavior
The@gate decorator automatically detects whether the wrapped function is synchronous or asynchronous and handles each case correctly.
When a sync function decorated with
@gate is called inside an already-running event loop (e.g., Jupyter notebooks), Attesta evaluates in a dedicated worker thread and waits up to sync_timeout seconds. Adjust this value if your approval flow involves long review times.Denial Behavior
When the operator denies the action, fails a challenge, or the review times out, the decorator raises anAttestaDenied exception. The protected function is never executed.
AttestaDenied are:
| Verdict | Meaning |
|---|---|
DENIED | The operator explicitly rejected the action |
TIMED_OUT | The review period expired without a response |
ESCALATED | The action was escalated but not yet resolved |
Introspection
Every gated function has a__gate__ attribute attached to it, which holds a reference to the internal CoreAttesta orchestrator instance. This is useful for testing and debugging.
Risk Hints
Risk hints are key-value pairs that influence the risk scorer without hardcoding a risk level. They are more flexible thanrisk= because the scorer combines them with other factors.
With the Attesta Instance
When using anAttesta instance (recommended for production), the instance-level gate() method inherits defaults from the instance’s policy, risk scorer, renderer, and audit logger. Per-gate overrides take precedence.
Type Signatures
The decorator preserves the original function’s type signature usingfunctools.wraps (Python) or generics (TypeScript). Type checkers see the correct parameter and return types through the wrapper.
Python