Moai workflow ddd

From modu-ai/moai-adk · View interactive version

Moai workflow ddd is an AI agent skill from the GitHub repository modu-ai/moai-adk. Domain-Driven Development workflow specialist using ANALYZE-PRESERVE-IMPROVE cycle for behavior-preserving code transformation. Use when refactoring legacy code or reducing technical debt safely.

How to use this skill

Skill file

Domain-Driven Development (DDD) Workflow

Development Mode Configuration (CRITICAL)

[NOTE] This workflow is selected based on .moai/config/sections/quality.yaml:

constitution:
  development_mode: ddd    # or tdd

When to use this workflow:

Key distinction:

Quick Reference

Domain-Driven Development provides a systematic approach for refactoring existing codebases where behavior preservation is paramount. Unlike TDD which creates new functionality, DDD improves structure without changing behavior.

Core Cycle - ANALYZE-PRESERVE-IMPROVE:

When to Use DDD:

When NOT to Use DDD:

Greenfield Project Adaptation:

For new projects without existing code, DDD adapts its cycle:

This makes DDD a superset of TDD - it includes TDD's test-first approach while also supporting refactoring scenarios.


Core Philosophy

DDD vs TDD Comparison

TDD Approach (for new features):

DDD Approach (for refactoring):

Behavior Preservation Principle

The golden rule of DDD is that observable behavior must remain identical before and after refactoring. This means:


Implementation Guide

Phase 1: ANALYZE

The analyze phase focuses on understanding the current codebase structure and identifying refactoring opportunities.

Domain Boundary Identification

Identify logical boundaries in the codebase by examining:

Use AST-grep to analyze structural patterns. For Python, search for import patterns to understand module dependencies. For class hierarchies, analyze inheritance relationships and method distributions.

Coupling and Cohesion Metrics

Evaluate code quality metrics:

Low cohesion and high coupling indicate refactoring candidates.

Structural Analysis Patterns

Use AST-grep to identify problematic patterns:

Create analysis reports documenting:

Phase 2: PRESERVE

The preserve phase establishes safety nets before making any changes.

Characterization Tests

Characterization tests capture existing behavior without assumptions about correctness. The goal is to document what the code actually does, not what it should do.

Steps for creating characterization tests:

Characterization test naming convention: testcharacterize[component]\_[scenario]

Behavior Snapshots

For complex outputs, use snapshot testing to capture current behavior:

Snapshot files serve as behavior contracts during refactoring.

Test Safety Net Verification

Before proceeding to improvement phase, verify:

Run mutation testing if available to verify test effectiveness.

Phase 3: IMPROVE

The improve phase makes structural changes while continuously validating behavior preservation.

Incremental Transformation Strategy

Never make large changes at once. Follow this pattern:

Safe Refactoring Patterns

Extract Method: When a code block can be named and isolated. Use AST-grep to identify candidates by searching for repeated code blocks or long methods.

Extract Class: When a class has multiple responsibilities. Move related methods and fields to a new class while maintaining the original API through delegation.

Move Method: When a method uses data from another class more than its own. Relocate while preserving all call sites.

Inline Refactoring: When indirection adds complexity without benefit. Replace delegation with direct implementation.

Rename Refactoring: When names do not reflect current understanding. Update all references atomically using AST-grep rewrite.

AST-Grep Assisted Transformations

Use AST-grep for safe, semantic-aware transformations:

For method extraction, create a rule that identifies the code pattern and rewrites to the extracted form.

For API migration, create a rule that matches old API calls and rewrites to new API format.

For deprecation handling, create rules that identify deprecated patterns and suggest modern alternatives.

Continuous Validation Loop

After each transformation:


DDD Workflow Execution

Standard DDD Session

When executing DDD through moai:2-run in DDD mode:

Step 1 - Initial Assessment:

Step 2 - Analyze Phase Execution:

Step 3 - Preserve Phase Execution:

Step 4 - Improve Phase Execution:

Step 5 - Validation and Completion:

DDD Loop Pattern

For complex refactoring requiring multiple iterations:


Quality Metrics

DDD Success Criteria

Behavior Preservation (Required):

Structure Improvement (Goals):

DDD-Specific TRUST Validation

Apply TRUST 5 framework with DDD focus:


Integration Points

With AST-Grep Skill

DDD relies heavily on AST-grep for:

Rules live in .moai/config/astgrep-rules/; scan with moai ast-grep and apply rewrites with moai ast-edit.

With Testing Workflow

DDD complements testing workflow:

With Quality Framework

DDD outputs feed into quality assessment:


Troubleshooting

Common Issues

Tests Fail After Transformation:

Characterization Tests Are Flaky:

Performance Degradation:

Recovery Procedures

When DDD session encounters issues:


Version: 1.0.0

Status: Active

<!-- moai:evolvable-start id="rationalizations" -->

Common Rationalizations

| Rationalization | Reality |

|---|---|

| "This legacy code is obviously broken, I will just rewrite it" | The broken-looking code may encode production behavior callers depend on. ANALYZE first, rewrite with tests. |

| "Characterization tests are just busywork" | They are the safety net that lets you refactor without fear. Without them, refactor equals rewrite. |

| "I understand this module well enough to skip ANALYZE" | Understanding is not verification. ANALYZE forces you to name assumptions and confront callers. |

| "This function has no tests because it has no behavior worth testing" | If it has callers, it has contract. The contract is the thing to characterize. |

| "I will preserve behavior by being careful" | Carefulness is not a mechanism. Tests are the only mechanism that catches regressions. |

| "This refactor is too small to need DDD" | Small refactors cause silent regressions exactly because people skip the safety net. |

Chesterton's Fence: Do not remove or replace code whose purpose you cannot explain. ANALYZE phase exists specifically to uncover the reasoning behind code that looks wrong.

<!-- moai:evolvable-end -->

<!-- moai:evolvable-start id="red-flags" -->

Red Flags

<!-- moai:evolvable-end -->

<!-- moai:evolvable-start id="verification" -->

Verification

<!-- moai:evolvable-end -->

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.