Multi-Agent Collaborative Coding: A Practical Guide to Five Stages and Two Roles
This article presents a practical framework for multi-agent collaborative programming that overcomes the limitations of single-model coding. It explains three common bottlenecks—requirement drift, lack of adversarial review, and context dilution—and introduces a five-stage workflow driven by two complementary roles: a planner and an executor. The guide covers how to turn requirements into a structured specification, stabilize model behavior with a project constraint file, implement module by module, run cross-audits to converge on disagreements, and design tests with targeted manual testing. It also lists common anti-patterns and explains why documentation-based contracts and adversarial review improve delivery predictability. Readers will learn how to structure AI-assisted development so that building, testing, and review become a reusable engineering process rather than a one-shot attempt.
1. Background: Three Bottlenecks of Single-Model Programming
Many developers run into the same problem with AI coding tools: you send the full requirements in one pass, get code that looks complete, and then keep finding errors in the details. In practice, the single-model approach tends to hit three bottlenecks.
The first is requirement drift. Natural-language requirements are inherently ambiguous, so the model fills in the assumptions it considers reasonable. Only after the code is written do you notice the direction has gone off track. The second is the lack of adversarial review. The same model finds it hard to genuinely challenge its own solution and tends to push mistakes forward as if they were correct. The third is context dilution. As the number of files grows, the context the model can focus on becomes thinner, and it becomes more likely to forget earlier constraints.
The value of multi-role collaboration is that it separates “thinking it through,” “writing it,” “finding faults,” and “testing it” into distinct responsibilities, using the workflow to compensate for the limits of any single point of capability.
2. Overall Framework: Five Stages and Two Roles
A recommended workflow has five stages: requirement discussion, solution finalization, modular implementation, code audit, and test design and execution. Only two roles are needed: one focused on planning and review (the “planner”), and one focused on implementation and execution (the “executor”). The two work in turns, and the intermediate artifacts are versionable documents and code.
The overall data flow can be sketched as follows:
Requirements → [Planner] discussion draft → [Executor] rebuttal → convergence → finalized spec→ [Executor] modular implementation → [Planner] audit → exchange and convergence→ finalized test plan → execution → manual supplementary testing → fixes
The key point is that stages are bound by documents as contracts, not by conversational memory.
3. Stage 1: Turning Requirements into a Convergent Specification Document
Do not ask the model to write code directly. First have it produce a structured requirements-and-constraints document (a harness document). This document should include at least: goals and scope, non-goals, input/output conventions, data structures, error-handling strategy, performance and capacity assumptions, and acceptance criteria.
A usable skeleton looks like this:
# Module Specification## Goals## Non-goals (explicitly out of scope)## Interface conventions- Input:- Output:- Exceptions:## Data model## Boundaries and constraints## Acceptance criteria (testable)
Then introduce an adversarial step: have the executor challenge the planner’s document, and let the planner respond, repeating for several rounds. “Convergence” here does not mean whoever is louder wins; it means whether disagreements are recorded and closed one by one. Keep a “disagreement list” where each entry contains the problem description, both sides’ views, the conclusion, and the affected scope. When the number of open items in the list drops to zero, the document is finalized.
In practice, all discussion should stay in a “plan only, do not modify files” mode, so that a large amount of half-finished code is not produced before the direction is settled.
4. Stage 2: Stabilizing Model Behavior with a Constraint File
Once the spec is finalized, you need a long-lived project constraint file (in the widely adopted CLAUDE.md style) that carries hard rules not open to repeated discussion: directory structure, naming conventions, principles for adding dependencies, logging and exception conventions, prohibited actions, and a pre-commit checklist.
Its role is similar to fixed compiler switches: it locks down “things that would otherwise have to be repeated every time,” reducing randomness. The constraint file should be short, actionable, and verifiable, rather than a long passage of value statements. Example:
# Project Constraints- Use only the standard library and approved dependencies- All external inputs must be validated- Prohibited from directly concatenating query statements in the business layer- Each module must include unit tests- Static checks and tests must be run before committing
The constraint file and the spec document have clear division of labor: the former is “always obey,” the latter is “what this module should be built into.”
5. Stage 3: Modular Implementation and Granularity Control
The core of the implementation stage is not speed but controllability. Proceed module by module, and only move to the next when each module meets three conditions: its interface matches the spec, unit tests pass, and static checks show no new warnings.
For granularity, follow the principle that “one module should be coverable by a single review.” If modules are too large, audit feedback becomes vague; if modules are too small, integration cost rises. A common approach is to split business flows vertically while keeping a shared foundation layer horizontal.
At the prompt level, provide the executor with the relevant excerpts of the spec document together with the constraint file, and explicitly require that any deviation from the spec must be explained, not silently changed.
6. Stage 4: Cross-Audit and Disagreement Convergence
After each module is completed, switch to the planner role for an audit, producing structured feedback: blocking issues, compliance issues, and optional improvements. Then have the executor respond point by point, forming several rounds of exchange until opinions align or disagreements are explicitly accepted and logged as risks.
A reference audit checklist:
- Deviations from the spec (interfaces, fields, exceptions)
- Input validation and boundary conditions
- Error handling and resource release
- Dependency and compatibility risks
- Testability and log observability
The value of this stage is shifting defect detection left. An interface ambiguity found during audit costs far less to fix than rework after release.
7. Stage 5: Test Plan Design and Manual Supplementary Testing
The test plan follows the same “generate–review–finalize” process: the planner first drafts an automated test plan covering happy paths, boundaries, exceptions, and regression scope, then discusses feasibility with the executor, and implementation begins after finalization.
The blind spots of automated testing must be acknowledged, especially at the UI and interaction layer. Manual supplementary testing should focus on: visual appearance and layout, interaction feedback, permissions and multi-role scenarios, and behavior under real data. In the original practice, UI defects made up a relatively high proportion, which matches experience: models have limited sensitivity to pixel-level details and state transitions.
8. Common Anti-Patterns and Improvement Suggestions
- Skipping the spec and coding directly: fast in the short term, the most rework in the long term.
- Overly long constraint files: the model’s attention is diluted and the rules become ineffective.
- Audits that only raise style issues: they fail to catch real defects, so blocking issues should be prioritized.
- Disagreements not recorded in a document: verbally converged, then everything is redone in the next conversation.
- Pursuing full automation: UI and business acceptance must retain a manual step.
9. Conclusion
The essence of multi-role collaboration is replacing the fantasy of “getting it right in one pass” with document contracts and adversarial review. For teams that want to improve delivery predictability, using cloud-based general-purpose container platforms, logging services, and monitoring and alerting capabilities to unify build, test, and runtime data can turn the collaborative workflow from a personal skill into a reusable engineering standard. This is also the engineering direction advocated by cloud providers such as Baidu AI Cloud.
