n-version-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

N-Version Programming Workflow Skill

N-Version Programming 工作流Skill

Purpose

用途

Execute N-version programming workflow for critical implementations where multiple independent solutions should be generated and compared to select the best approach.
为关键实现执行N版本编程工作流,这类场景需要生成多个独立解决方案并对比,以选出最优方案。

When to Use This Skill

何时使用该Skill

USE FOR:
  • Critical security features (authentication, authorization)
  • Complex algorithms with multiple valid approaches
  • High-risk refactoring of core components
  • Architecture decisions with significant long-term impact
  • When correctness is paramount over speed
AVOID FOR:
  • Simple CRUD operations
  • Straightforward bug fixes
  • Documentation updates
  • Minor UI tweaks
  • Time-sensitive quick fixes
适用场景:
  • 关键安全特性(身份认证、授权)
  • 存在多种有效实现方式的复杂算法
  • 核心组件的高风险重构
  • 具有重大长期影响的架构决策
  • 正确性优先于开发速度的场景
避免场景:
  • 简单CRUD操作
  • 直接的Bug修复
  • 文档更新
  • 微小UI调整
  • 时间敏感的快速修复

Configuration

配置

Core Parameters

核心参数

N (Number of Versions):
  • 3
    - Default for standard tasks
  • 4-6
    - Critical features requiring high confidence
  • 2
    - Quick validation of approach
Selection Criteria (priority order):
  1. Correctness - Meets requirements and passes tests
  2. Security - No vulnerabilities or anti-patterns
  3. Simplicity - Ruthless simplicity, minimal complexity
  4. Philosophy Compliance - Follows project principles
  5. Performance - Efficiency and resource usage
Agent Diversity Profiles:
  • conservative
    - Proven patterns and safety
  • innovative
    - Novel approaches and optimizations
  • minimalist
    - Ruthless simplicity
  • pragmatic
    - Balance trade-offs for practical solutions
  • performance-focused
    - Optimize for speed and efficiency
N(版本数量):
  • 3
    - 标准任务默认值
  • 4-6
    - 需要高可信度的关键特性
  • 2
    - 快速验证实现思路
选择标准(优先级顺序):
  1. 正确性 - 满足需求并通过测试
  2. 安全性 - 无漏洞或反模式
  3. 简洁性 - 极致简洁,最小化复杂度
  4. 理念合规性 - 遵循项目原则
  5. 性能 - 效率与资源占用
Agent多样性配置文件:
  • conservative
    - 经验证的模式与安全优先
  • innovative
    - 新颖实现思路与优化
  • minimalist
    - 极致简洁
  • pragmatic
    - 平衡取舍的实用方案
  • performance-focused
    - 针对速度与效率优化

Execution Process

执行流程

Step 1: Prepare Common Context

步骤1:准备通用上下文

  • Use prompt-writer agent to create crystal-clear specification
  • Document all requirements explicitly
  • Define success criteria measurably
  • Prepare identical task specification for all N versions
  • Identify evaluation metrics upfront
  • CRITICAL: Capture explicit user requirements that CANNOT be optimized away
Output: Single authoritative specification document
  • 使用prompt-writer agent 创建清晰明确的规格说明
  • 明确记录所有需求
  • 可衡量地定义成功标准
  • 为所有N个版本准备相同的任务规格
  • 提前确定评估指标
  • 关键:记录不可被优化掉的明确用户需求
输出: 单一权威规格说明文档

Step 2: Generate N Independent Implementations

步骤2:生成N个独立实现

  • Spawn N Claude subprocesses simultaneously
  • Each subprocess receives IDENTICAL task specification
  • NO context sharing between subprocesses (true independence)
  • Each uses different agent diversity profile
  • Each produces complete implementation with tests
  • Each works in isolated directory (version_1/, version_2/, etc.)
Example for N=3:
  • Subprocess 1: Conservative approach (proven patterns, comprehensive error handling)
  • Subprocess 2: Pragmatic approach (balance simplicity and robustness)
  • Subprocess 3: Minimalist approach (ruthless simplification, minimal dependencies)
  • 同时启动N个Claude子进程
  • 每个子进程接收完全相同的任务规格
  • 子进程间无上下文共享(真正的独立性)
  • 每个子进程使用不同的Agent多样性配置文件
  • 每个子进程生成包含测试的完整实现
  • 每个子进程在独立目录中工作(version_1/、version_2/等)
N=3的示例:
  • 子进程1:保守型实现思路(经验证的模式、全面的错误处理)
  • 子进程2:实用型实现思路(平衡简洁性与健壮性)
  • 子进程3:极简型实现思路(极致简化、最小依赖)

Step 3: Collect and Compare Implementations

步骤3:收集并对比实现方案

  • Wait for all N implementations to complete
  • Use analyzer agent to examine each implementation
  • Use tester agent to run tests for each version
  • Document results in comparison matrix
