Pairing with an AI Coding Assistant: Context, Review and Verification
Add a sort option to the list with an AI assistant, working the way that makes generated code trustworthy: project context up front, a failing test that encodes the acceptance criteria, a read diff, and the project's own gates before any commit.
Contentsand lesson path
On this page
- The task
- Step 1: put the project in the context
- Step 2: write the acceptance criteria as a failing test
- Step 3: request the change with the right material
- Step 4: review the diff
- Step 5: verify with the project's gates
- Check your understanding
- Apply it
- Review prompt
Lessons of Frontend Tooling & Builds
- Packages, Versions and Lockfiles Concept · 20 min
- Inside a Vite Project: Dev Server, Production Build and Composed Apps Worked example · 30 min
- Linting, Formatting and a Test Runner as Guardrails Worked example · 20 min
- Lab: From Empty Folder to a Linted, Tested, Built Project Lab · 30 min
- Pairing with an AI Coding Assistant: Context, Review and Verification Worked example · 25 min
- Check: Read a package.json, a Build Report and an AI-Generated Diff Knowledge check · 8 questions
What you will learn
- Use an AI coding assistant within the project's guardrails: supply repository context and acceptance criteria, review and test every generated change before committing it, and keep secrets and restricted code out of prompts.
01. The task
The list project now has the full guardrail set from the previous lesson, which makes it the right place to work with an AI coding assistant. The feature is small — add a sort option — and that is the point: the discipline below matters on every change, and learning it on a small one is cheap. The same loop applies to a human colleague; the assistant is simply faster and knows nothing about your project that you have not told it.
The loop has four stations. Give the assistant your project's context. Encode the acceptance criteria as a failing test before asking for code. Review the generated diff like any pull request. Then verify by running the project's own checks and only commit what passes. Runs below are real, from the list project.
02. Step 1: put the project in the context
An assistant generates code for a project like yours, not your project, unless you tell it what your project is. Many assistants read a project instruction file in the repository root (a growing convention is AGENTS.md); anything you would tell a new colleague belongs there. A useful file is short and concrete:
# Shopping list
Vanilla JavaScript, ES modules, built with Vite 8.
## Commands
- npm run dev dev server
- npm run check lint, format check and tests; must pass before a commit
## Conventions
- Pure logic lives in src/list.js; DOM code stays in src/main.js.
- Tests live next to the code as *.test.js and run with Vitest.
- Prettier formatting (print width 88, single quotes) is not negotiable.
## Data rules
- Never put secrets, .env contents or customer data into prompts.Two blocks matter beyond convenience. The commands block tells the assistant — and anyone reviewing its work — how the project checks itself. The data rules block states the boundary before it is crossed: prompts leave your machine, so they get code and errors, never credentials or personal data. When a change needs live configuration to debug, paste the shape with a placeholder, as in calling a JSON API from code, not the real key. A leaked key is rotated, not regretted.
03. Step 2: write the acceptance criteria as a failing test
Before asking for any code, write the test that says what "done" means. The feature: sort the visible list by a field, without mutating the caller's array — a rule the DOM layer relies on, since it keeps its own state. Put the test in src/sort.test.js and run it:
import { describe, expect, it } from 'vitest';
import { applyFilter, sortList } from './list.js';
const items = [
{ id: 1, label: 'Oats', done: false },
{ id: 2, label: 'Milk', done: true },
{ id: 3, label: 'Rye bread', done: false },
];
describe('sortList', () => {
it('sorts by label alphabetically', () => {
expect(sortList(items, 'label')).toEqual([items[1], items[0], items[2]]);
});
it('does not mutate the input array', () => {
const copy = [...items];
sortList(copy, 'label');
expect(copy).toEqual(items);
});
it('composes with applyFilter', () => {
expect(applyFilter(sortList(items, 'label'), 'o')).toHaveLength(1);
});
});$ npm run test
FAIL src/sort.test.js > sortList > sorts by label alphabetically
TypeError: sortList is not a function
❯ src/sort.test.js:12:12
Test Files 1 failed | 1 passed (2)
Tests 3 failed | 3 passed (6)Exit code 1, and the failure names exactly the missing thing. This run is the contract for the generated change: whatever the assistant writes has to turn this red suite green without touching the three passing tests. It also protects you from the most seductive failure of generated code — something plausible that solves a slightly different problem than the one you meant.
04. Step 3: request the change with the right material
The quality of a generated change tracks the quality of what you attach. What the assistant needs for this feature: the failing test (or its output), the current list.js, and one sentence of scope — "implement sortList in src/list.js; do not change any other file". What it does not need and must not get: the contents of .env, a real API key, or customer rows pasted for "context". The data rules from step 1 exist so this paragraph is short.
Assistants connected to tools through MCP (Model Context Protocol) can go one step further: instead of guessing at your conventions, they can run npm run test themselves and read the real output — the same protocol idea you will meet again in AI tool use. Whether the assistant or you press the keys, the rule that matters does not move: someone must run the real suite on the real project before a commit, and that someone is you.
05. Step 4: review the diff
The change that came back (here as the diff you would receive) is three lines:
+ export function sortList(items, key) {
+ return [...items].sort((a, b) => a[key].localeCompare(b[key]));
+ }Read it against a short list of questions, the same ones a pull request review asks of any diff. Does it match the conventions in the instruction file — pure function, in list.js, no DOM? Is the scope exactly the one file? The spread [...items] copies before sorting, which is what the no-mutation test demands; a version that called items.sort(...) directly would have mutated the caller's array and failed the test. localeCompare orders labels by the runtime's language rules, so Éclair sorts next to Eclair — unlike the naive a < b, which compares character codes and puts accented capitals after Z. Small diffs like this one are the sign to look for: if a one-function feature comes back as a ten-file refactor, ask for the small version before reading any of it.
06. Step 5: verify with the project's gates
Now the machines vote. Run the suite, then the whole chain:
$ npm run test
Test Files 2 passed (2)
Tests 6 passed (6)
$ npm run lint && npm run format:check && npm run build
$ echo $?
0Six tests pass, ESLint stays silent on clean code, Prettier and the build succeed, and the chain exits 0. Only now is the change a commit — with a message that describes the feature honestly, and, if your team tracks it, a note that an assistant drafted it and what you corrected. That note is not bureaucracy: "the first draft sorted in place and I asked for a copy" is exactly the kind of context a reviewer needs next time.
07. Check your understanding
08. Apply it
Run the full loop on the shopping-list practice for this skill:
- Write the
AGENTS.mdfor your project and read it once as if you were the assistant: can you follow it without asking anything? - Pick the next feature, write the failing test first, and keep the failing run on screen while you request the change.
- After reviewing and verifying the generated diff, submit both — the diff and a short note on what you corrected and why. The note is the deliverable the practice asks for.
09. Review prompt
Without looking back: what goes into a project instruction file, and which two blocks protect the project? Why is the failing test written before the generated code? Name three things to check while reading a diff. And which steps of the loop still apply when the assistant is a human colleague?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.