SSyncropel Docs
Inference

Fold Functions

The five fold functions that combine answers from several responders into one: consensus, best_of, waterfall_first, ensemble_weighted, and expression. Each gives the same result for the same answers.

Overview

The fold step combines the answers from your responders into one answer, and lists which answers contributed. The same answers always give the same result.

Because of that, a query can be replayed and give the same answer. Retries and escalation belong to orchestration, not fold.

There are five fold functions. This guide covers each one, when to use it, and an example.

The five functions

FunctionOne-linerTypical use
consensusMost agreed-upon answer wins. Trust-weighted vote.Multi-LLM summaries, classification, structured extraction
best_ofHighest-trust responder wins.Named-expert routing, single-winner flows
waterfall_firstFirst acceptable response wins.Cost-minimising cascades (cheap → expensive)
ensemble_weightedWeighted aggregation with a custom signal.Confidence-weighted, recency-weighted, custom-metric fusion
expressionYour own CEL logic over the list of responses.Domain-specific aggregation (median, regex, IQR filter…)

Order of responses

Before combining, every fold function sorts the responses by their clock and then by id, lowest first. The result never depends on the order the answers arrived in. You don't need to do anything for this.

If two responses tie, the tie_break rule decides.

Fold output

Every fold returns the same envelope:

{
  "answer": <the folded value>,
  "chosen_response_id": "<record id or null>",
  "provenance": ["<id1>", "<id2>", "..."],
  "tally": { "<canonical-json-of-answer>": <weight>, "...": <weight> }
}

chosen_response_id is the ID of the winning response. It is used to carry over the winner's modifiers (body.fulfills, body.cancels, body.awaits). When there is no single winner (the expression function), it is null.

provenance lists every response that contributed, so you can see who weighed in on this answer.

tally is present for consensus and ensemble_weighted. It shows how close the second-place answer was.

Trust floor

Combining uses the same minimum trust as ranking: 0.05 by default. A responder's trust never counts for less than that. Without it, a new responder with no history would count for nothing and could never build any.

If every contributor is below the floor, the answer carries body.fold.cold_start_warning: true. You can route on that in later rules.

The floor is set with a syncropel.config.fold.v1 record on th_engine_config:

spl config add-fold-rule \
  --id default \
  --priority 0 \
  --status active \
  --expression 'true'

(Fold rules are covered in the task-management guide. The floor is saved alongside them.)

Min quorum

Syncropel won't combine answers unless at least min_quorum responses came back without errors. The default is 1. If there aren't enough, you get an infer.error.v1 record with code: "quorum_not_met" instead of an answer.

Raise quorum when you want a minimum of 2 or 3 answers before you believe anything:

"fold": { "function": "consensus", "min_quorum": 2 }

Modifier preservation

Some responses carry modifiers in their body: fulfills, cancels, awaits. Fold doesn't change them. It picks a winner, and the winner's modifiers are attached to the saved answer.

The obligation_resolution field in side_effects decides what happens when responses carry conflicting modifiers (for example, one fulfills and one cancels):

  • last_writer_wins (default): the most-recent response's modifiers apply.
  • fulfills_wins: any fulfills preempts any cancels.
  • validation_error: a conflict stops the query with obligation_conflict.

Function: consensus

Trust-weighted vote. Responses with identical body.answer values are grouped, the weights in each group are added up, and the group with the highest total wins. Ties are broken by the tie_break setting.

Weight expression (default): trust * recency * pattern_confidence. All three come from the responder's details when the query is sent. You can override this for a query:

"fold": { "function": "consensus", "weight_expression": "response.trust" }

The floor applies after the full expression evaluates.

When to use: classification, structured extraction, tagging. Anything where responses are naturally comparable as JSON trees and you want the system's collective best-guess.

Example: three LLMs agreeing on a sentiment label:

{
  "fold": { "function": "consensus", "min_quorum": 2 }
}

Response bodies:

  • A: { "sentiment": "positive", "confidence": 0.9 } (trust 0.8)
  • B: { "sentiment": "positive", "confidence": 0.8 } (trust 0.7)
  • C: { "sentiment": "neutral", "confidence": 0.6 } (trust 0.6)

A and B give the same answer. Weight sum for positive = 0.8 + 0.7 = 1.5. Weight sum for neutral = 0.6. Winner: positive. chosen_response_id is A (higher trust within the group).

Function: best_of

The highest-trust responder wins, and its whole answer is kept. Ties are broken by clock and id (lowest first), then by the tie_break rule.

When to use: one expert is right about this, but you don't know which one when you send the query. Code review, medical suggestion, legal reasoning: domains where you'd rather hear the most-trusted voice than a mediocre consensus.

Example: you have two code reviewers of known-different trust:

{
  "fold": { "function": "best_of", "tie_break": "highest_trust" }
}

If Alice has trust 0.95 and Bob has 0.82, Alice's response wins. The full body, including her confidence and rationale, survives intact into the KNOW.

Function: waterfall_first

The first response, in the order described above, that passes the acceptance test wins. By default a response is accepted if its answer is not null. You can set your own test with a CEL accept_expression.

When to use: you've deliberately ordered responders cheap-to-expensive (or fast-to-slow). You'd rather take the first one that's good enough than wait for the best one.

This is not the waterfall orchestration pattern, which asks responders one after another. waterfall_first takes responses that have already arrived and picks the first one that passes.

Example: pattern first, then LLM:

Responders: one pattern responder at clock 100, one llm responder at clock 101. If the pattern answer passes accept_expression: "response.body.confidence >= 0.8", fold picks the pattern. Otherwise it moves to the LLM.

{
  "fold": {
    "function": "waterfall_first",
    "expression": "response.body.confidence >= 0.8"
  }
}

(When you want true sequential dispatch with early termination, use the waterfall orchestration pattern instead. See the orchestration guide.)

Function: ensemble_weighted

Like consensus, but the weight is a CEL expression you write. The highest-weight group of matching answers wins, and the answer returned is the one from the highest-trust responder in that group.

Default weight expression: response.trust. Common overrides:

GoalExpression
Confidence-weightedresponse.trust * response.body.confidence
Recency-weightedresponse.trust * (1.0 - (now() - response.clock) / 86400)
Kind-biased (favour patterns)response.trust * (response.kind == "pattern" ? 1.5 : 1.0)

Negative weights are treated as 0.

When to use: you trust responders differently than their raw trust score suggests, and you can compute the difference from each response's own details.

Function: expression

Write your own combining logic in CEL. fold.expression can use responses (the sorted list) and tally (always empty for this function), and whatever it returns becomes the answer. chosen_response_id is always null.

When to use: none of the first four fit. Median over numeric answers, inter-quartile-range filtering, regex extraction, custom multi-body merge.

Example: median numeric answer:

{
  "fold": {
    "function": "expression",
    "expression": "responses.map(r, r.body.value).sort()[(responses.size() - 1) / 2]"
  }
}

Example: concatenate all non-empty string answers:

{
  "fold": {
    "function": "expression",
    "expression": "responses.filter(r, r.body.text != '').map(r, r.body.text).join('\\n---\\n')"
  }
}

Because expression doesn't produce a chosen_response_id, modifiers can't be kept. If your responders carry fulfills/cancels/awaits and you need to keep them, prefer best_of or consensus.

Choosing a function

You wantReach for
Most-agreed answer across peersconsensus
Most-trusted voicebest_of
First acceptable answer (cost cascade)waterfall_first
Custom weight on trust (confidence, recency)ensemble_weighted
Custom aggregation logic (median, regex, merge)expression

When in doubt, start with consensus. It is the most forgiving, and the tally shows how close second place was.

See also

On this page