5D Library › 检索增强生成几何、倒数秩融合与多智能体误差级联 · English

第四章 多智能体流水线:误差级联与验证器门控的吸收马尔可夫链

92% 的十次方

把一个复杂任务拆给一串智能体:一个检索,一个规划,一个写代码,一个调用工具……每一步单独看都相当可靠。问题在于串联:没有外部检查时,任何一步的错误都会一路传到终点。本章先算出串联的代价,再看两种补救——多个智能体投票,以及在每一步之后放一个验证器——各自能挽回多少,又依赖什么条件。

串联与投票

推导

串联的代价。 LL 步、每步正确概率 pp、没有检查时,端到端成功率为 pLp^L。每步由三个智能体做、多数表决时,若它们的错误独立,单步成功率升为 q=p3+3p2(1−p)q = p^3 + 3p^2(1-p)。若有比例为 cc 的「难例」让三者同时出错,投票对这些情形无能为力。

推导

# Chapter 4. p08: Error cascades in serial and voting agent pipelines.
# A. Serial chain of L steps, each correct with probability p, no checks:
#    end-to-end success p^L (any error propagates to the end).
# B. Each step done by three agents and decided by majority vote. With
#    independent errors a step succeeds with q = p^3 + 3 p^2 (1 - p).
#    Correlated errors: with probability c a step is a shared "hard case"
#    on which all three fail together; otherwise each agent is right with
#    p / (1 - c), so that one agent's overall accuracy is still p.
#    Simulated: 20,000 ten-step chains for each c.
import random

def maj3(p):
    return p ** 3 + 3 * p * p * (1 - p)

