Lompat ke konten

Pelajaran 25 ยท Projects and Reference

Project: Fix a Bug with Cursor

Practice debugging with Cursor: reproduce a billing date bug, write a failing test, find the root cause, fix it, check for similar bugs and prevent a return.

Fixing bugs in existing code is everyday work. In this project you take a subscription billing bug from report to commit. Cursor is your helper, and you stay in charge of the evidence.

The report

TEXT
Subject: Wrong billing date
A customer who subscribed on January 31 was billed on
February 28 (expected), then on March 28 instead of March 31.

Step 1: reproduce it

Here is the code in question. Run it.

Python Reproduce the drifting billing date

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dates stay on the 28th after February. You now have a reliable reproduction in a few lines.

Step 2: understand before fixing

Switch to Ask mode and ask:

TEXT
@billing.py
Why does a subscription that started on the 31st move to the 28th
and stay there? Point to the line. List every caller of next_billing.

Check the answer against what you saw. The cause is that each date is calculated from the previous date. Once February shortens the day to 28, the original day 31 is lost. The function has no way to know it.

Step 3: write a failing test

Describe the correct behavior: the day of the month comes from the start date, and is shortened only in months that are too short. Include a leap year, because February 2028 has 29 days.

TEXT
tests/test_billing.py:
 - start 2027-01-31: Feb 28, Mar 31, Apr 30, May 31
 - start 2028-01-30: Feb 29, Mar 30
 - December to January crosses into the next year
 - a start on the 15th always stays on the 15th

Confirm the rule with the person who owns billing. "Which day should this customer pay?" is a product decision, not a coding detail.

Step 4: choose the design

There is a real choice here. You could pass the original day into the function, or calculate the n-th billing date directly from the start date. Use Plan mode to compare them. Also ask what happens to stored records that may have drifted already. That question is about data, and a code fix alone does not answer it.

Step 5: fix and verify

Python The fix, with checks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 6: check many cases

Hand-picked examples are good. A property check over many inputs adds confidence. Two things should always be true: each billing date falls in the expected month, and its day is the start day or, for a short month, the last day of that month.

Python Check properties over many start dates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A bug often has relatives. Ask:

TEXT
Search the codebase for other date calculations that add months or
days to a previous result. For each one, say whether it can drift.
Do not change anything yet.

Also check how existing customers are affected. Changing stored dates may change when real people are charged. Do not let an agent run a data migration without a review, a backup, a dry run (which reports what would change and writes nothing) and approval from the person responsible.

Python A dry run that writes nothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This example works only because all its records are in March. A real script must handle every month length.

Step 8: review and commit

  • The test that failed now passes.
  • All callers use the new function with the original start date.
  • No temporary logging remains.
  • The commit message explains the cause.
TEXT
Fix billing day drifting after short months

Each date was computed from the previous one, so a shortened
February date became permanent. Compute from the start date.

Reflect

Which step taught you the most? What might have happened if you had asked only "fix the billing bug"? Add one line to AGENTS.md, such as "Compute recurring dates from the original start date", and keep the regression test, so that the bug cannot return unnoticed.

For bugs that are hard to reproduce, use Debug mode with the same discipline.

Why this bug survived

Think about why the original code could pass a casual test. A start on the 15th works forever. A start on January 31 even gives the right first result, February 28. The error appears only on the second step. A test that checked a single call would have passed.

This suggests a question for every review: does the code keep state from one step to the next, and have I tested a sequence? The same pattern appears in running totals with rounding, retry delays, repeating calendar events and ID counters.

Ask the agent the same question about your fix: "Is there any sequence of calls that gives a wrong result now?" Then test the answer. One limit remains in this example: it handles monthly billing by calendar date only. Time zones, plan changes in the middle of a cycle and paused subscriptions need their own rules, which someone must specify before anyone writes code for them.

Hand over the fix

Write a short note for support and the billing owner: who was affected, what changes from now on, which stored records still need a decision, and how to check the first billing run after release. Fixing the code is only part of fixing the problem.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What was the root cause?

    The original day of the month was lost after a short month.
  2. 2. Why write a failing test before the fix?

    It captures the bug and guards against its return.
  3. 3. How should a change to stored customer billing dates be handled?

    It affects real people and may be hard to reverse.

Summary

  • Reproduce first, then understand the cause.
  • Capture the bug in a failing test, and verify the fix with examples and properties.
  • Search for related bugs, and think about existing data.
  • Record the lesson in a test and a short instruction.

Next: Cursor Glossary and Cheat Sheet

Progres Anda disimpan di browser ini. Tidak perlu akun.