iterative-refinement

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Iterative Refinement

迭代优化

Execute workflows iteratively with systematic validation, progress tracking, and intelligent termination.
通过系统化验证、进度跟踪和智能终止机制,以迭代方式执行工作流。

When to Use

适用场景

Use for tasks requiring iterative refinement:
  • Test-fix-validate cycles: Fix failures → retest → repeat until passing
  • Code quality improvement: Review → fix → review until standards met
  • Performance optimization: Profile → optimize → measure until targets achieved
  • Progressive enhancement: Iterative improvements until diminishing returns
Don't use for single-pass tasks, purely parallel work, or simple linear workflows.
适用于需要迭代优化的任务:
  • 测试-修复-验证循环:修复问题 → 重新测试 → 重复直至全部通过
  • 代码质量提升:评审 → 修复 → 评审直至符合标准
  • 性能优化:分析 → 优化 → 度量直至达成目标
  • 渐进式增强:持续迭代改进直至收益递减
不适用于单次完成的任务、纯并行工作或简单线性工作流。

Pre-Usage Research (Optional)

预使用调研(可选)

Before starting iterations, consider researching:
  • Current best practices for your validation tools (search "[tool] best practices 2025")
  • Known issues with your tech stack (search "[language] [tool] common issues")
  • Optimal configuration for your validators (search "[tool] configuration production")
  • Recent improvements or alternatives (search "[tool] vs alternatives 2025")
Benefits:
  • Better validators from the start
  • Avoid known issues
  • Use current best practices
  • Save iteration cycles
When to research first:
  • Unfamiliar validation tools
  • New tech stack
  • Complex quality criteria
  • High-stakes optimization
开始迭代前,可考虑调研:
  • 验证工具的当前最佳实践(搜索 "[tool] best practices 2025")
  • 技术栈的已知问题(搜索 "[language] [tool] common issues")
  • 验证工具的最优配置(搜索 "[tool] configuration production")
  • 近期改进或替代方案(搜索 "[tool] vs alternatives 2025")
调研收益:
  • 从一开始就使用更优质的验证工具
  • 规避已知问题
  • 采用当前最佳实践
  • 减少迭代次数
优先调研的场景:
  • 不熟悉的验证工具
  • 全新技术栈
  • 复杂的质量标准
  • 高风险优化任务

Core Loop Pattern

核心循环模式

Every iteration follows:
  1. Execute action (fix, optimize, improve)
  2. Validate result (test, measure, check)
  3. Assess progress (compare to criteria)
  4. Decide (continue or stop)
每次迭代遵循以下步骤:
  1. 执行操作(修复、优化、改进)
  2. 验证结果(测试、度量、检查)
  3. 评估进度(与标准对比)
  4. 决策(继续或终止)

Instructions

操作步骤

Step 1: Define Configuration

步骤1:定义配置

Establish before starting:
Success Criteria (specific and measurable):
  • Criterion 1: [Example: "All 50 tests passing"]
  • Criterion 2: [Example: "Zero linter warnings"]
  • Criterion 3: [Example: "Response time < 100ms"]
Loop Limits:
  • Max iterations: 5-15 (justify if >20)
  • Min iterations: (optional)
Termination Mode:
  • Fixed: Run exactly N iterations
  • Criteria: Stop when success criteria met
  • Convergence: Stop when improvements < threshold (e.g., <10% over 3 iterations)
  • Hybrid: Combine multiple conditions
开始前需确定:
成功标准(具体可衡量):
  • 标准1:[示例:"全部50项测试通过"]
  • 标准2:[示例:"无代码检查工具警告"]
  • 标准3:[示例:"响应时间 < 100ms"]
循环限制
  • 最大迭代次数:5-15次(超过20次需说明理由)
  • 最小迭代次数:(可选)
终止模式
  • 固定次数:恰好执行N次迭代
  • 达标终止:满足成功标准时停止
  • 收敛终止:连续3次迭代改进幅度<阈值(如<10%)时停止
  • 混合模式:结合多种终止条件

Step 2: Execute Iteration

步骤2:执行迭代

