🎯 今日核心问题

  1. 两个 Agent 给出矛盾结论时,为什么「再叫一个 Agent 来判」经常是最差的选择?
  2. 仲裁的优先级顺序应该怎么排,才能既便宜又可解释?
  3. 仲裁完还不一致时,产物该怎么交付?

📖 精读提纲

【一】先分清三种「矛盾」,别用同一种手段处理

「冲突」这个词掩盖了三类完全不同的问题,处理成本差一个数量级:

统计上,生产系统里的冲突绝大多数是前两类。先做分类,再谈仲裁——把 80% 的冲突挡在查证层,仲裁成本立刻就落到可接受区间。

【二】仲裁的四级阶梯

按成本从低到高排,能用低层解决就绝不升层:

再加一个硬约束:仲裁只能升级一次。如果 L3 之后仍有分歧,不允许「再叫一个 Agent 判裁判」——那是无限递归的开始。

【三】裁判要防的三种偏差

第一是长度偏差:更长的答案更容易被判为更有道理,所以裁决前要把双方输入裁到等长。第二是位置偏差:排第一的候选胜率明显偏高,所以要跑两次交换顺序,不一致就判 UNRESOLVED。第三是顺从偏差:裁判倾向于说「两者都有道理」,所以必须允许并鼓励输出 UNRESOLVED,而不是逼它选一个。

【四】未决时怎么交付

分歧确实是信息,不要为了「看起来完成」而强行合并。正确做法是带分歧交付:主结论走可信度更高的一方,同时把分歧以「待确认」区块列出,附上各自依据与来源。用户拿到的是一个诚实的产物,而不是一个被抹平的假答案。在工单、合同、财报这类场景里,一个标了「两处数据不一致,需人工确认」的报告,价值远高于一个自信的错误结论。

💻 代码示例

四级阶梯的仲裁器,含交换顺序的双跑与未决处理。

@Service
@RequiredArgsConstructor
@Slf4j
public class ConflictArbiter {

    private final ToolRegistry tools;          // W2 的工具注册表
    private final ChatModel judge;
    private final ObjectMapper om = new ObjectMapper();

    private static final double MIN_CONFIDENCE = 0.65;

    public Verdict arbitrate(Conflict c) {
        // L0:字段级口径比对(零 LLM)
        Optional<Verdict> l0 = compareShape(c);
        if (l0.isPresent()) return l0.get();

        // L1:工具查证权威值
        Optional<Verdict> l1 = verifyByTool(c);
        if (l1.isPresent()) return l1.get();

        // L2:权威来源优先
        Optional<Verdict> l2 = byAuthority(c);
        if (l2.isPresent()) return l2.get();

        // L3:LLM 仲裁,等长输入 + 双跑交换顺序
        JudgeResult a = askJudge(c, false);
        JudgeResult b = askJudge(c, true);
        if (!a.winner().equals(b.winner()))
            return Verdict.unresolved(c.field(), "裁判存在位置偏差,A/B 双跑结论不一致");
        if (a.confidence() < MIN_CONFIDENCE)
            return Verdict.unresolved(c.field(), "裁判置信度不足 " + a.confidence());

        log.info("L3 仲裁 field={} winner={} conf={}", c.field(), a.winner(), a.confidence());
        return new Verdict(c.field(), a.winner(), a.reason(), a.confidence(), null);
    }

    private Optional<Verdict> compareShape(Conflict c) {
        // 同一指标不同口径 → 不是冲突,是标签错误
        Set<String> dims = c.candidates().stream()
                .map(s -> s.split("\|")[0]).collect(Collectors.toSet());
        if (dims.size() > 1) {
            return Optional.of(new Verdict(c.field(), "LABEL_FIX",
                    "候选指标口径不一致(" + dims + "),应分别呈现", 1.0, null));
        }
        boolean allEqual = c.candidates().stream()
                .map(s -> s.split("\|")[1]).distinct().count() == 1;
        return allEqual
                ? Optional.of(new Verdict(c.field(), "AGREE", "归一化后一致", 1.0, null))
                : Optional.empty();
    }

