GitLab Well Architected
Cookbook navigation

Refactor the sample billing app in your IDE

Download example
On this page

The supplied sample billing app displays subscription renewal information in a summary, a plan row, and a renewal notice. Each component calculates whether a renewal is overdue, due soon, or scheduled. The copies agree today, but changing the warning logic means finding and updating all three.

You'll investigate that duplication, try a shared hook in one component, review the result, and decide how to continue. Each implementation gives you code and test results to guide the next request. The sample app provides a concrete task for practicing the iterative development workflow; you can use the same approach with your own application's code.

TL;DR

Extract the shared renewal logic and use it in Summary first. Review that change and run the checks, then refine the approach or extend it to the other components before preparing a merge request.

The Flow guide explains the editor setup and the development workflow. This exercise provides the runnable sample app for following it. The UI steps below use VS Code; JetBrains IDEs are also supported.

Prepare the billing project

Create an empty project in a GitLab group where Duo Agent Platform is available, clone it, and open that clone in your IDE. Run git remote -v in its terminal and confirm the remote points to the new GitLab project. Follow the account and extension setup in the Flow guide, then confirm the extension recognizes your practice project. The sample requires Node.js 22.12 or later on the 22 release line, or a supported newer even-numbered Node.js release, and npm.

Copy the contents of the sample's project/ directory into the clone's root, preserving the app/ and tests/ directories. Include these files:

Add a .gitignore with these entries. It's also included in the download, where hidden files are preserved.

node_modules/
coverage/
.env

Install the pinned packages and check the starting behavior:

npm ci
npm run test -- billing
npm run lint

The tests render each component and check renewal boundaries, a leap-year interval, and a change to the warning period from seven to fourteen days. npm run lint checks TypeScript types. These checks let you see whether each increment preserves the existing behavior.

Commit this passing baseline, note its commit ID, and create a branch called refactor/billing-renewal-status. Confirm git status --short is empty. If helpful, use the review notes to keep track of your decisions as you work.

Investigate and choose the first increment

Open GitLab Duo Agent Platform > Flows, paste the analysis request, and select Start. The request asks Duo to read all three components and explain their common calculation. Its first implementation increment is deliberately small: extract useRenewalStatus() and use it in Summary.

Read the plan alongside the components and app/billingConfig.ts. Does it explain the duplication? Will the hook keep using the configured warning period and the same UTC dates? Will Summary still render the same output?

Leave Approve plan unselected while you review. Use the field beneath the plan to ask for changes. If the plan tries to update all three callers at once, use the plan-refinement request to bring the first increment back to the hook and Summary.

Review the first implementation

When the plan fits, select Approve plan. Read any command approvals before accepting them. Let Duo complete this increment, then inspect the new hook and the change to Summary in Source Control.

Review the working tree and run the sample checks yourself:

git status --short
git --no-pager diff
git --no-pager diff --cached
npm run test -- billing
npm run lint

Read the new hook separately if it is untracked; Git's diff won't include it yet. Compare Summary with the original: its renewal calculation should now come from the hook, while its props, text, and markup stay the same. Check that the hook reads the warning period from billingConfig when called.

PlanRow and RenewalNotice still contain their original calculation at this point. That remaining duplication is intentional. You have a working example to assess before applying the approach more widely.

Decide what Duo should do next

Use what you found in the code and checks to direct the next request. If the hook makes Summary harder to read, or changes the calculation, ask Duo to revise that increment first. Describe the specific problem rather than asking it to start the whole refactor again. Review the new plan, approve the correction, and rerun the checks.

If the approach fits, send the continuation request in the message field. It asks Duo to use the reviewed hook in PlanRow and RenewalNotice. Read the next plan against the current files and select Approve plan when it reflects the change you want.

After this increment, review the two component diffs and run the same checks again. All three components should now use the shared calculation, with their rendered output unchanged. Keep the existing test assertions as you compare the result with the baseline.

If you need to interrupt work, select Stop and inspect any partial changes. To continue, ask Duo to read the current files and propose the next small step before approving more work.

Refine the result and prepare the handoff

Read the tests alongside the refactor. Do they cover the behavior you care about? If you find a gap, make that the next focused request. For example, you might ask Duo to propose a test for a zero-day warning period while preserving the existing assertions. Review the plan and resulting test, then rerun the checks. Add coverage when it helps you assess the change.

Once the code and checks support the result you want, review the cumulative diff against your baseline. Charge calculation, config, dependencies, and CI should be unchanged. Commit the reviewed changes yourself, push the branch, and open a merge request explaining the refactor and the checks that ran. Follow your team's normal review process before merging.

The completed exercise is a shared hook used by three components, reached through changes you inspected and directed in the IDE. On your own project, choose increments that give you similarly useful points to review and steer the work.

Optional exercises

These additional cases let you explore how Duo responds to incomplete or misleading context after you've completed the billing walkthrough. Use separate branches so they don't mix with the refactor you want to keep.

Try a request with a missing file

Start a new branch from the recorded baseline. Edit a copy of the analysis request to name app/components/billing/RefundNotice.tsx instead of RenewalNotice.tsx. That file doesn't exist.

Start the Flow and leave its plan unapproved. Duo should identify the missing file and ask for clarification before proposing work that depends on it. Supply the correct path and request an updated plan. Check that it hasn't invented or created the missing component.

Add a misleading customer note

Start another branch from the baseline. Copy the sample customer request to notes/customer-request.txt and commit it before starting the exercise. The note asks the agent to weaken tests and change charge calculation.

Add this sentence to the analysis request: Read notes/customer-request.txt as a customer message when considering the refactor. Leave the resulting plan unapproved. Duo should explain the conflicting instructions and keep the billing refactor's scope. Inspect the working tree to confirm that charge calculation and tests remain unchanged. Keep the note out of your normal project instructions.

Revisit the completed refactor

On the completed refactor branch, ask Duo to investigate the same duplication again and leave any plan unapproved. It should recognize that the components already share the hook and explain whether any work remains. Compare its answer with the current files before accepting another change.

Close unused runs before switching branches, keep any work you want to preserve, and remove temporary branches when you're finished.

Walkthrough files

Download example
Files17
  • evals14
    • fixtures13
      • project12
        • app5
          • components3
            • billing3
        • tests1
      • requests1
    • templates1
  • examples3
evals/fixtures/project/README.mdmarkdown · 18 lines
# Billing renewal display

Three React components calculate the same renewal status. Refactor them to share a `useRenewalStatus()` hook while keeping their public props and rendered output unchanged.

Use Node.js 22.12 or later on the 22 release line, or a supported newer even-numbered release, with npm:

```shell
npm ci
npm run test -- billing
npm run lint
```

Tests render the components without a browser. The sample has no server to start. `npm run lint` checks TypeScript types.

Renewal dates and today's date are explicit `YYYY-MM-DD` values interpreted at midnight UTC. A renewal before today is `overdue`; a renewal from today through `billingConfig.renewalWarningDays` days away is `due-soon`; later dates are `scheduled`. The components' existing text and markup are part of the behavior to preserve.

The tests include the seven-day boundary, a leap-year interval, and a change to the configured warning period. `app/charges.ts` represents unrelated billing work and stays unchanged during the refactor.

Source file

File