For each iteration:
  1. Take action - Apply fixes or implement changes
  2. Run validator - Execute tests, linters, or measurements
  3. Record progress:
    Iteration N:
    - Action: [what was done]
    - Results: [metrics/outcomes]
    - Issues remaining: [count/description]
    - Decision: [Continue/Success/Stop]
  4. Assess termination:
    • All criteria met? → SUCCESS
    • Improvement < threshold? → CONVERGED
    • Reached max iterations? → STOP
    • Otherwise → CONTINUE
每次迭代流程:
  1. 执行操作 - 修复问题或实施改进
  2. 运行验证 - 执行测试、代码检查或度量
  3. 记录进度
    第N次迭代:
    - 操作:[执行的内容]
    - 结果:[指标/产出]
    - 剩余问题:[数量/描述]
    - 决策:[继续/成功/终止]
  4. 评估终止条件
    • 满足所有标准?→ 成功
    • 改进幅度<阈值?→ 已收敛
    • 达到最大迭代次数?→ 终止
    • 其他情况 → 继续

Step 3: Pass Context Between Iterations

步骤3:迭代间传递上下文

Each iteration needs:
  • Previous results
  • Current metrics
  • Remaining issues
  • Progress trend
This prevents repeating failed approaches.
每次迭代需获取:
  • 上一次的结果
  • 当前指标
  • 剩余问题
  • 进度趋势
避免重复已失败的方案。

Step 4: Handle Stuck States

步骤4:处理停滞状态

If no progress for 2-3 iterations:
  1. Analyze why progress stopped
  2. Try different approach
  3. Consider manual intervention
  4. Stop if truly stuck
若连续2-3次迭代无进展:
  1. 分析停滞原因
  2. 尝试不同方案
  3. 考虑人工干预
  4. 确认无法推进则终止

Step 5: Report Results

步骤5:结果报告

Loop Summary:
- Iterations: N
- Termination: [Success/Converged/Max/Stuck]
- Initial state: [metrics]
- Final state: [metrics]
- Improvement: [percentage/delta]
- Remaining issues: [list if any]
循环总结:
- 迭代次数:N
- 终止原因:[成功/收敛/达到最大次数/停滞]
- 初始状态:[指标]
- 最终状态:[指标]
- 改进幅度:[百分比/差值]
- 剩余问题:[列表(如有)]

Validation Best Practices

验证最佳实践

Make Validators Specific

验证标准需具体

Bad: "Check if code is better" Good: "Run linter and count warnings"
Bad: "See if it's faster" Good: "Run benchmark: average response time over 100 requests"
错误示例:"检查代码是否更优" 正确示例:"运行代码检查工具并统计警告数量"
错误示例:"看看是否更快" 正确示例:"运行基准测试:100次请求的平均响应时间"

Use Automated Validation

使用自动化验证

Prefer scripts/tools over manual inspection:
  • Test frameworks over reading test code
  • Linters over manual code review
  • Benchmarks over estimated performance
  • Coverage tools over counting tests
优先使用脚本/工具而非人工检查:
  • 测试框架而非人工阅读测试代码
  • 代码检查工具而非人工代码评审
  • 基准测试而非估算性能
  • 覆盖率工具而非人工统计测试数量

Capture Concrete Metrics

追踪具体指标

Track measurable progress:
  • Test pass rate: 42/50 → 48/50 → 50/50
  • Warning count: 23 → 8 → 2 → 0
  • Response time: 320ms → 180ms → 95ms → 48ms
  • Code coverage: 65% → 78% → 85% → 92%
记录可衡量的进度:
  • 测试通过率:42/50 → 48/50 → 50/50
  • 警告数量:23 → 8 → 2 → 0
  • 响应时间:320ms → 180ms → 95ms → 48ms
  • 代码覆盖率:65% → 78% → 85% → 92%

Examples

示例

Example 1: Test Fixing

示例1:测试修复

Task: Fix all failing tests
Configuration:
  • Success: 100% tests passing
  • Max iterations: 8
Execution:
I1: 42/50 → Fix 8 failures → Continue
I2: 48/50 → Fix 2 failures → Continue
I3: 50/50 → SUCCESS ✓
任务:修复所有失败的测试
配置:
  • 成功标准:100%测试通过
  • 最大迭代次数:8
执行过程:
I1: 42/50 → 修复8个失败项 → 继续
I2: 48/50 → 修复2个失败项 → 继续
I3: 50/50 → 成功 ✓

Example 2: Linter Cleanup

示例2:代码检查清理

