AI-Assisted Coding · Module 2Lesson 4

Study explanation · Lesson 04 · 12 min

Status transitions are a business rule

A PATCH route that sets a field is not a business rule. The backend decides whether a status change is allowed from the pair of current and new status, using a small set of allowed transitions, and returns 422 for anything else.

Listen to this lesson

Hear the lesson read aloud, with each sentence highlighted as you go.

Narrated by an AI voice.

A field setter is not a rule

Cursor can easily write a PATCH route that sets a field. That is not the same as implementing a business rule. In Part 2.3 the backend must decide whether a requested status change is allowed, based on both the current status and the requested new one. The UI can hide invalid buttons, but a user can still call the API directly with curl, Postman or another client. The backend is the trusted enforcement point.

Key ideaUI buttons can hide invalid actions; only the backend can refuse them.

The transition rules

Three moves are allowed. Everything else, including staying on the same status, is rejected.

The three allowed moves · Lecture notes p. 7 · Prompt library p. 14
  1. ToDoCan only move to InProgress.
  2. InProgressCan move to Done.
  3. DoneCan be reopened to InProgress, never back to ToDo.

Any pair not in VALID_TRANSITIONS, including same -> same, returns 422.

Transition rules · Lecture notes p. 7
TransitionValid?Reason
ToDo -> InProgressYesWork has started.
InProgress -> DoneYesWork is finished.
Done -> InProgressYesA completed task can be reopened.
ToDo -> DoneNoCannot skip InProgress.
Done -> ToDoNoCannot revert to the beginning.
Same status -> same statusNoNo-op moves are rejected in the intended rule set.

Read with careInProgress -> ToDo is not listed in the notes’ table. Prompt C1’s set of allowed pairs leaves it out, so it is rejected too.

Prompt C1: a rule module and a PATCH update

Prompt C1 creates a new module, app/business_rules.py, and modifies only the existing PATCH route. The rule is a frozenset of allowed (current, new) pairs and one function, validate_status_transition, that raises a 422 for any pair outside it. In the route, validation runs only when payload.status is provided, and only after the existing task has been fetched, so a missing id still returns 404 first. The prompt forbids inlining the rules as if/elif chains: the set is the rule.

Part of Prompt C1, as the library prints it (indentation flattened in the PDF)VALID_TRANSITIONS: frozenset[tuple[TaskStatus, TaskStatus]] = frozenset({ (TaskStatus.TODO, TaskStatus.IN_PROGRESS), (TaskStatus.IN_PROGRESS, TaskStatus.DONE), (TaskStatus.DONE, TaskStatus.IN_PROGRESS), }) Create this function: def validate_status_transition(current: TaskStatus, new: TaskStatus) -> None: # Same -> same is invalid. Anything not in VALID_TRANSITIONS is invalid. if (current, new) not in VALID_TRANSITIONS: allowed = sorted({f"{f.value}->{t.value}" for f, t in VALID_TRANSITIONS}) raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"Invalid status transition from {current.value} to {new.value}. Allowed transitions: {allowed}", )

What to review in the validator

Before applying C1’s output, the notes ask for six checks. The first and the last two are where generated code most often goes wrong.

  • app/business_rules.py defines VALID_TRANSITIONS as a frozenset of allowed pairs.
  • The validator checks the pair (current, new), not just whether new is a valid enum value.
  • Same-to-same transitions are not included in VALID_TRANSITIONS; if Cursor includes them, remove them.
  • Invalid transitions return 422.
  • The route fetches the existing task first, so a missing id returns 404 before transition validation.
  • Title-only or description-only updates skip transition validation.

Verification C: the six-test matrix

Run a transition sequence against one created task. The intended pattern of status codes is 200, 200, 422, 200, 422, 200. Because the checks run in order on the same task, each one starts from the status the previous check left behind. If the pattern is off, Prompt C2 takes the failing lines as evidence, lists the usual root causes, and forbids making a check pass by weakening the rules.

  1. T1: ToDo -> InProgress returns 200.
  2. T2: InProgress -> Done returns 200.
  3. T3: Done -> ToDo returns 422.
  4. T4: Done -> InProgress returns 200.
  5. T5: InProgress -> InProgress returns 422.
  6. T6: title-only update, no status field, returns 200.

Read with careThe notes describe the six checks but don’t print the script that runs them; it was not supplied.

Common AI mistakes in business rules

The notes list five, and C2’s root-cause checklist repeats most of them.

  • Allows every enum value because it checks only whether the new status is valid.
  • Returns 400 instead of the expected 422.
  • Validates status before confirming the task exists, causing the wrong error for missing ids.
  • Runs transition validation even when status is not in the PATCH payload.
  • Allows same-to-same transitions because the generated set includes them or because the rule was not explicit enough.

What you leave Part 2.3 with

Part 2.3 ends with the rule, the updated route, the matrix result and notes on what you corrected.

  • app/business_rules.py with VALID_TRANSITIONS and validate_status_transition.
  • PATCH updated to enforce transitions only when status is being changed.
  • The six-test matrix returns the expected pattern.
  • Notes about any AI-generated assumptions you corrected.

Key takeaways

  • The backend is the trusted enforcement point; the UI only hides buttons.
  • Three moves are allowed: ToDo to InProgress, InProgress to Done, Done back to InProgress.
  • Validate the (current, new) pair, after the 404 check, and only when status is in the payload.
  • The matrix pattern is 200, 200, 422, 200, 422, 200.

Built from the Module 2 lecture notes and prompt library; page references are in the source notes below.

Your study checkpoint

Ready to continue?

Mark the lesson complete once you have read it and tried its idea on something real.

Next · Lesson 5