建立自動化 Multi-Agent Coding Workflow:讓 Codex、Claude Code、Cursor 與 Grok 自動協作
版本:2026年 10月 16日
- 目標
- 1. Agent 分工
- 2. 整體 Architecture
- 3. 建議 Project Structure
- 4.
task.md - 5.
AGENTS.md - 6. Orchestrator
- 7. 第一版
orchestrator.py - 8. Feedback Loop
- 9. Claude Fix Prompt
- 10. 第一版 Safety Rules
- 11. Git Branch
- 12. 第二階段:Git Worktree
- 13. 唔好一開始 Parallel 改同一個 Folder
- 14. 第三階段:GitHub Issue → AI
- 15. 第四階段:CI Feedback
- 16. 最終 Architecture
- 17. 建議開發次序
- 18. 最重要原則
- 19. 第一個實驗建議
- 20. 最終目標
Multi-Agent 自動化寫 Code 工作流程
Section titled “Multi-Agent 自動化寫 Code 工作流程”建立一個可以由多個 AI Coding Agent 自動分工合作嘅開發流程,減少手動 copy & paste。
基本概念係:
Requirement ↓Orchestrator ↓Codex → Claude Code → Cursor Agent → Grok ↓Tests / Review ↓PASS → Git Diff / Pull RequestFAIL → 自動回到 Claude Code 修正你只需要寫一次 Requirement,之後由 Orchestrator 自動將工作交俾唔同 Agent。
1. Agent 分工
Section titled “1. Agent 分工”第一版建議唔好俾四個 Agent 同時改同一批 files,而係用 Sequential Pipeline。
Codex — Architect / Planner
Section titled “Codex — Architect / Planner”主要工作:
- 讀 Requirement
- 分析 repository
- 理解現有 architecture
- 找出受影響 files
- 拆 implementation steps
- 定義 tests
- 找出 edge cases / risks
- 產生
plan.md
Codex 喺呢一步主要負責「諗」,唔改 code。
Claude Code — Main Developer
Section titled “Claude Code — Main Developer”主要工作:
- 讀
task.md - 讀
plan.md - 讀
AGENTS.md - Implement feature / bug fix
- 修改 source code
- 加 automated tests
- Run build / tests
- 修正 implementation errors
Claude Code 係第一版 workflow 入面嘅主要 coder。
Cursor Agent — Integration / Tester
Section titled “Cursor Agent — Integration / Tester”主要工作:
- 讀 Requirement
- Review Claude Code 嘅 Git diff
- Run build
- Run unit tests
- Run integration tests
- Run lint
- Run type checking
- 發現 integration 問題
- 必要時修正 implementation 問題
Cursor Agent 主要係確認:
Claude 寫完之後,成個 project 真係仍然可以正常運作。
Grok — Independent Reviewer
Section titled “Grok — Independent Reviewer”Grok 最好唔參與原本 implementation。
主要工作:
- Review current Git diff
- 找 logic bugs
- 找 security issues
- 找 regression
- 找 race conditions
- 找 missing edge cases
- 找 missing tests
- 找 architecture problems
- 找 unnecessary complexity
最後只需要俾一個清楚 verdict:
VERDICT: PASS或者:
VERDICT: FAIL如果 FAIL,就列出 blocking issues。
Grok 作為獨立 reviewer 嘅好處係:
唔係由寫 code 嗰個 model 自己 review 自己。
2. 整體 Architecture
Section titled “2. 整體 Architecture” YOU │ │ .ai/task.md │ ▼ ┌──────────────┐ │ Orchestrator │ │ Python │ └──────┬───────┘ │ ▼ ┌──────────────┐ │ Codex │ │ Architect │ └──────┬───────┘ │ .ai/plan.md │ ▼ ┌──────────────┐ │ Claude Code │ │ Developer │ └──────┬───────┘ │ Git Diff │ ▼ ┌──────────────┐ │ Cursor Agent │ │ Test / Int. │ └──────┬───────┘ │ Test Report │ ▼ ┌──────────────┐ │ Grok │ │ Reviewer │ └──────┬───────┘ │ ┌───────┴────────┐ │ │ PASS FAIL │ │ ▼ ▼ Stop / PR Claude Fix │ ▼ Cursor Retest │ ▼ Grok Review3. 建議 Project Structure
Section titled “3. 建議 Project Structure”例如 MarketLens:
MarketLens/│├── .ai/│ ├── task.md│ ├── plan.md│ ├── implementation.md│ ├── test-report.md│ └── review.md│├── AGENTS.md├── orchestrator.py│├── src/├── tests/└── ...4. task.md
Section titled “4. task.md”你日後主要需要寫呢個 file。
例如:
# Task
Add support for scanning a single ticker.
Example:
python scan.py --ticker NVDA
## Requirements
- Existing watchlist scan must continue working.- Validate invalid ticker input.- Support lowercase input.- Add automated tests.- Do not change unrelated functionality.
## Definition of Done
- Existing tests pass.- New tests pass.- Lint passes.- Type checking passes.- Independent review returns PASS.5. AGENTS.md
Section titled “5. AGENTS.md”AGENTS.md 可以當成所有 AI developer 都要遵守嘅 project rules。
例如:
# Project Instructions
## Architecture
Python application for stock screening and analysis.
## Coding Rules
- Do not change public APIs without approval.- Keep functions small.- Avoid unnecessary dependencies.- Do not modify unrelated files.- Never commit secrets.- Add tests for new behaviour.
## Validation
Before completing a task run:
pytestruff check .mypy .
All tests must pass.
## Git Rules
- Work on a feature branch.- Never push directly to main.- Do not rewrite unrelated history.6. Orchestrator
Section titled “6. Orchestrator”第一版唔需要 CrewAI、LangGraph 或其他大型 framework。
一個簡單 Python script 已經可以控制四個 CLI Agent。
概念:
Python ↓subprocess ↓Codex CLIClaude CLICursor CLIGrok CLI7. 第一版 orchestrator.py
Section titled “7. 第一版 orchestrator.py”from pathlib import Pathimport subprocess
ROOT = Path(__file__).parentAI_DIR = ROOT / ".ai"
AI_DIR.mkdir(exist_ok=True)
def run(command: list[str], prompt: str) -> str:
result = subprocess.run( command + [prompt], cwd=ROOT, text=True, capture_output=True, encoding="utf-8", )
if result.returncode != 0: raise RuntimeError(result.stderr)
return result.stdout
# =========================================================# 1. CODEX — ARCHITECT# =========================================================
plan = run( ["codex", "exec"], '''Read:
.ai/task.mdAGENTS.md
Inspect this repository.
Act as the software architect.
Do NOT modify any files.
Produce an implementation plan covering:
- affected files- architecture- implementation steps- tests required- edge cases- risks''')
(AI_DIR / "plan.md").write_text( plan, encoding="utf-8")
# =========================================================# 2. CLAUDE CODE — IMPLEMENTATION# =========================================================
implementation = run( [ "claude", "-p", "--output-format", "text" ], '''Read:
.ai/task.md.ai/plan.mdAGENTS.md
Implement the task.
You may modify source code and tests.
Run the relevant build and tests before finishing.
Rules:
- Do not modify unrelated files.- Do not remove existing tests.- Follow the architecture plan.- Add tests for new behaviour.
At the end summarize what you changed.''')
(AI_DIR / "implementation.md").write_text( implementation, encoding="utf-8")
# =========================================================# 3. CURSOR — INTEGRATION / TESTING# =========================================================
test_report = run( [ "agent", "-p", "--output-format", "text" ], '''Read:
.ai/task.md.ai/plan.mdAGENTS.md
Inspect the current git diff.
Act as an integration engineer.
Run all relevant:
- tests- lint- type checking- build
Fix problems caused by this implementation.
Do not perform unrelated refactoring.
Report:
- commands executed- tests run- failures found- fixes made- final result''')
(AI_DIR / "test-report.md").write_text( test_report, encoding="utf-8")
# =========================================================# 4. GROK — INDEPENDENT REVIEW# =========================================================
review = run( [ "grok", "-p", "--output-format", "plain" ], '''Read:
.ai/task.md.ai/plan.md.ai/test-report.md
Inspect the current git diff.
Act as an independent senior code reviewer.
Do NOT modify the implementation.
Review for:
- logic bugs- security issues- regressions- race conditions- missing edge cases- missing tests- architecture problems- unnecessary complexity
Finish with EXACTLY one of:
VERDICT: PASS
or
VERDICT: FAIL
If FAIL, list the blocking issues clearly.''')
(AI_DIR / "review.md").write_text( review, encoding="utf-8")
print(review)8. Feedback Loop
Section titled “8. Feedback Loop”真正開始變成 Agent Team 嘅關鍵係:
Reviewer FAIL 之後,自動交返俾 Developer。
例如:
Claude ↓Cursor ↓Grok ↓ FAIL ↓Claude Fix ↓Cursor Retest ↓Grok Review可以加入:
MAX_REVIEW_ATTEMPTS = 2
for attempt in range(MAX_REVIEW_ATTEMPTS):
test_report = run_cursor_tests()
review = run_grok_review()
if "VERDICT: PASS" in review: print("Review passed.") break
run_claude_fix(review)9. Claude Fix Prompt
Section titled “9. Claude Fix Prompt”當 Grok 回報 FAIL:
Read:
.ai/task.md.ai/plan.md.ai/review.md
The independent reviewer found blocking issues.
Fix ONLY the blocking issues listed in the review.
Do not perform unrelated refactoring.
Run relevant tests after fixing.然後再:
Claude Fix ↓Cursor Test ↓Grok Review直到:
VERDICT: PASS或者達到最大 retry 次數。
10. 第一版 Safety Rules
Section titled “10. 第一版 Safety Rules”初期唔建議 AI 擁有:
- Production DB write access
- Azure production credentials
- AWS production credentials
- Production deployment permission
- Direct push permission to
main - Auto merge permission
- Secret management permission
第一版最好停喺:
AI Development ↓Tests PASS ↓Review PASS ↓Git Diff / Pull Request ↓STOP ↓Human Approval11. Git Branch
Section titled “11. Git Branch”每一個 task 建議有自己 branch:
main │ └── ai/123-single-ticker-scanAI 只可以改:
ai/*最後先由人 approve merge。
12. 第二階段:Git Worktree
Section titled “12. 第二階段:Git Worktree”當 Sequential Pipeline 穩定之後,可以開始做 Parallel Agents。
Git Worktree 可以俾每個 Agent 有自己 working copy。
例如:
MarketLens/│├── main/│├── worktrees/│ ├── backend/│ ├── frontend/│ ├── tests/│ └── review/之後可以變成:
Requirement │ ▼ Architect │ ┌─────────┼─────────┐ │ │ │ ▼ ▼ ▼ Backend Frontend Tests Agent Agent Agent │ │ │ └─────────┼─────────┘ │ ▼ Integrator │ ▼ Review13. 唔好一開始 Parallel 改同一個 Folder
Section titled “13. 唔好一開始 Parallel 改同一個 Folder”第一版避免:
Claude ─┐Cursor ─┼── 同時修改 src/Grok ───┘因為容易:
- overwrite changes
- merge conflicts
- tests互相影響
- agent唔知道其他 agent 已改咗乜
- debugging變得困難
所以第一版用:
Codex ↓Claude ↓Cursor ↓Grok會穩定好多。
14. 第三階段:GitHub Issue → AI
Section titled “14. 第三階段:GitHub Issue → AI”之後可以將:
GitHub Issue變成 system input。
例如:
GitHub Issue Created ↓Webhook / GitHub Action ↓Create branch ↓Generate .ai/task.md ↓Run orchestrator.py ↓Codex ↓Claude ↓Cursor ↓Grok ↓PASS ↓Create Pull Request到時你甚至唔需要自己建立 task.md。
只需要開 GitHub Issue:
Add support for single-ticker scanningAI team 就自己開始做。
15. 第四階段:CI Feedback
Section titled “15. 第四階段:CI Feedback”下一步可以將 CI errors 自動餵返 Agent。
例如:
Pull Request ↓GitHub Actions ↓pytest FAIL ↓CI log ↓Claude Code ↓Fix ↓Push ↓CI rerun形成:
Code ↓CI ↓FAIL ↓AI Fix ↓CI ↓PASS16. 最終 Architecture
Section titled “16. 最終 Architecture”長遠可以變成:
GitHub Issue │ ▼ AI Orchestrator │ ▼ Architect │ ┌────────────┼────────────┐ │ │ │ ▼ ▼ ▼ Backend Frontend Tests Agent Agent Agent │ │ │ └────────────┼────────────┘ │ ▼ Integrator │ ▼ CI │ ┌───────┴───────┐ │ │ PASS FAIL │ │ ▼ ▼ Reviewer Developer │ │ ┌──────┴──────┐ │ │ │ │ PASS FAIL ◄────┘ │ ▼ Pull Request │ ▼ Human Approval │ ▼ Merge17. 建議開發次序
Section titled “17. 建議開發次序”唔好一次過做到最複雜。
建議:
Phase 1
Section titled “Phase 1”task.md ↓Codex ↓Claude ↓Cursor ↓Grok目標:
四個 Agent 可以完全唔需要 copy & paste 自動接力。
Phase 2
Section titled “Phase 2”加入:
Grok FAIL ↓Claude Fix ↓Cursor Test ↓Grok ReviewPhase 3
Section titled “Phase 3”加入:
- Structured JSON output
- Retry limits
- Error handling
- Logs
- Token / cost tracking
- Run history
Phase 4
Section titled “Phase 4”加入:
- Git branches
- Git worktrees
- Parallel agents
Phase 5
Section titled “Phase 5”加入:
GitHub Issue ↓Agent Team ↓Pull RequestPhase 6
Section titled “Phase 6”加入:
CI FAIL ↓AI Fix ↓CI RetestPhase 7
Section titled “Phase 7”最後先考慮:
- Auto merge
- Auto deploy
- Production changes
呢啲應該最後先開。
18. 最重要原則
Section titled “18. 最重要原則”AI 唔應該自己定義「完成」
Section titled “AI 唔應該自己定義「完成」”一定要有客觀 Definition of Done,例如:
Build passesTests passLint passesType checking passesSecurity review passesIndependent reviewer passesTests 係 Agentic Coding 最重要嘅 Guardrail
Section titled “Tests 係 Agentic Coding 最重要嘅 Guardrail”越自動化,tests 越重要。
一個有良好 automated tests 嘅 repository:
普通 AI Model+Strong Tests好多時會比:
最強 AI Model+No Tests可靠。
不同 Model 做不同角色
Section titled “不同 Model 做不同角色”唔需要每個 Agent 都用最強 model。
可以:
Codex→ Architecture / Planning
Claude Code→ Implementation
Cursor→ Integration / Testing
Grok→ Independent Review最大價值唔係「四個 AI 一齊寫」。
而係:
四個 AI 有清楚角色、清楚輸入、清楚輸出,同埋客觀驗證流程。
19. 第一個實驗建議
Section titled “19. 第一個實驗建議”可以用一個自己嘅 project,例如 MarketLens,揀一個細 feature。
例如:
Add:
python scan.py --ticker NVDA成功條件:
Codex writes plan ↓Claude implements ↓Cursor tests ↓Grok reviews ↓PASS如果呢條 pipeline 可以完全唔需要手動 copy & paste 行完,就代表第一版 Multi-Agent Development Pipeline 已經成功。
20. 最終目標
Section titled “20. 最終目標”最終唔係:
Harry→ ChatGPT→ copy→ Claude→ copy→ Cursor→ copy→ Grok而係:
Harry ↓Requirement ↓AI Software Team ↓Pull Request ↓Harry Review即係由:
使用 AI 寫 Code
進化到:
管理一隊 AI Software Agents。