首页 / Skills / 开发提效 / 精准修复 Surgical Patch
开发提效

精准修复 Surgical Patch

Julius BrusseeApache-2.0Skill 语言: 英文
协议已核对不含脚本
演示真实运行记录 · claude-opus-5-5 · 2026年10月
Bug: `paginate([1,2,3,4,5], page=2, size=2)` returns `[4, 5]` but should return `[3, 4]`. Page numbers are 1-based. The files are not in this workspace, so give me the fix as a diff and I will apply it. ```python # utils/pagination.py def paginate(items, page, size): """Return the items on a 1-based page.""" if size <= 0: raise ValueError("size must be positive") start = page * size end = start + size return items[start:end] def page_count(items, size): return (len(items) + size - 1) // size ``` ```python # tests/test_pagination.py from utils.pagination import paginate, page_count def test_page_count(): assert page_count([1, 2, 3, 4, 5], 2) == 3 ```
Skill: surgical-patch

The bug is in how the start index is calculated. start = page * size treats page as 0-based, so page 2 starts at index 4 and returns [4, 5]. For a 1-based page, the start index needs to be (page - 1) * size.

--- a/utils/pagination.py
+++ b/utils/pagination.py
@@ -2,7 +2,7 @@ def paginate(items, page, size):
     """Return the items on a 1-based page."""
     if size <= 0:
         raise ValueError("size must be positive")
-    start = page * size
+    start = (page - 1) * size
     end = start + size
     return items[start:end]
 
--- a/tests/test_pagination.py
+++ b/tests/test_pagination.py
@@ -3,2 +3,10 @@ from utils.pagination import paginate, page_count
 def test_page_count():
     assert page_count([1, 2, 3, 4, 5], 2) == 3
+
+
+def test_paginate_is_one_based():
+    items = [1, 2, 3, 4, 5]
+    assert paginate(items, page=1, size=2) == [1, 2]
+    assert paginate(items, page=2, size=2) == [3, 4]
+    assert paginate(items, page=3, size=2) == [5]
+    assert paginate(items, page=4, size=2) == []

The new test checks the case you reported (page 2), plus the first page, the partial last page, and a page past the end. I couldn't run it because the files aren't in this workspace. After you apply the diff, run pytest tests/test_pagination.py. The new test should fail on the old code and pass with the fix.

Something to consider: this diff only fixes the off-by-one. It still doesn't check that page is valid. Now that pages start at 1, page=0 gives start = -size, so it would return a slice taken from the end of the list instead of failing. If you want that rejected, add if page < 1: raise ValueError("page must be >= 1") next to the existing size check. I didn't include it because it changes behavior beyond the bug you reported.

Real run recorded with claude-code / claude-opus-5-5. Output is shown verbatim, unmodified.

它做什么

一条用于修 bug 和小幅行为调整的工作规则:只改真正负责的那一层,并证明修好了。

工作方式

  1. 复现成本低时先复现故障,否则先收集最有力的证据。
  2. 从现象追到出错的机制,只改那一层。
  3. 修复范围之外不做清理、不改名、不加新抽象,不碰无关的行为和用户自己的改动。
  4. 只补任务需要的回归测试,运行它和最相关的检查,通过后就停。

适合场景

在成熟代码库里修 bug,评审希望看到小而集中的改动。

需要了解

出自开源项目 Caveman 的一组通用工作方式。指令本身不带 Caveman 品牌,放到任何项目里都能用。

说明与风险

纯指令文件:没有脚本、不联网、不写文件。 压缩包里另有 LICENSE、NOTICE 和 agents/openai.yaml(供 Codex 使用的显示名和默认提示词)。