Skip to main content
Gates are the core primitive in Pecta. Each gate is a single pass/fail check that runs against an agent’s output before it reaches the user. You compose any number of gates when you call createEngine, and the engine runs all of them in parallel under a shared timeout budget. Fail-fast (the default) aborts remaining gates the moment one fails, keeping evaluation latency low.

All built-in gates

Composing gates

Pass a gates array to createEngine. The engine validates that every gate has a unique name and a run function, then returns an Engine object whose evaluate method you call for each agent output.
The return value is an EvaluationResult:

Timeout budget

The timeout option (default 50 ms) is a wall-clock budget shared across all gates. When the budget expires, the engine fires an AbortController signal. Gates that check signal.aborted before doing expensive work return immediately; any gate that is still running when the timer fires has its result recorded as passed: false with reason: "pecta:aborted: pecta:timeout".
Setting timeout too low can cause gates to return aborted results even when the output is valid. Start with the default 50 ms and reduce only after profiling your gate set.

Fail-fast behavior

When failFast is true (the default), the engine aborts the shared AbortController the instant a gate returns passed: false. Gates that have not yet returned will see signal.aborted === true. The engine still waits for every Promise to settle, but aborted gates complete almost immediately. Total evaluation latency is therefore dominated by the first failing gate, not the full gate set. Set failFast: false if you want verdicts from every gate regardless of earlier failures — useful for auditing or debugging.
A gate that returns skipped: true is treated as passing. The tmaxGuard RTB gate uses this to opt out of evaluation when the OpenRTB deadline is already exhausted.

Custom gates

You can write your own gate and pass it alongside the built-in ones. See the custom gates page for the full interface and a worked example.