多智能体交接:契约、角色与失败模式
明确的契约、清晰的角色和结构化的交接,能让多智能体编程工作流免于陷入混乱——并附上今天就能采用的模式。
The most common failure in a multi-agent coding workflow is not a bad model — it is a bad handoff. When Agent A passes context to Agent B without a shared understanding of what was decided, the second agent either re-does the work or makes assumptions that contradict the first. The result is overlapping effort, contradictory code, and a review that takes longer than doing it yourself.
Distributed systems have a name for this: the need for contracts between autonomous components. In the agentic context, a contract is a structured agreement about what each agent produces, in what format, and with what guarantees. Teams that define these contracts spend less time debugging inter-agent friction and more time shipping.
Why handoffs fail
| Failure mode | What happens | Cost |
|---|---|---|
| Context loss | Agent B receives a summary that omits a key constraint | Incorrect output; rework |
| Role drift | Agent B expands scope beyond its brief | Overlapping or conflicting code |
| Silent contradiction | Agent B’s changes violate Agent A’s assumptions | Regressions found in review or test |
| Sequential latency | Agent B cannot start until Agent A is done | Pipeline stalls on slow agents |
| Output mismatch | Agent B expects JSON; Agent A delivered Markdown | Failed parse; manual rework |
Each pattern appears when agents share a repository without sharing a protocol. The fix is better interfaces. Microsoft’s guidance on competing consumers makes the same argument: formalise the message schema before writing the consumers.
Contracts: what each agent promises
A contract defines the interface between two agents. It says: here is what I produce, in this format, with these guarantees. The receiving agent can depend on it without reading the producing agent’s internal reasoning.
contract:
agent: "code-reviewer"
produces: "review-summary"
format:
type: "json"
schema:
files_approved: ["array", "string"]
files_rejected: ["array", "object"]
files_rejected[].path: "string"
files_rejected[].reason: "string"
files_rejected[].severity: "blocker" | "warning" | "nit"
guarantees:
- "All file paths are relative to repository root"
- "Blocker severity requires human resolution"
- "No code modifications are made by this agent"
A contract need not be this formal on every team — but the properties it captures matter. Contracts reduce ambiguity by externalising the mental model of what each agent should produce. When you launch a second agent to continue work a first agent started, include the contract rather than the entire conversation. The contract is the shared boundary; everything else is implementation detail.
Roles: boundaries that prevent mission creep
Role drift — an agent meant to review code deciding to rewrite it — is the most common handoff pathology. Roles bound what an agent is allowed to do. Each agent needs a scope boundary, a decision authority, an escalation path, and an output contract.
| Role | Scope | Authority | Output |
|---|---|---|---|
| Planner | Architecture and file structure | Approves plan; passes to Implementer | Plan document with file map and data flow |
| Implementer | Code in scoped files only | Writes code; does not change architecture | Feature branch with commits |
| Reviewer | All changed files | Approves or rejects; does not rewrite | Review summary with severity-annotated findings |
| Tester | Test files only | Writes and runs tests | Test report with pass/fail per scenario |
Role boundaries create predictable interfaces between stages. When an Implementer writes code and nothing else, and a Tester verifies and does not debug, the pipeline becomes linear and inspectable. Each stage produces a known artefact for the next.
Failure modes in practice
Contracts and roles do not eliminate every failure — they make failures visible and recoverable. Here is how the common modes look when things still go wrong.
Context loss through summarisation
When Agent A hands off by summarising, details compress. “Refactored the auth flow” loses the decision to use refresh tokens over opaque session IDs. Agent B rewrites refresh tokens back to sessions because it sees a simpler path.
The fix: pass the full decision log, not a summary. Treat the handoff artefact as a trace of reasoning — the signal is in the rejected alternatives, not the final output alone.
Role drift as a feature request
Sometimes role drift signals a missing role. If the Implementer keeps fixing lint warnings, the pipeline may need a dedicated Formatter. Monitor drift patterns: repeated boundary violations usually indicate a gap in the workflow design, not a misbehaving agent.
Silent contradictions in merged code
Two agents working in parallel can produce code that compiles but contradicts — one changes an API contract, the other consumes the old shape. The fix is a shared schema file that both agents read and a CI step that fails if the schema changes without updating all consumers.
The Liskov substitution principle captures this formally: if a downstream agent depends on a base contract, the upstream agent must not change it without a version bump.
Designing your handoff protocol
Handoff from Planner to Implementer.
The plan (attached) defines:
- Files to create: src/lib/auth.ts, src/hooks/useAuth.ts
- Data flow: JWT stored in httpOnly cookie, read by middleware
- Constraints: no external auth library; use Web Crypto API
Implementer scope:
- Write code only in the files listed above
- Do not modify existing routes, middleware, or types
- Output: a branch with one commit per file
Review threshold: if a design decision contradicts the plan,
stop and flag for Planner — do not override.
This prompt structures the handoff explicitly: what to work on, what not to touch, and what to do on conflict. The review threshold is the critical line — it prevents silent overrides when agents disagree.
The benefits of getting it right
Teams that invest in handoff protocols report fewer merge conflicts, shorter review cycles, and more predictable pipeline times. The overhead of writing a contract — a few lines of YAML or a structured prompt — pays for itself the first time it prevents one agent from silently undoing another agent’s work.
For a deeper treatment of control loops and agent reliability, see Harness Engineering. If you are designing production agent teams, Agentic Coding Pro covers the PLAN methodology for structuring role hierarchies and handoff protocols across services.
The agent handoff is the architecture
The quality of a multi-agent system is bounded by the quality of its interfaces. Choose explicit contracts over implied understanding. Choose bounded roles over flexible agents. Choose structured handoffs over summarised context.
更多文章
全部文章Vibe Coding 新手必读:读懂 AI 所写 JavaScript 的最低要求
学会刚好够用的 JavaScript,去读懂 AI 生成的前端代码——包括变量、函数、DOM 基础,以及提示你应当重新提问的危险信号。
阅读全文