Comparison Matrix:
| Version | Correctness | Security | Simplicity | Philosophy | Performance | Lines of Code |
|---------|-------------|----------|------------|------------|-------------|---------------|
| v1      | PASS        | PASS     | 7/10       | 8/10       | 150ms       | 180           |
| v2      | PASS        | PASS     | 9/10       | 9/10       | 180ms       | 95            |
| v3      | FAIL        | N/A      | 10/10      | 7/10       | N/A         | 65            |
  • 等待所有N个实现完成
  • 使用analyzer agent 检查每个实现方案
  • 使用tester agent 为每个版本运行测试
  • 在对比矩阵中记录结果
对比矩阵:
| Version | Correctness | Security | Simplicity | Philosophy | Performance | Lines of Code |
|---------|-------------|----------|------------|------------|-------------|---------------|
| v1      | PASS        | PASS     | 7/10       | 8/10       | 150ms       | 180           |
| v2      | PASS        | PASS     | 9/10       | 9/10       | 180ms       | 95            |
| v3      | FAIL        | N/A      | 10/10      | 7/10       | N/A         | 65            |

Step 4: Review and Evaluate

步骤4:评审与评估

  • Use reviewer agent for comprehensive comparison
  • Use security agent to evaluate security of each version
  • Apply selection criteria in priority order
  • Eliminate versions that fail correctness tests
  • Compare remaining versions on other criteria
  • Identify best parts of each implementation
Evaluation Process:
  1. Filter: Remove versions failing correctness tests
  2. Security Gate: Eliminate versions with security issues
  3. Philosophy Check: Score each on simplicity and compliance
  4. Performance Compare: Measure and compare benchmarks
  5. Synthesis: Identify if hybrid approach could be superior
  • 使用reviewer agent 进行全面对比
  • 使用security agent 评估每个版本的安全性
  • 按优先级顺序应用选择标准
  • 淘汰未通过正确性测试的版本
  • 对比剩余版本的其他标准
  • 识别每个实现方案的优势部分
评估流程:
  1. 筛选:移除未通过正确性测试的版本
  2. 安全关卡:淘汰存在安全问题的版本
  3. 理念检查:对简洁性与合规性打分
  4. 性能对比:测量并对比基准指标
  5. 综合:判断是否混合方案更优

Step 5: Select or Synthesize Solution

步骤5:选择或综合解决方案

Decision Tree:
  1. Is there ONE version that passes all criteria?
    • YES → Select it and document why
    • NO → Continue to step 2
  2. Are there 2+ versions tied on top criteria?
    • YES → Continue to step 3
    • NO → Select highest scoring version
  3. Do versions have complementary strengths?
    • YES → Synthesize hybrid combining best parts
    • NO → Select based on weighted criteria priority
Example Synthesis:
Selected: Hybrid of v1 and v2
- Core logic from v2 (ruthless simplicity)
- Error handling from v1 (comprehensive coverage)
- Testing approach from v2 (focused, minimal)
- Documentation style from v1 (thorough)

Rationale: v2's minimalist core paired with v1's robust
error handling provides optimal balance of simplicity
and production-readiness.
决策树:
  1. 是否存在一个通过所有标准的版本?
    • 是 → 选择该版本并记录原因
    • 否 → 进入步骤2
  2. 是否有2个及以上版本在最高优先级标准上持平?
    • 是 → 进入步骤3
    • 否 → 选择得分最高的版本
  3. 各版本是否具有互补优势?
    • 是 → 综合各版本的优势部分生成混合方案
    • 否 → 根据加权标准优先级选择
综合示例:
Selected: Hybrid of v1 and v2
- Core logic from v2 (ruthless simplicity)
- Error handling from v1 (comprehensive coverage)
- Testing approach from v2 (focused, minimal)
- Documentation style from v1 (thorough)

Rationale: v2's minimalist core paired with v1's robust
error handling provides optimal balance of simplicity
and production-readiness.

Step 6: Implement Selected Solution

步骤6:实现选定的解决方案

  • Use builder agent to implement final version
  • If single version selected: Use it directly
  • If synthesis: Implement hybrid combining best parts
  • Preserve all explicit user requirements from Step 1
  • Run full test suite
  • Document selection rationale in code comments
Documentation Template:
python
"""
N-Version Implementation Selection

Generated Versions: 3
Selection: Hybrid of v1 (conservative) and v2 (pragmatic)

Rationale:
- v1 had superior error handling and edge case coverage
- v2 had cleaner architecture and better testability
- v3 failed correctness tests (edge case handling)

This implementation combines v2's core logic with v1's
defensive programming approach for production robustness.

Selection Criteria Applied:
1. Correctness: v1=PASS, v2=PASS, v3=FAIL
2. Security: All passed
3. Simplicity: v2 ranked highest
4. Philosophy: v1 and v2 tied
5. Performance: Negligible difference
  • 使用builder agent 实现最终版本
  • 若选定单一版本:直接使用该版本
  • 若为综合方案:实现融合各优势部分的混合版本
  • 保留步骤1中记录的所有明确用户需求
  • 运行完整测试套件
  • 在代码注释中记录选择依据
文档模板:
python
"""
N-Version Implementation Selection