if __name__ == "__main__":
    print("A. serial chain, p^L")
    print("     p      L = 3    L = 5    L = 10   L = 20")
    for p in (0.99, 0.95, 0.92, 0.85):
        print("   {:.2f}   ".format(p) + "".join(
            "  {:.4f} ".format(p ** l) for l in (3, 5, 10, 20)))
    print("   0.92^10 = {:.4f}".format(0.92 ** 10))
    print("\nB. majority of three agents per step, p = 0.92, L = 10")
    rng = random.Random(61008)
    print("   shared hard cases c   step accuracy   chain success")
    for c in (0.0, 0.02, 0.04, 0.08):
        p_easy = min(0.92 / (1 - c), 1.0)
        ok_chains = 0
        ok_steps = 0
        for _ in range(200000 // 10):
            ok = True
            for _ in range(10):
                if rng.random() < c:
                    step = False
                else:
                    votes = sum(rng.random() < p_easy for _ in range(3))
                    step = votes >= 2
                ok_steps += step
                ok = ok and step
            ok_chains += ok
        print("        {:4.2f}               {:.4f}         {:.4f}".format(
            c, ok_steps / 200000, ok_chains / 20000))
    print("   independent formula: step {:.4f}, chain {:.4f}".format(
        maj3(0.92), maj3(0.92) ** 10))
    print("\nReading: 92 % per step leaves 43.4 % after ten steps. Voting")
    print("by three independent agents lifts each step to about 98 % and")
    print("the chain to about 83 %, but only while their errors are")
    print("independent: once some cases defeat all three together, the")
    print("vote cannot recover them and the chain falls back towards the")
    print("serial figure.")
# A. serial chain, p^L
#      p      L = 3    L = 5    L = 10   L = 20
#    0.99     0.9703   0.9510   0.9044   0.8179
#    0.95     0.8574   0.7738   0.5987   0.3585
#    0.92     0.7787   0.6591   0.4344   0.1887
#    0.85     0.6141   0.4437   0.1969   0.0388
#    0.92^10 = 0.4344
#
# B. majority of three agents per step, p = 0.92, L = 10
#    shared hard cases c   step accuracy   chain success
#         0.00               0.9819         0.8337
#         0.02               0.9692         0.7337
#         0.04               0.9546         0.6252
#         0.08               0.9201         0.4355
#    independent formula: step 0.9818, chain 0.8324
#
# Reading: 92 % per step leaves 43.4 % after ten steps. Voting
# by three independent agents lifts each step to about 98 % and
# the chain to about 83 %, but only while their errors are
# independent: once some cases defeat all three together, the
# vote cannot recover them and the chain falls back towards the
# serial figure.

p=0.92p = 0.92、L=10L = 10 时,0.9210=0.43440.92^{10} = 0.4344:每一步 92% 可靠,十步之后不到一半能成功。步数加到 20,只剩 0.1887。即使每步 99%,二十步后也只有 0.8179。

三个智能体投票时,若错误独立,单步成功率升到 0.9818,十步链的成功率按公式升到 0.8324(模拟 0.8337)。但这依赖独立:只要有 2% 的步骤是三者共同的难例,链成功率降到 0.7337;8% 时降到 0.4355,几乎回到不投票的 0.4344。用同一个模型的三个副本投票,它们的错误往往高度相关,投票的收益会远小于独立假设给出的数字 (Ladha 1992)。

验证器门控:吸收马尔可夫链

推导

验证器在环。 每次尝试之后,验证器以灵敏度 ss 抓住错误,以误报率 ff 错拒正确结果;被拒的尝试重做,每步最多 nn 次,用尽则整个任务升级给人处理。被放过的错误是「静默失败」:任务继续,但结果已错。每次尝试: a=p(1−f),b=(1−p)(1−s),r=pf+(1−p)s,a = p(1-f),\quad b = (1-p)(1-s),\quad r = pf + (1-p)s , 分别为正确通过、错误通过、重做。记 G=(1−rn)/(1−r)G = (1-r^n)/(1-r),则成功率为 (aG)L(aG)^L,升级率为 1−(1−rn)L1-(1-r^n)^L,期望尝试次数为 ∑i<L(1−rn)iG\sum_{i<L}(1-r^n)^i G。同样的数也可以由吸收链的基本矩阵 N=(I−Q)−1N = (I-Q)^{-1} 得到 (Kemeny and Snell 1960)。

推导

# Chapter 4. p09: A verifier in the loop as an absorbing Markov chain.
# L = 10 steps, each attempt right with p = 0.92. After every attempt a
# verifier passes or rejects it: it catches a wrong attempt with
# sensitivity s and wrongly rejects a right one with false-alarm rate f.
# A rejected attempt is retried, up to n attempts per step; if all n are
# rejected the run stops and is escalated to a person. A wrong attempt
# that passes is a silent failure: the run goes on, but its result is
# wrong. Per attempt:
#     accept right a = p (1 - f), accept wrong b = (1 - p)(1 - s),
#     retry r = p f + (1 - p) s,   G = (1 - r^n) / (1 - r).
# Closed form: success (a G)^L, escalation 1 - (1 - r^n)^L, expected
# attempts sum_i (1 - r^n)^i G. The same numbers are recomputed from the
# fundamental matrix N = (I - Q)^(-1) of the absorbing chain whose
# transient states are (step, attempt, already wrong?).
L, P = 10, 0.92

def closed(s, f, n):
    a, r = P * (1 - f), P * f + (1 - P) * s
    g = (1 - r ** n) / (1 - r) if r < 1 else n
    esc = r ** n
    return ((a * g) ** L, 1 - (1 - esc) ** L,
            sum((1 - esc) ** i * g for i in range(L)))

def fundamental(s, f, n):
    a, b = P * (1 - f), (1 - P) * (1 - s)
    r = P * f + (1 - P) * s
    states = [(i, j, t) for i in range(L) for j in range(n) for t in (0, 1)]
    ix = {st: k for k, st in enumerate(states)}
    m = len(states)
    q = [[0.0] * m for _ in range(m)]
    win = [0.0] * m
    for (i, j, t), k in ix.items():
        if j + 1 < n:
            q[k][ix[(i, j + 1, t)]] += r
        if i + 1 < L:
            q[k][ix[(i + 1, 0, t)]] += a
            q[k][ix[(i + 1, 0, 1)]] += b
        elif t == 0:
            win[k] += a
    def solve(rhs):
        mat = [[(1.0 if x == y else 0.0) - q[x][y] for y in range(m)]
               + [rhs[x]] for x in range(m)]
        for c in range(m):
            piv = max(range(c, m), key=lambda z: abs(mat[z][c]))
            mat[c], mat[piv] = mat[piv], mat[c]
            for z in range(m):
                if z != c and mat[z][c]:
                    fct = mat[z][c] / mat[c][c]
                    mat[z] = [u - fct * v for u, v in zip(mat[z], mat[c])]
        return [mat[z][m] / mat[z][z] for z in range(m)]
    start = ix[(0, 0, 0)]
    return solve(win)[start], solve([1.0] * m)[start]

if __name__ == "__main__":
    print("   verifier          s     f     n   success   escalated"
          "  attempts")
    cases = (("none", 0.0, 0.0, 1), ("code check", 1.0, 0.0, 3),
             ("code check", 1.0, 0.0, 10), ("good judge", 0.8, 0.05, 3),
             ("weak judge", 0.5, 0.10, 3), ("strict judge", 0.8, 0.30, 3))
    worst = 0.0
    for name, s, f, n in cases:
        succ, esc, tot = closed(s, f, n)
        fs, ft = fundamental(s, f, n)
        worst = max(worst, abs(fs - succ), abs(ft - tot))
        print("   {:<15} {:4.2f}  {:4.2f}  {:3d}   {:.4f}    {:.4f}"
              "    {:6.2f}".format(name, s, f, n, succ, esc, tot))
    print("   largest gap, closed form vs fundamental matrix: {:.1e}"
          .format(worst))
    print("   exact checker, n = 3: escalation per step 0.08^3 = {:.2e}"
          .format(0.08 ** 3))
    print("\nReading: an exact, independent checker turns 43 % into")
    print("better than 99 % for under 11 attempts over ten steps: errors are")
    print("caught and retried instead of propagated, and the residual")
    print("risk falls geometrically with each allowed retry. A judge that")
    print("misses a fifth of errors keeps silent failures in the chain;")
    print("one that rejects too much spends attempts and escalates.")
#    verifier          s     f     n   success   escalated  attempts
#    none            0.00  0.00    1   0.4344    0.0000     10.00
#    code check      1.00  0.00    3   0.9949    0.0051     10.84
#    code check      1.00  0.00   10   1.0000    0.0000     10.87
#    good judge      0.80  0.05    3   0.8231    0.0132     11.15
#    weak judge      0.50  0.10    3   0.6097    0.0228     11.38
#    strict judge    0.80  0.30    3   0.5239    0.3303     12.23
#    largest gap, closed form vs fundamental matrix: 7.1e-15
#    exact checker, n = 3: escalation per step 0.08^3 = 5.12e-04
#
# Reading: an exact, independent checker turns 43 % into
# better than 99 % for under 11 attempts over ten steps: errors are
# caught and retried instead of propagated, and the residual
# risk falls geometrically with each allowed retry. A judge that
# misses a fifth of errors keeps silent failures in the chain;
# one that rejects too much spends attempts and escalates.

程序用两种方法计算每一行,闭式解与基本矩阵的最大差异是 7.1e-15,互为校验。没有验证器时成功率是 0.4344。一个确定性的、独立的代码检查(s=1s = 1、f=0f = 0),每步最多三次尝试,成功率升到 0.9949,十步总共只需 10.84 次尝试;允许十次时达到 1.0000,尝试 10.87 次。剩下的风险随允许的重试次数几何下降:每步最多三次时,单步升级概率是 0.0830.08^3。这就是提纲所说的「指数级可靠性增益」——它的前提是验证器确定、独立。

用语言模型当评判,情形就不同了:灵敏度 0.8、误报 0.05 的「好评判」把成功率提到 0.8231,剩下的差距来自被放过的静默失败;灵敏度只有 0.5 的「弱评判」只到 0.6097;误报率高达 0.30 的「严苛评判」把大量正确结果打回重做,成功率反而只有 0.5239,三成的任务被升级。

十步链的成功率:无验证器、三票表决(独立与相关错误)、确定性代码检查与语言模型评判。

注

「确定性」的含义。 代码检查之所以接近理想,是因为它的判断不依赖生成器:单元测试、类型检查、模式校验、数值对账。能被写成这类检查的步骤,应当尽量写成;只能由模型评判的步骤,其评判的灵敏度与误报率需要单独测量,而不能假定。

假说 · 尚未验证

作者的假说(尚未验证): 在真实的多步智能体任务中,为至少一半的步骤配上确定性检查(测试、校验、对账)后,端到端成功率的提高幅度,大于把每步换成更强的生成模型所带来的提高。什么结果会证明它错:在预先登记的任务集上,更换生成模型带来的配对提高大于加检查的提高。

小结

串联放大误差:每步 92% 可靠的十步链只有 43.4% 能成功。三票表决只在错误独立时有效,共同难例一多,收益就消失。在每步之后放一个确定、独立的检查,把失败从「传播」变成「重做」,十步链的成功率升到 99% 以上而成本几乎不增;语言模型评判则受其灵敏度与误报率限制,放过的错误留在链中,过严的评判把正确结果也打回去。

参考文献

Kemeny, John G., and J. Laurie Snell. 1960. Finite Markov Chains. Van Nostrand.
Ladha, Krishna K. 1992. “The Condorcet Jury Theorem, Free Speech, and Correlated Votes.” American Journal of Political Science 36 (3): 617–34.

Fan, Y. (2026). Retrieval-Augmented Generation Geometry, Reciprocal Rank Fusion and Multi-Agent Error Cascades: High-Dimensional Concentration of Measure, Hybrid Lexical-Dense Retrieval Bounds and Verifier-Gated Fixed Points [检索增强生成几何、倒数秩融合与多智能体误差级联] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090170 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).