A bug in the tuck shop till
The school tuck shop has a small program that adds up a basket. A pupil buys crisps, juice and an apple, and the till says 35p. That is wrong. You could read the code and find the bug yourself. Today you ask an AI coding agent to help, and you check its work.
By the end of this lesson
Note
I can use a coding agent to fix a bug and check the fix myself.
Key words
- Coding agent: a program that reads and changes code for you.
- Model: the AI that writes the agent's answers.
- Prompt: what you ask the agent to do.
- Test: a small program that checks your code gives the right answer.
- Permission: the agent asking before it changes something.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. The lab is one service called agent, and the terminal reaches it by that name. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
1. Is the agent ready?
The agent is installed each time the lab starts. This prints installing while that happens and ready when it has finished. Run it again until it says ready.
docker exec agent cat /tmp/agent/statusShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
2. Run the tests first
Before anybody changes anything, find out what is wrong. The project has five tests. Four pass and one fails: a basket adds up expected 180 and got 35. Write down which test failed. You will need it later.
docker exec -w /home/node/project agent node --testShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
3. Ask the agent to explain
An agent can read a whole project. Here you ask a question and nothing else. opencode run asks one question and prints the answer. Read it: does it spot that the loop keeps only the last price?
docker exec -w /home/node/project agent opencode run "Explain what the total function in shop.js does, in three short sentences. Do not change any file."Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think an agent's answer must be right because it sounds sure. It can be wrong, and still sound sure. Trust a change only when you have run the tests yourself and read the line it changed.
4. Let the agent fix it, with your say-so
This opens the agent's own screen in your terminal. Type: Fix the bug in total in shop.js so the tests pass. Do not change shop.test.js. When it asks to change a file, read the change first, then allow it. When it has finished, press Ctrl+C twice to close it.
docker exec -it -w /home/node/project agent opencodeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
5. Check it yourself
Now run the tests again: all five should pass. Then run the till. The basket of crisps, juice and an apple should cost £1.80, and the change from £5 should be £3.20.
docker exec -w /home/node/project agent node --testShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
docker exec -w /home/node/project agent node shop.jsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Questions to answer
- Which test failed before the fix, and what did it expect?
- Which line did the agent change, and why was the old line wrong?
- Why did you run the tests yourself after the agent said it had finished?
- What should you never type into a prompt?
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service with an AI coding agent beside it, and its model is the platform's, in London. Key learning points: 1. An agent reads and changes code for you. 2. Run the tests before and after any change. 3. Read every change before you allow it. A pupil who believes the agent because it sounds sure is the mistake to listen for: ask them to show you the test passing. The student hands in the tests before and after, the changed line and their answers. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.