Generated Versions: 3
Selection: Hybrid of v1 (conservative) and v2 (pragmatic)

Rationale:
- v1 had superior error handling and edge case coverage
- v2 had cleaner architecture and better testability
- v3 failed correctness tests (edge case handling)

This implementation combines v2's core logic with v1's
defensive programming approach for production robustness.

Selection Criteria Applied:
1. Correctness: v1=PASS, v2=PASS, v3=FAIL
2. Security: All passed
3. Simplicity: v2 ranked highest
4. Philosophy: v1 and v2 tied
5. Performance: Negligible difference

Step 7: Document Learnings

步骤7:记录经验总结

  • Create analysis document:
    n_version_analysis.md
  • Document all N implementations generated
  • Explain selection rationale in detail
  • Capture insights from rejected versions
  • Store patterns learned in memory using
    store_discovery()
    from
    amplihack.memory.discoveries
  • Include comparison matrix for future reference
  • 创建分析文档:
    n_version_analysis.md
  • 记录所有生成的N个实现方案
  • 详细解释选择依据
  • 记录从被淘汰版本中获得的洞见
  • 使用
    amplihack.memory.discoveries
    中的
    store_discovery()
    将学到的模式存储到内存中
  • 包含对比矩阵以供未来参考

Trade-Offs

权衡

Cost: N times the compute resources and time Benefit: Significantly reduced risk of critical errors Best For: Features where bugs are expensive (security, data integrity)
成本: 计算资源与时间为常规方式的N倍 收益: 显著降低关键错误的风险 最佳适用场景: 漏洞代价高昂的特性(安全、数据完整性)

Examples

示例

Example 1: Authentication System

示例1:身份认证系统

Task: Implement JWT-based authentication Configuration: N=4 (critical security feature) Profiles: conservative, security-focused, pragmatic, minimalist
Result:
  • v1 (conservative): Most comprehensive but over-engineered
  • v2 (security-focused): Excellent security but complex
  • v3 (pragmatic): Good balance, missing edge cases
  • v4 (minimalist): Too simple, security gaps
Selection: Hybrid of v2 and v3
  • Security implementation from v2
  • API design and simplicity from v3
Rationale: Security cannot be compromised, but v3's cleaner API design improved usability without sacrificing security.
任务: 实现基于JWT的身份认证 配置: N=4(关键安全特性) 配置文件: conservative、security-focused、pragmatic、minimalist
结果:
  • v1(conservative):最全面但过度设计
  • v2(security-focused):安全性极佳但复杂度高
  • v3(pragmatic):平衡良好,但缺少边缘场景处理
  • v4(minimalist):过于简单,存在安全漏洞
选择: v2与v3的混合方案
  • 采用v2的安全实现
  • 采用v3的API设计与简洁性
依据: 安全不可妥协,但v3更简洁的API设计在不牺牲安全性的前提下提升了易用性。

Example 2: Data Processing Pipeline

示例2:数据处理管道

Task: Process large CSV files efficiently Configuration: N=3 (performance-critical) Profiles: pragmatic, performance-focused, minimalist
Result:
  • v1 (pragmatic): Pandas-based, familiar but slow
  • v2 (performance-focused): Custom streaming, 10x faster
  • v3 (minimalist): Python CSV module, simple but slow
Selection: v2 (performance-focused)
Rationale: Performance requirements justified complexity. v2's streaming approach met throughput requirements while v1 and v3 could not scale.
任务: 高效处理大型CSV文件 配置: N=3(性能关键) 配置文件: pragmatic、performance-focused、minimalist
结果:
  • v1(pragmatic):基于Pandas,易上手但速度慢
  • v2(performance-focused):自定义流处理,速度快10倍
  • v3(minimalist):基于Python CSV模块,简单但速度慢
选择: v2(performance-focused)
依据: 性能要求证明复杂度是合理的。v2的流处理方案满足吞吐量要求,而v1和v3无法扩展。

Philosophy Alignment

理念对齐

This workflow enforces:
  • Reduced Risk: Multiple implementations catch errors single approach might miss
  • Exploration: Different approaches reveal design trade-offs
  • Evidence-Based Selection: Systematic comparison vs. gut feeling
  • Learning: Rejected versions still provide valuable insights
  • Parallel Execution: N implementations run simultaneously for efficiency
该工作流强化了以下理念:
  • 降低风险: 多个实现方案可发现单一思路可能遗漏的错误
  • 探索性: 不同实现思路可揭示设计权衡
  • 基于证据的选择: 系统性对比而非凭直觉
  • 学习性: 被淘汰的版本仍能提供有价值的洞见
  • 并行执行: N个实现方案同时运行以提升效率

Integration with Default Workflow

与默认工作流的集成

This workflow replaces Steps 4-5 (Research/Design and Implementation) of the DEFAULT_WORKFLOW when enabled. All other steps (requirements, testing, CI/CD) remain the same.
启用该工作流时,将替代DEFAULT_WORKFLOW的步骤4-5(研究/设计与实现)。所有其他步骤(需求、测试、CI/CD)保持不变。