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 與小幅行為調整的工作規則:只改真正負責的那一層,並證明已修好。
運作方式
- 重現成本低時先重現故障,否則先蒐集最有力的證據。
- 從現象追到出錯的機制,只修改那一層。
- 修正範圍以外不做整理、不改名、不加新抽象,不碰無關的行為與使用者自己的修改。
- 只補任務需要的回歸測試,執行它與最相關的檢查,通過後就停。
適合情境
在成熟的程式碼庫中修 bug,審查者希望看到小而集中的變更。
需要了解
出自開源專案 Caveman 的一組通用工作方式。指令本身不帶 Caveman 品牌,放到任何專案都能使用。
純指令檔:沒有腳本、不連網、不寫入檔案。 壓縮檔中另附 LICENSE、NOTICE 與 agents/openai.yaml(供 Codex 使用的顯示名稱與預設提示詞)。