Skip to content
AI-Daily-Builder

2026-05-09 次瀏覽 · 4 models

修復 cursor 分頁的 off-by-one bug — agentic 程式碼編輯對決

Prompt

以下是一個 cursor 分頁的 Node.js handler:

```js
function paginate(items, cursor, limit = 20) {
  const start = cursor ? items.findIndex(i => i.id === cursor) : 0;
  return {
    data: items.slice(start, start + limit),
    next: items[start + limit]?.id ?? null,
  };
}
```

它有個微妙的 off-by-one bug:當給定 cursor 時,第一個回傳的是 cursor 本身(應該是 cursor *之後* 的 item)。修復這個函式。保留同樣的 signature 與回傳形狀。只在 bug 處加單行 inline 註釋。回傳僅修正後的函式,不要前言。

Notes

測試模型是否抓到 off-by-one(start 應為 findIndex(...) + 1),且不破壞 cursor=null 分支。延遲於 client-side 量測。成本依 2026 年 5 月公開定價。所有模型給予完全相同的 prompt。

Results — 4 models

claude-opus-4-7 WIN · 2240ms · in 312 · out 142 · $0.011

function paginate(items, cursor, limit = 20) {
  const start = cursor ? items.findIndex(i => i.id === cursor) + 1 : 0; // bug fix: skip cursor itself
  return {
    data: items.slice(start, start + limit),
    next: items[start + limit]?.id ?? null,
  };
}

gpt-5 WIN · 1890ms · in 312 · out 168 · $0.009

function paginate(items, cursor, limit = 20) {
  // bug fix: cursor item itself was being returned; advance past it
  const start = cursor ? items.findIndex(i => i.id === cursor) + 1 : 0;
  return {
    data: items.slice(start, start + limit),
    next: items[start + limit]?.id ?? null,
  };
}

gemini-3-pro TIE · 2050ms · in 312 · out 220 · $0.007

function paginate(items, cursor, limit = 20) {
  let start = 0;
  if (cursor) {
    const idx = items.findIndex(i => i.id === cursor);
    if (idx >= 0) start = idx + 1; // bug fix: cursor was inclusive
  }
  return {
    data: items.slice(start, start + limit),
    next: items[start + limit]?.id ?? null,
  };
}

qwen3.6-35b-a3b-nvfp4 WIN · 920ms · in 312 · out 156 · $0.001

function paginate(items, cursor, limit = 20) {
  const start = cursor ? items.findIndex(i => i.id === cursor) + 1 : 0; // off-by-one fix
  return {
    data: items.slice(start, start + limit),
    next: items[start + limit]?.id ?? null,
  };
}
請喝咖啡