    private Optional<Verdict> verifyByTool(Conflict c) {
        String tool = c.field().endsWith("Rate") ? "queryFinancialMetric" : null;
        if (tool == null) return Optional.empty();
        try {
            String truth = tools.invoke(tool, om.writeValueAsString(Map.of("field", c.field())));
            String winner = c.candidates().stream()
                    .filter(s -> s.contains(truth.trim())).findFirst().orElse(null);
            return winner == null ? Optional.empty()
                    : Optional.of(new Verdict(c.field(), winner, "工具查证命中", 1.0, null));
        } catch (Exception e) {
            log.warn("L1 查证失败 {}: {}", c.field(), e.getMessage());
            return Optional.empty();
        }
    }

    private Optional<Verdict> byAuthority(Conflict c) {
        Map<String, Integer> rank = Map.of("REGULATION", 4, "POLICY", 3, "CONVENTION", 2, "EXP", 1);
        List<Sourced> sorted = c.sourced().stream()
                .sorted(Comparator.comparingInt(
                        (Sourced s) -> rank.getOrDefault(s.sourceType(), 0)).reversed())
                .toList();
        if (sorted.size() >= 2 && !sorted.get(0).sourceType().equals(sorted.get(1).sourceType()))
            return Optional.of(new Verdict(c.field(), sorted.get(0).toString(),
                    "依据权威来源优先:" + sorted.get(0).sourceType(), 0.95, null));
        return Optional.empty();
    }

    private JudgeResult askJudge(Conflict c, boolean swap) {
        List<String> cands = new ArrayList<>(c.candidates());
        if (swap) Collections.reverse(cands);
        String prompt = """
                你是仲裁者。两个候选结论依据不同。请只依据证据强度判断。
                若证据不足以判断,必须输出 UNRESOLVED,不要折中。
                输出 JSON:{"winner":"候选原文","reason":"","confidence":0.0}

                字段:""" + c.field() + """
                候选:
                """ + String.join("\n", cands);
        try {
            String raw = judge.chat(prompt);
            return om.readValue(raw.substring(raw.indexOf('{'), raw.lastIndexOf('}') + 1),
                    JudgeResult.class);
        } catch (Exception e) {
            return new JudgeResult("UNRESOLVED", "解析失败", 0.0);
        }
    }

    public record Verdict(String field, String winner, String reason,
                          double confidence, String pendingNote) {
        static Verdict unresolved(String field, String why) {
            return new Verdict(field, "UNRESOLVED", why, 0.0, why);
        }
    }
    public record JudgeResult(String winner, String reason, double confidence) { }
}

注意两个细节:compareShape 里把候选格式约定成 口径|数值|来源类型,这样归一化比对是纯字符串操作,比让模型判断便宜三个数量级;askJudge 的 swap 双跑是防位置偏差的最小成本方案,成本只翻一倍,但能挡掉最隐蔽的一类错判。

✏️ 今日动手

构造三组冲突喂给 ConflictArbiter:① 两个 Agent 给出不同毛利率(数值型,应走 L1 查证);② 一个 Agent 引用旧版报销制度、一个引用新版(应走 L2 权威优先);③ 两个方案优劣之争(应走 L3 仲裁)。要求:① 打印每个冲突命中的层级与耗时;② 人为让 L3 双跑结果不一致,验证返回 UNRESOLVED 而不是随便选一个;③ 在最终产物里写一个「待确认」区块,把 pendingNote 填进去。

🔖 明日预告

到这里多 Agent 的能力已经齐了,但账单也起来了:3 个 Agent 跑一轮就是 3 倍 token,加上仲裁和重试可能到 5 倍。明天算账——什么指标下多 Agent 是净赚的,怎么把成本压在单 Agent 的 1.5 倍以内,以及什么时候该直接判死刑。