Skip to content

建立自動化 Multi-Agent Coding Workflow:讓 Codex、Claude Code、Cursor 與 Grok 自動協作

版本:2026年 10月 16日


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 Request
FAIL → 自動回到 Claude Code 修正

你只需要寫一次 Requirement,之後由 Orchestrator 自動將工作交俾唔同 Agent。


第一版建議唔好俾四個 Agent 同時改同一批 files,而係用 Sequential Pipeline。

主要工作:

  • 讀 Requirement
  • 分析 repository
  • 理解現有 architecture
  • 找出受影響 files
  • 拆 implementation steps
  • 定義 tests
  • 找出 edge cases / risks
  • 產生 plan.md

Codex 喺呢一步主要負責「諗」,唔改 code。


主要工作:

  • 讀 task.md
  • 讀 plan.md
  • 讀 AGENTS.md
  • Implement feature / bug fix
  • 修改 source code
  • 加 automated tests
  • Run build / tests
  • 修正 implementation errors

Claude Code 係第一版 workflow 入面嘅主要 coder。


主要工作:

  • 讀 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 最好唔參與原本 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 自己。


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 Review

例如 MarketLens:

MarketLens/
│
├── .ai/
│ ├── task.md
│ ├── plan.md
│ ├── implementation.md
│ ├── test-report.md
│ └── review.md
│
├── AGENTS.md
├── orchestrator.py
│
├── src/
├── tests/
└── ...

你日後主要需要寫呢個 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.

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:
pytest
ruff check .
mypy .
All tests must pass.
## Git Rules
- Work on a feature branch.
- Never push directly to main.
- Do not rewrite unrelated history.

第一版唔需要 CrewAI、LangGraph 或其他大型 framework。

一個簡單 Python script 已經可以控制四個 CLI Agent。

概念:

Python
↓
subprocess
↓
Codex CLI
Claude CLI
Cursor CLI
Grok CLI

from pathlib import Path
import subprocess
ROOT = Path(__file__).parent
AI_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.md
AGENTS.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.md
AGENTS.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.md
AGENTS.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)

真正開始變成 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)

當 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 次數。


初期唔建議 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 Approval

每一個 task 建議有自己 branch:

main
│
└── ai/123-single-ticker-scan

AI 只可以改:

ai/*

最後先由人 approve merge。


當 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
│
▼
Review

13. 唔好一開始 Parallel 改同一個 Folder

Section titled “13. 唔好一開始 Parallel 改同一個 Folder”

第一版避免:

Claude ─┐
Cursor ─┼── 同時修改 src/
Grok ───┘

因為容易:

  • overwrite changes
  • merge conflicts
  • tests互相影響
  • agent唔知道其他 agent 已改咗乜
  • debugging變得困難

所以第一版用:

Codex
↓
Claude
↓
Cursor
↓
Grok

會穩定好多。


之後可以將:

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 scanning

AI team 就自己開始做。


下一步可以將 CI errors 自動餵返 Agent。

例如:

Pull Request
↓
GitHub Actions
↓
pytest FAIL
↓
CI log
↓
Claude Code
↓
Fix
↓
Push
↓
CI rerun

形成:

Code
↓
CI
↓
FAIL
↓
AI Fix
↓
CI
↓
PASS

長遠可以變成:

GitHub Issue
│
▼
AI Orchestrator
│
▼
Architect
│
┌────────────┼────────────┐
│ │ │
▼ ▼ ▼
Backend Frontend Tests
Agent Agent Agent
│ │ │
└────────────┼────────────┘
│
▼
Integrator
│
▼
CI
│
┌───────┴───────┐
│ │
PASS FAIL
│ │
▼ ▼
Reviewer Developer
│ │
┌──────┴──────┐ │
│ │ │
PASS FAIL ◄────┘
│
▼
Pull Request
│
▼
Human Approval
│
▼
Merge

唔好一次過做到最複雜。

建議:

task.md
↓
Codex
↓
Claude
↓
Cursor
↓
Grok

目標:

四個 Agent 可以完全唔需要 copy & paste 自動接力。


加入:

Grok FAIL
↓
Claude Fix
↓
Cursor Test
↓
Grok Review

加入:

  • Structured JSON output
  • Retry limits
  • Error handling
  • Logs
  • Token / cost tracking
  • Run history

加入:

  • Git branches
  • Git worktrees
  • Parallel agents

加入:

GitHub Issue
↓
Agent Team
↓
Pull Request

加入:

CI FAIL
↓
AI Fix
↓
CI Retest

最後先考慮:

  • Auto merge
  • Auto deploy
  • Production changes

呢啲應該最後先開。


一定要有客觀 Definition of Done,例如:

Build passes
Tests pass
Lint passes
Type checking passes
Security review passes
Independent reviewer passes

Tests 係 Agentic Coding 最重要嘅 Guardrail

Section titled “Tests 係 Agentic Coding 最重要嘅 Guardrail”

越自動化,tests 越重要。

一個有良好 automated tests 嘅 repository:

普通 AI Model
+
Strong Tests

好多時會比:

最強 AI Model
+
No Tests

可靠。


唔需要每個 Agent 都用最強 model。

可以:

Codex
→ Architecture / Planning
Claude Code
→ Implementation
Cursor
→ Integration / Testing
Grok
→ Independent Review

最大價值唔係「四個 AI 一齊寫」。

而係:

四個 AI 有清楚角色、清楚輸入、清楚輸出,同埋客觀驗證流程。


可以用一個自己嘅 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 已經成功。


最終唔係:

Harry
→ ChatGPT
→ copy
→ Claude
→ copy
→ Cursor
→ copy
→ Grok

而係:

Harry
↓
Requirement
↓
AI Software Team
↓
Pull Request
↓
Harry Review

即係由:

使用 AI 寫 Code

進化到:

管理一隊 AI Software Agents。