xiaohongshu
xiaohongshu
moai-alfred-dev-guide
**Enterprise SPEC-First TDD Development Orchestration**
Full skill instructions
Enterprise SPEC-First TDD Development Orchestration
Research Base: 7,549 code examples from 6 platforms Version: 4.0.0
Alfred's SPEC-First TDD workflow orchestrates the complete development lifecycle with three mandatory phases:
/alfred:1-plan): Requirements with EARS format/alfred:2-run): RED → GREEN → REFACTOR cycleCore Principle: No spec, no code. No tests, no implementation.
Key Capabilities:
Objective: Define clear, testable requirements before any code.
EARS Format (5 patterns):
Example SPEC Document:
# .moai/specs/SPEC-001/spec.md
---
id: SPEC-001
title: User Authentication System
status: approved
---
## Requirements
### SPEC-001-REQ-01: Login Validation
**Pattern**: Event-driven
**Statement**: WHEN a user submits login credentials, the system SHALL validate against the user database within 200ms.
**Acceptance Criteria**:
- Username and password must be non-empty
- Password must be hashed before comparison
- Failed attempts must be logged
- Success returns JWT token with 1-hour expiry
**Priority**: High
**Risk**: Medium (security-critical)
Objective: Write tests that fail because implementation doesn't exist yet.
Pytest Example:
# tests/test_auth.py
import pytest
from app.auth import AuthService
@pytest.fixture
def auth_service(tmp_path):
"""Create test authentication service with temporary database."""
db_path = tmp_path / "test_auth.db"
service = AuthService(db_path=db_path)
service.initialize() # Create tables
yield service
service.close() # Cleanup
def test_login_with_valid_credentials(auth_service):
# RED: This test will fail because login() doesn't exist
auth_service.create_user("alice", "secure_password_123")
token = auth_service.login("alice", "secure_password_123")
assert token is not None
assert token.startswith("eyJ") # JWT format
assert auth_service.is_token_valid(token) is True
def test_login_with_invalid_password(auth_service):
auth_service.create_user("bob", "correct_password")
with pytest.raises(AuthenticationError) as exc_info:
auth_service.login("bob", "wrong_password")
assert "Invalid credentials" in str(exc_info.value)
assert exc_info.value.code == "AUTH_FAILED"
Run RED Phase:
$ pytest tests/test_auth.py -v
============================= test session starts =============================
collected 2 items
tests/test_auth.py::test_login_with_valid_credentials FAILED [ 50%]
tests/test_auth.py::test_login_with_invalid_password FAILED [100%]
============================== 2 failed in 0.15s ==============================
Objective: Write just enough code to make tests pass.
# app/auth.py
import sqlite3
import hashlib
import jwt
import datetime
from typing import Optional
class AuthenticationError(Exception):
def __init__(self, message: str, code: str = "AUTH_ERROR"):
super().__init__(message)
self.code = code
class AuthService:
"""JWT-based authentication service."""
def __init__(self, db_path: str, secret_key: str = "test-secret"):
self.db_path = db_path
self.secret_key = secret_key
self.conn = None
def initialize(self):
self.conn = sqlite3.connect(self.db_path)
cursor = self.conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
self.conn.commit()
def create_user(self, username: str, password: str):
password_hash = hashlib.sha256(password.encode()).hexdigest()
cursor = self.conn.cursor()
cursor.execute(
"INSERT INTO users (username, password_hash) VALUES (?, ?)",
(username, password_hash)
)
self.conn.commit()
def login(self, username: str, password: str) -> str:
"""Authenticate user and generate JWT token."""
# Input validation
if not username:
raise ValueError("Username cannot be empty")
if not password:
raise ValueError("Password cannot be empty")
# Hash password and check against database
password_hash = hashlib.sha256(password.encode()).hexdigest()
cursor = self.conn.cursor()
cursor.execute(
"SELECT id FROM users WHERE username = ? AND password_hash = ?",
(username, password_hash)
)
user = cursor.fetchone()
if user is None:
raise AuthenticationError("Invalid credentials", code="AUTH_FAILED")
# Generate JWT token
payload = {
"user_id": user[0],
"username": username,
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1)
}
token = jwt.encode(payload, self.secret_key, algorithm="HS256")
return token
def is_token_valid(self, token: str) -> bool:
try:
jwt.decode(token, self.secret_key, algorithms=["HS256"])
return True
except jwt.ExpiredSignatureError:
return False
except jwt.InvalidTokenError:
return False
def close(self):
if self.conn:
self.conn.close()
Objective: Improve code quality without changing behavior.
Before (GREEN phase - minimal):
def login(self, username: str, password: str) -> str:
if not username:
raise ValueError("Username cannot be empty")
if not password:
raise ValueError("Password cannot be empty")
password_hash = hashlib.sha256(password.encode()).hexdigest()
cursor = self.conn.cursor()
cursor.execute(
"SELECT id FROM users WHERE username = ? AND password_hash = ?",
(username, password_hash)
)
user = cursor.fetchone()
if user is None:
raise AuthenticationError("Invalid credentials", code="AUTH_FAILED")
payload = {
"user_id": user[0],
"username": username,
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1)
}
token = jwt.encode(payload, self.secret_key, algorithm="HS256")
return token
After (REFACTOR phase - improved):
def login(self, username: str, password: str) -> str:
"""Authenticate user and generate JWT token.
Args:
username: User's login name (1-255 characters)
password: Plain-text password (will be hashed)
Returns:
JWT token string valid for 1 hour
Raises:
ValidationError: If input validation fails
AuthenticationError: If credentials are invalid
"""
self._validate_login_inputs(username, password)
user_id = self._authenticate_user(username, password)
return self._generate_token(user_id, username)
def _validate_login_inputs(self, username: str, password: str) -> None:
"""Validate login input parameters."""
if not username:
raise ValidationError("Username cannot be empty")
if not password:
raise ValidationError("Password cannot be empty")
if len(username) > 255:
raise ValidationError("Username too long (max 255 characters)")
def _authenticate_user(self, username: str, password: str) -> int:
"""Authenticate user against database."""
password_hash = hashlib.sha256(password.encode()).hexdigest()
cursor = self.conn.cursor()
cursor.execute(
"SELECT id FROM users WHERE username = ? AND password_hash = ?",
(username, password_hash)
)
user = cursor.fetchone()
if user is None:
raise AuthenticationError("Invalid credentials", code="AUTH_FAILED")
return user[0]
def _generate_token(self, user_id: int, username: str) -> str:
"""Generate JWT token with 1-hour expiry."""
payload = {
"user_id": user_id,
"username": username,
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1),
"iat": datetime.datetime.utcnow()
}
return jwt.encode(payload, self.secret_key, algorithm="HS256")
TRUST 5 Framework:
T: Test-Driven Example:
# ✅ CORRECT: Test-first approach
# Step 1: Write failing test (RED)
def test_delete_user_removes_from_database(auth_service, user_factory):
user_id = user_factory("alice", "password")
auth_service.delete_user(user_id)
# Verify user no longer exists
with pytest.raises(UserNotFoundError):
auth_service.get_user(user_id)
# Step 2: Implement minimal code (GREEN)
def delete_user(self, user_id: int):
cursor = self.conn.cursor()
cursor.execute("DELETE FROM users WHERE id = ?", (user_id,))
self.conn.commit()
# Step 3: Refactor (maintain TRUST-R: Readable)
def delete_user(self, user_id: int) -> None:
"""Delete user and all associated data.
Implements GDPR right to erasure (SPEC-005).
Args:
user_id: Database user ID to delete
Raises:
UserNotFoundError: If user_id doesn't exist
"""
if not self._user_exists(user_id):
raise UserNotFoundError(f"User {user_id} not found")
self._delete_user_sessions(user_id)
self._delete_user_data(user_id)
self._delete_user_record(user_id)
Alfred coordinates specialized agents across the TDD workflow:
Agent Delegation Matrix:
| Phase | Primary Agent | Supporting Agents | Task |
|---|---|---|---|
| SPEC | spec-builder | plan-agent, doc-syncer | Requirements engineering |
| RED | tdd-implementer | test-engineer | Failing test creation |
| GREEN | tdd-implementer | backend-expert, frontend-expert | Minimal implementation |
| REFACTOR | tdd-implementer | format-expert, database-expert | Code quality improvement |
| SYNC | doc-syncer | tag-agent, git-manager | Documentation & validation |
Example: Multi-Agent Workflow:
# Alfred's orchestration logic
async def run_tdd_cycle(spec_id: str):
"""Orchestrate complete TDD cycle with agent delegation."""
# Phase 1: Plan (plan-agent)
plan = await delegate_to_agent(
agent_type="plan-agent",
prompt=f"Analyze SPEC-{spec_id} and create implementation plan"
)
# Phase 2: RED (test-engineer)
tests = await delegate_to_agent(
agent_type="test-engineer",
prompt=f"Write failing tests for SPEC-{spec_id} based on plan: {plan}"
)
# Phase 3: GREEN (backend-expert + tdd-implementer)
implementation = await delegate_to_agent(
agent_type="tdd-implementer",
prompt=f"Implement {spec_id} to pass tests: {tests}"
)
# Phase 4: REFACTOR (format-expert)
refactored = await delegate_to_agent(
agent_type="format-expert",
prompt=f"Refactor code for TRUST 5 compliance: {implementation}"
)
# Phase 5: SYNC (doc-syncer + git-manager)
await delegate_to_agent(
agent_type="doc-syncer",
prompt=f"Generate documentation for {spec_id}"
)
GitHub Actions Workflow:
# .github/workflows/alfred-tdd.yml
name: Alfred TDD Pipeline
on:
push:
branches: [ feature/* ]
pull_request:
branches: [ develop, main ]
jobs:
test-red-green-refactor:
name: "TDD Cycle Verification"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install pytest pytest-cov mypy ruff
- name: Run tests (GREEN phase check)
run: |
pytest tests/ -v --cov=src --cov-report=term-missing --cov-fail-under=85
- name: Type checking (TRUST-R: Readable)
run: |
mypy src/ --strict
- name: Linting (TRUST-U: Unified)
run: |
ruff check src/ tests/
security-scan:
name: "Security Audit (TRUST-S)"
runs-on: ubuntu-latest
needs: test-red-green-refactor
steps:
- uses: actions/checkout@v3
- name: OWASP dependency check
run: |
pip install safety
safety check --json
- name: Bandit security linter
run: |
pip install bandit
bandit -r src/ -f json -o bandit-report.json
- name: Fail on HIGH severity
run: |
if grep -q '"issue_severity": "HIGH"' bandit-report.json; then
echo "❌ HIGH severity security issues found"
exit 1
fi
Step-by-Step Execution:
# ===== PHASE 1: SPEC (/alfred:1-plan) =====
$ /alfred:1-plan "User Authentication System"
Alfred creates:
✅ feature/SPEC-001 branch
✅ .moai/specs/SPEC-001/spec.md (EARS format)
✅ TodoWrite task list
# ===== PHASE 2: TDD (/alfred:2-run) =====
$ /alfred:2-run SPEC-001
# --- RED Commit ---
Alfred (tdd-implementer agent):
1. Reads SPEC-001/spec.md
2. Writes failing tests
3. Runs pytest → 2 failed ❌
4. Git commit: "test(SPEC-001): Add failing tests for authentication"
# --- GREEN Commit ---
Alfred (tdd-implementer agent):
1. Minimal implementation to pass tests
2. Runs pytest → 2 passed ✅
3. Git commit: "feat(SPEC-001): Implement basic authentication"
# --- REFACTOR Commit ---
Alfred (tdd-implementer agent):
1. Extracts methods, improves docstrings
2. Adds type hints, security hardening
3. Runs pytest → 2 passed ✅ (no behavior change)
4. Git commit: "refactor(SPEC-001): Improve code quality and documentation"
# ===== PHASE 3: SYNC (/alfred:3-sync) =====
$ /alfred:3-sync auto SPEC-001
Alfred (doc-syncer agent):
1. Generates Sphinx documentation
2. Runs quality gates:
- Coverage: 93% (≥85% ✅)
- Type check: Pass ✅
- Linting: Pass ✅
- Security: No HIGH issues ✅
3. Creates sync report
4. Git commit: "docs(SPEC-001): Add API documentation and sync report"
5. Creates PR to develop branch
# Coverage ≥85% requirement
$ pytest --cov=src --cov-report=term-missing
Name Stmts Miss Cover Missing
------------------------------------------------------
src/auth.py 85 8 91% 145-152
src/database.py 42 0 100%
src/validation.py 28 3 89% 67-69
------------------------------------------------------
TOTAL 155 11 93%
✅ TRUST-E: Coverage 93% (≥85% required)
Required Checks (10/10):
Quality Metrics:
This skill is built on 7,549 production code examples from:
Research date: 2025-11-12
Version: 4.0.0
Last Updated: 2025-11-12
Maintained By: Alfred SuperAgent (MoAI-ADK)
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience