ar-01:代码优化(fix 模式)
场景
使用 codex-autoresearch 的 fix 工作流模式,自动定位并修复 Python 代码缺陷,同时扩展测试覆盖。
任务
给定 Python math_utils 模块,其中包含 5 个已知缺陷:除零无保护、空列表无守卫、阶乘负数未检查、最大值为空列表崩溃、回文判断未清理空白。初始测试覆盖率约 40%。
Prompt
-autoresearch Mode: fix
Goal: Fix all bugs in math_utils.py and expand test coverage to at least 90%.
Metric: pytest failure count
Direction: lower
Verify: python -m pytest test_math_utils.py -v --cov=math_utils
Iterations: 10
关键输出与验证
修复后的 math_utils.py
"""math_utils.py - A utility module with bugs fixed and edge cases handled."""
import math
def divide(a: float, b: float) -> float:
"""Divide a by b. Raises ZeroDivisionError if b is zero."""
if b == 0:
raise ZeroDivisionError("Cannot divide by zero")
return a / b
def average(numbers: list) -> float:
"""Return average of numbers. Raises ValueError for empty list."""
if not numbers:
raise ValueError("Cannot compute average of empty list")
return sum(numbers) / len(numbers)
def factorial(n: int) -> int:
"""Return n!. Raises ValueError for negative n. Uses math.factorial for safety."""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
return math.factorial(n)
def find_max(numbers: list) -> float:
"""Return max value. Raises ValueError for empty list."""
if not numbers:
raise ValueError("Cannot find max of empty list")
return max(numbers)
def is_palindrome(s: str) -> bool:
"""Check if string is palindrome. Case-insensitive, ignores whitespace."""
cleaned = s.replace(" ", "").lower()
return cleaned == cleaned[::-1]
测试结果
collected 18 items
test_math_utils.py .................. 18 passed in 0.12s
Coverage: 95%
判定
PASS — 5 个缺陷全部修复,测试覆盖率从 40% 提升至 95%,18 个测试用例全部通过。修复过程遵循"每次迭代一个变更"原则,每步均有验证。