Wrapper
Wrap any async function with a consensus gate using multiple reviewers and configurable strategies.
Overview
@consensus-tools/wrapper wraps any async function with a consensus gate. Multiple reviewers score the function's output, and a configurable strategy (unanimous, majority, or threshold) decides whether to allow, block, retry, or escalate.
Installation
Quick start
API reference
consensus()
Wraps a function with a consensus gate. Returns an async function with the same signature that produces a DecisionResult.
Options:
| Field | Type | Description |
|---|---|---|
name | string | Identifier for this consensus gate |
fn | (...args: A) => Promise<T> | The function to wrap |
reviewers | ReviewerFn<T>[] | Array of reviewer functions |
strategy | StrategyConfig | Strategy and threshold |
maxRetries | number | Max retry attempts before escalation |
hooks | LifecycleHooks<T> | Optional lifecycle hooks |
Writing reviewers
A reviewer receives the function output and context, and returns a score (0-1):
Setting block: true on any reviewer immediately blocks regardless of strategy.
Strategies
| Strategy | Passes when... |
|---|---|
"threshold" | Average score >= threshold (default 0.5) |
"majority" | More than half of reviewers score >= threshold |
"unanimous" | Every reviewer scores >= threshold |
Lifecycle hooks
aggregateScores()
Use the aggregation logic directly without the wrapper:
Exports reference
| Export | Description |
|---|---|
consensus(opts) | Wrap a function with a consensus gate; returns an async function |
aggregateScores(scores, output, attempt, config, maxRetries) | Score aggregation utility |
ConsensusOptions | Options for consensus() |
ReviewerFn<T> | Reviewer function type |
ReviewContext | Context passed to reviewers (name, args, attempt) |
ReviewResult | Reviewer return type (score, rationale, block) |
Strategy | "unanimous" | "majority" | "threshold" |
StrategyConfig | Strategy + threshold |
DecisionResult<T> | Final decision (action, output, scores, aggregateScore) |
LifecycleHooks<T> | Hook functions for beforeSubmit, afterResolve, onBlock, onEscalate |
Looking for a simpler entry point?
@consensus-tools/universal wraps this package with sensible defaults — built-in guard reviewers and fail-safe behavior out of the box. Use universal for quick setup; use wrapper directly when you need full control over reviewers and strategies.