Task: Remove all linter warnings
Configuration:
  • Success: 0 warnings
  • Max iterations: 5
Execution:
I1: 15 warnings → Fix → 6 warnings
I2: 6 warnings → Fix → 1 warning
I3: 1 warning → Fix → 0 warnings ✓
任务:清除所有代码检查警告
配置:
  • 成功标准:0个警告
  • 最大迭代次数:5
执行过程:
I1: 15个警告 → 修复 → 6个警告
I2: 6个警告 → 修复 → 1个警告
I3: 1个警告 → 修复 → 0个警告 ✓

Example 3: Performance Loop

示例3:性能优化循环

Task: Optimize response time
Configuration:
  • Success: <50ms OR converged
  • Max iterations: 15
  • Convergence: <10% over 3 iterations
Execution:
I1: 320ms → Optimize → 180ms (44%)
I2: 180ms → Optimize → 95ms (47%)
I3: 95ms → Optimize → 48ms (49%)
SUCCESS (target met)
任务:优化响应时间
配置:
  • 成功标准:<50ms 或已收敛
  • 最大迭代次数:15
  • 收敛条件:连续3次迭代改进幅度<10%
执行过程:
I1: 320ms → 优化 → 180ms(提升44%)
I2: 180ms → 优化 → 95ms(提升47%)
I3: 95ms → 优化 → 48ms(提升49%)
成功(达标)

Example 4: Coverage Improvement

示例4:覆盖率提升

Task: Increase test coverage to 90%
Configuration:
  • Success: Coverage ≥ 90%
  • Max iterations: 12
Execution:
I1: 65% → Write tests → 72%
I2: 72% → Write tests → 81%
I3: 81% → Write tests → 88%
I4: 88% → Write tests → 91% ✓
任务:将测试覆盖率提升至90%
配置:
  • 成功标准:覆盖率≥90%
  • 最大迭代次数:12
执行过程:
I1: 65% → 编写测试 → 72%
I2: 72% → 编写测试 → 81%
I3: 81% → 编写测试 → 88%
I4: 88% → 编写测试 → 91% ✓

Language-Specific Tools

各语言专用工具

For validation tools and commands for your language:
  • Python: See tools/python.md
  • JavaScript/TypeScript: See tools/javascript.md
  • Rust: See tools/rust.md
  • Java: See tools/java.md
  • Go: See tools/go.md
  • C/C++: See tools/cpp.md
  • Ruby: See tools/ruby.md
  • PHP: See tools/php.md
  • C#/.NET: See tools/dotnet.md
各语言的验证工具及命令参考:
  • Python: See tools/python.md
  • JavaScript/TypeScript: See tools/javascript.md
  • Rust: See tools/rust.md
  • Java: See tools/java.md
  • Go: See tools/go.md
  • C/C++: See tools/cpp.md
  • Ruby: See tools/ruby.md
  • PHP: See tools/php.md
  • C#/.NET: See tools/dotnet.md

Advanced Usage

高级用法

For complex workflows, convergence detection, and advanced patterns: See patterns.md
复杂工作流、收敛检测及高级模式参考: See patterns.md

Best Practices

最佳实践

DO:

建议:

✓ Define clear, measurable success criteria ✓ Set reasonable max limits (5-15) ✓ Use automated validators ✓ Pass context between iterations ✓ Track concrete metrics ✓ Stop early when criteria met ✓ Detect convergence ✓ Document changes
✓ 定义清晰、可衡量的成功标准 ✓ 设置合理的最大迭代次数(5-15次) ✓ 使用自动化验证工具 ✓ 在迭代间传递上下文 ✓ 追踪具体指标 ✓ 达标后尽早终止 ✓ 检测收敛状态 ✓ 记录变更内容

DON'T:

禁忌:

✗ Use loops for single-pass tasks ✗ Set high limits (>20) without justification ✗ Skip validation between iterations ✗ Lose context between iterations ✗ Continue after success/convergence ✗ Ignore stuck signals ✗ Use vague criteria ✗ Miss early termination
✗ 对单次任务使用循环 ✗ 无理由设置高迭代次数(>20次) ✗ 跳过迭代间的验证步骤 ✗ 丢失迭代间的上下文 ✓ 达标/收敛后仍继续迭代 ✗ 忽略停滞信号 ✗ 使用模糊的标准 ✗ 错过提前终止时机