Everyday coding and bug fixing
Send the failing behaviour and the code around it, and have Claude Sonnet 5 explain the cause and the smallest fix. Add a test that reproduces the bug before you merge.
Example: Why does the export skip some orders?
Material
- export.ts
- for (let page = 1; ; page++) { const rows = await db.orders.find({ status: 'new' }).sort({ createdAt: 1 }).skip((page - 1) * 100).limit(100); if (!rows.length) break; for (const r of rows) await markExported(r); }
- Symptom
- With 250 new orders, only about 150 end up exported.
PromptUsing the code and the symptom, explain why records go missing and give the smallest fix.
Sample answer · written by hand to show the reasoning
markExported changes each order’s status, so after the first page those 100 orders no longer match status: 'new'. The query result shrinks, and skip(100) on page 2 jumps over 100 orders that were never exported. Either always read page 1 until it comes back empty, or page by createdAt (fetch rows created after the last one you processed) instead of using skip.