导演Showrunner
用验证器跑通多智能体 SOPRun a multi-agent SOP with a verifier
任务简报Brief
产品发布前,内容团队要交一份 launch checklist。初稿不能直接上线:必须包含回滚演练,并由独立验证器审核;被拒后要带着反馈重写,而不是无限循环。
Before a launch, the content team must deliver a launch checklist. The draft cannot ship directly: it must include a rollback rehearsal and pass an independent verifier. A rejection must feed back into a rewrite, not an infinite loop.
多智能体最容易犯的错,是让两个 Agent 互相聊天直到“感觉可以”。这一关引入三个硬约束:结构化交接(handover)、结构化裁决(verdict)和重试预算(max_attempts)。Python 用 SOPEngine 直接表达状态机;Java 2.0.4 没有对应引擎,于是手工编排执行器与验证器。两条轨道最终产出同一种 sop_run.json,你可以对比“框架帮你做了什么”。
The classic multi-agent failure is two agents chatting until it “feels done.” This mission adds three hard constraints: a structured handover, a structured verdict and a retry budget. Python expresses the state machine with SOPEngine; Java 2.0.4 has no equivalent, so the track orchestrates executor and verifier by hand. Both produce the same sop_run.json, letting you compare what the framework does for you.
学习目标Learning goals
用 SOP / 手工循环表达“执行 → 验证 → 重试 → 交接”。
Express execute → verify → retry → hand over with SOP or a manual loop.
让验证器返回结构化 verdict,而不是自然语言“还行”。
Make the verifier return a structured verdict, not a vague “looks fine.”
验证首次被拒、第二次通过,且执行器/验证器调用次数符合预算。
Verify a rejection then a pass, with executor/verifier call counts within budget.
把流程状态落盘,形成可观测的产物。
Persist the flow state as an observable artifact.
核心概念Core concepts
SOP / SOPStep
把流程拆成有主题、执行器、验证器和最大尝试次数的步骤。
Splits a flow into steps with a subject, executor, verifier and max attempts.
SOPEngine
驱动 SOP 状态机,发出 CustomEvent,并维护每步的验证记录。
Drives the SOP state machine, emits CustomEvents and keeps verification records.
Verifier
独立审核者;职责是拒绝不合格交接,而不是帮执行器改写。
An independent reviewer whose job is to reject bad handovers, not rewrite them.
Structured handover / verdict
交接与裁决都使用固定 schema;否则“通过”只是字符串,无法驱动状态机。
Both handover and verdict use fixed schemas; otherwise “passed” is just a string and cannot drive a state machine.
双轨实现Build it twice
先完整跑通一条轨道,再切换重来。两条轨道写出同一种产物,方便直接对照行为。Run one track end to end, then switch and repeat. Both tracks write the same artifact so you can compare behavior directly.
SOPEngine 在 Python 里是一等公民;步骤、验证器、最大尝试次数都由框架调度。你只需要提供 Agent 和结构化的裁决模型。
SOPEngine is a first-class citizen in Python; steps, verifier and max attempts are scheduled by the framework. You only supply agents and structured verdicts.
声明 SOP 与步骤Declare the SOP and steps
第一步带 verifier 和 max_attempts=3;第二步没有 verifier,表示执行一次即完成。
The first step has a verifier and max_attempts=3; the second has none, meaning one execution completes it.
from agentscope.sop import SOP, SOPEngine, SOPStep
sop = SOP(
name="launch-review",
description="Draft a launch checklist, survive review, then approve release.",
steps=[
SOPStep(subject="Draft launch checklist",
executor=executor, verifier=verifier, max_attempts=3),
SOPStep(subject="Approve release", executor=executor),
],
)
engine = SOPEngine(sop)
结构化裁决Structured verdicts
验证器必须返回 {passed, message}。message 会被喂回执行器,所以它要具体到“缺什么”。
The verifier must return {passed, message}. The message is fed back to the executor, so it must say exactly what is missing.
class Verdict(BaseModel):
passed: bool
message: str
class Handover(BaseModel):
handover: str
verifier_model = ScriptedModel([
structured_response({"passed": False,
"message": "Missing a rollback rehearsal; add it."}),
structured_response({"passed": True,
"message": "Rollback is covered."}),
])
运行并检查预算Run and check the budget
check 断言两步都完成、第一步两次尝试、裁决为 [False, True]、执行器调用 3 次、验证器调用 2 次。
The check asserts both steps complete, the first step needs two attempts, verdicts are [False, True], executor calls are 3 and verifier calls are 2.
python lab.py check m4
cat work/m4/sop_run.json
# 期望看到
# PASS CODE: AS-M4-W3JZ
python lab.py check m4
run 会把 CustomEvent 打印出来;check 则直接断言状态机结果。两者共用同一套 SOP 定义。
run prints CustomEvents; check asserts the state machine result. Both share the same SOP definition.
lab/missions/m4_studio/mission.py判题证明了什么What the check proves
- 两个 SOPStep 都通过 SOPEngine 完成Both SOPSteps completed through SOPEngine
- 验证器拒绝第一次、接受第二次The verifier rejected attempt one and accepted attempt two
- 被接受的 handover 包含回滚演练The accepted handover includes a rollback rehearsal
- 产物:lab/work/m4/sop_run.jsonArtifact: lab/work/m4/sop_run.json
Java core 2.0.4 没有 SOPEngine;这一关手工实现状态机。你仍然使用两个 ReActAgent、结构化输出和明确的重试预算,只是调度逻辑由你掌握。
Java core 2.0.4 has no SOPEngine; this mission implements the state machine by hand. You still use two ReActAgents, structured output and an explicit retry budget, but you own the scheduling.
结构化调用Structured calls
call(..., Type.class) 让 ReActAgent 走 generate_response;record 直接映射字段,不需要手工解析 JSON。
call(..., Type.class) makes ReActAgent use generate_response; records map fields directly without manual JSON parsing.
public record Handover(String handover) {}
public record Verdict(boolean passed, String message) {}
Handover draft = callStructured(executor, task, Handover.class, "m4-executor");
Verdict verdict = callStructured(verifier,
"Review this launch checklist:\n" + draft.handover(),
Verdict.class, "m4-verifier");
手工验证循环Manual verifier loop
循环最多三次;通过就接受交接,否则把验证器的 message 作为下一轮反馈。这就是 SOPEngine 在幕后做的事。
Loop at most three times; accept on pass, otherwise feed the verifier's message into the next round. This is what SOPEngine does behind the scenes.
String feedback = "";
String accepted = "";
List<Boolean> verdicts = new ArrayList<>();
for (int attempt = 1; attempt <= 3; attempt++) {
String task = attempt == 1
? "Draft a launch checklist with rollback."
: "Revise the checklist. Reviewer said: " + feedback;
Handover draft = callStructured(executor, task, Handover.class, "m4-executor");
Verdict verdict = callStructured(verifier,
"Review:\n" + draft.handover(), Verdict.class, "m4-verifier");
verdicts.add(verdict.passed());
if (verdict.passed()) { accepted = draft.handover(); break; }
feedback = verdict.message();
}
编译并判题Compile and check
检查会验证两步状态、尝试次数 [2,1]、裁决 [[false,true],[true]]、接受文本包含 rollback,以及调用计数。
The check validates two step states, attempts [2,1], verdicts [[false,true],[true]], accepted text containing rollback, and call counts.
cd lab/java
mvn -q -o compile exec:java -Dexec.args="check m4"
# 期望看到
# PASS CODE: AS-M4-W3JZ
cd lab/java && mvn -q -o compile exec:java -Dexec.args="check m4"
没有 SOPEngine 不是缺陷,而是提醒:状态机、预算和裁决 schema 本来就是你的业务责任。框架只提供 Agent 与结构化输出。
No SOPEngine is not a defect; it is a reminder that the state machine, budget and verdict schema are your business responsibility. The framework provides agents and structured output.
lab/java/src/main/java/lab/m4/M4Studio.java判题证明了什么What the check proves
- 两个手工步骤通过显式验证循环完成Two manual steps completed through an explicit verifier loop
- 验证器拒绝第一次、接受第二次The verifier rejected attempt one and accepted attempt two
- 被接受的交接包含回滚演练The accepted handover contains a rollback rehearsal
- 产物:lab/work/m4/sop_run.jsonArtifact: lab/work/m4/sop_run.json
Python ↔ Java 差异Python ↔ Java differences
Python 用
SOP/SOPEngine表达流程;Java 2.0.4 没有对应 API,需要手工维护 steps、attempts 和 verdicts。Python uses
SOP/SOPEngine; Java 2.0.4 has no equivalent, so steps, attempts and verdicts are maintained by hand.两条轨道都用结构化裁决;Python 通过
structured_response脚本,Java 通过generate_response+ record。Both use structured verdicts; Python scripts
structured_response, Java usesgenerate_response+ records.Python 的
SOPEngine会发出 CustomEvent;Java 只有 Agent 事件,流程事件需要自己发。Python's
SOPEngineemits CustomEvents; Java only has agent events, so flow events are yours to emit.
常见坑Pitfalls
没有重试上限:验证器一直拒绝,循环会烧光预算。必须设置
max_attempts或手工计数。No retry cap: a verifier that keeps rejecting burns the budget. Set
max_attemptsor count manually.验证器返回自由文本:状态机无法判断“通过”,只能靠字符串匹配,极其脆弱。
A verifier returning free text: the state machine cannot tell “passed” and must string-match, which is brittle.
验证器和执行器共用同一个模型/提示词:容易出现“自己审自己”,拒绝率过低。生产应使用不同提示词、模型或规则。
Sharing one model/prompt between executor and verifier invites self-approval. Use different prompts, models or rules in production.
交接内容缺少证据:只写“已完成”而不写回滚演练、测试结果或负责人,验证器只能拒绝或放水。
Handovers without evidence: “done” without rollback, test results or owners forces the verifier to reject or rubber-stamp.
领取通行码Claim your pass code
先运行当前轨道的 check。CLI 会打印 PASS CODE、XP 与徽章。粘贴到这里;同一关在每条轨道可各领取一次。Run the check for the current track. The CLI prints PASS CODE plus the exact XP and badge. Paste it here; the same mission can be claimed once on each track.
费曼复述Feynman recall
合上教程,用自己的话讲清机制。关键点覆盖 65%、篇幅 20%、结构 15%;60 分通过并一次性获得 20 XP。Explain the mechanism without looking. Coverage 65%, length 20%, structure 15%; 60 passes and earns +20 XP once.
为什么多智能体流程需要验证器和重试预算?一次“被拒 → 重写 → 通过”的循环里,状态如何变化?
Why does a multi-agent flow need a verifier and a retry budget? How does state change during a reject → rewrite → pass loop?
查看参考答案Show the model answer
验证器把“是否合格”从主观感觉变成可执行的状态转换:通过才允许交接,不通过则把具体缺口喂回执行器。重试预算是安全阀,防止双方陷入无限循环并烧光成本。一次循环里,步骤从执行中进入验证;第一次 verdict 为 false,步骤保持未完成并携带反馈重试;第二次为 true,步骤完成并接受 handover。
A verifier turns “is it good enough?” into an executable state transition: only a pass allows handover; a rejection feeds the specific gap back to the executor. The retry budget is a safety valve against infinite loops and runaway cost. In one loop, a step goes from executing to verifying; the first verdict is false, so the step stays incomplete with feedback and retries; the second is true, so the step completes and accepts the handover.
间隔复习Spaced review
首次领取后会在明天安排复习。按真实感受评分,下一次间隔会自适应。After the first claim a review is scheduled for tomorrow. Rate how it felt; the next interval adapts.