Day 6 of 960 minCipher CadetHands-on lab
Day 6: Agent Hooks: Automate Repeated Work Safely
Taught by Mr Elite Updated
By the end of today you can
- Explain what an agent hook is and which events can fire it
- Create a hook that runs on file save and asks the agent to update tests
- Test the hook with a harmless change and read what it did
- Spot tests that pass but check nothing, and keep hooks from looping or editing too much
Warm-up
Answer from memory first, then tap to check. Recalling yesterday is what makes it stick.
Where do steering files live in a Kiro project, and what are the three starter files called?
In .kiro/steering/. They are product.md, tech.md and structure.md.
If a workspace steering rule and a global steering rule disagree, which one wins?
The workspace rule wins.
Picture this. You change a function in your task app, tidy it up, save it, and move on. Three days later something breaks, because the tests still describe the old behaviour. Nobody forgot on purpose. Updating tests is dull, repeated work, and dull repeated work gets skipped.
Today I will show you how to hand that chore to the agent with a hook. I will also show you the catch. An agent can write tests that look green and prove nothing, and a hook can do that on every save, quietly, forever. So we build the automation and the habit of checking it together.
What a hook is and when it fires
A hook is a saved instruction that Kiro runs for you when something happens in your project. You write the event ("when a file is saved") and the instruction ("update the tests"). After that you stop typing the prompt yourself. Think of it as a tripwire connected to the agent.
Two events matter today. A file save trigger fires when you save a file that matches a pattern you choose, such as src/**/*.js. A file create trigger fires when a new file matching the pattern appears. The pattern is what keeps the hook narrow. A hook watching every file is a hook that will surprise you.
- Agent hook
- An event plus an instruction. When the event happens, the agent carries out the instruction.
- Glob pattern
- A short way to match file names, for example src/*.js means every .js file directly inside src.
- Trigger
- The event that starts the hook, such as saving or creating a file.
- Regression
- A bug that comes back, or an old behaviour that breaks after a change.
Quick check
You want a hook to run only when you edit your JavaScript files in src. What controls that?
The pattern decides which files fire the hook, so a tight pattern means a tight hook.
Writing an instruction the agent can follow
The instruction is the part that goes wrong most often. Vague words like "improve the tests" invite the agent to do whatever it likes. Good instructions say what to read, what to change, what not to touch and how to know it is finished.
A source file in src/ was just saved.
Read the saved file and its matching test file in tests/.
Add or update tests only for functions whose behaviour changed.
Each test must assert a specific expected value, not just that the code runs.
Do not edit any file outside tests/.
Do not change the source file.
When done, list which tests you changed and why.Watch me do it: build and fire the hook
We use the task app from Day 4. Everything runs on your own machine. If you do not have it, any small folder with a src file and a tests folder works. Open the project in Kiro first.
Quick check
After your test save, the diff shows edits in tests/ and in src/. What do you do first?
A hook that touches files outside its remit is breaking its boundary, so stop it before it runs again.
Limits: loops and surprise edits
Hooks run without you asking each time, which is the point and the risk. Three problems to know about.
Do the generated tests check anything?
A green test run feels like proof. It is not. A test that only calls a function and asserts that it returned something will pass even if the function is wrong. Humans call this false confidence, and AI tools produce it very easily because they are rewarded for passing, not for catching bugs.
Here is a quick way to check any test. Break the source on purpose, in a copy or with version control ready to undo, and see if a test fails. If everything stays green, those tests were decoration.
// weak: passes even if discount() is broken
test('discount runs', () => {
expect(discount(100, 10)).toBeDefined();
});
// strong: fails if the maths is wrong
test('10 percent off 100 is 90', () => {
expect(discount(100, 10)).toBe(90);
});Quick check
Which test would still pass if discount() always returned 0?
toBeDefined only needs some value, and 0 is a value, so it never notices the break.
Your turn
Tests That Test Nothing
This lab is exactly the problem a test-update hook can create. An assistant wrote seven tests for a discount function and coverage reads 100 percent, yet some tests would pass even if the function were broken. You get to practise spotting them, which is the review skill you need for every hook run. The lab is a SecurityElites practice environment, so nothing here touches a real system.
What to try
What to notice
- Coverage measures which lines ran, not whether anything was checked.
- Weak tests often look tidy and have confident names.
- The fastest test of a test is to imagine breaking the code and asking if it fails.
If it did not work
Using a broad pattern like **/* on the trigger
Narrow it to src/**/*.js or similar. If the hook fires on files you did not expect, the pattern is too wide.
Letting the hook edit files outside tests/
State the boundary in the instruction and check the diff file list on every early run.
Trusting green tests the agent wrote
Break the source on purpose and confirm at least one test fails. If none do, the tests check nothing.
Leaving a hook on during a big refactor
Toggle it off while you make wide changes, then turn it back on and run it once deliberately.
Recap
- A hook is an event plus an instruction that the agent runs for you.
- The file pattern decides how narrow or noisy a hook is, so keep it tight.
- Good hook instructions name what to read, what to change, what never to touch and what to report.
- Hooks can loop or edit files you did not expect, so keep edits in a separate folder from the trigger.
- Generated tests can pass while checking nothing, so test the tests by breaking the code.
- Never put secrets or destructive commands in a hook prompt.
Cheat sheet
File save trigger- Hook fires when a file matching the pattern is saved
File create trigger- Hook fires when a new matching file appears
src/**/*.js- Every .js file under src, at any depth
Boundary line- An instruction such as "do not edit outside tests/" that limits damage
Loop- A hook whose own edits trigger it again
Mutation check- Break the source on purpose to see whether any test fails
toBeDefined- A weak assertion that only checks a value exists
Before the next day
Mission: Fire the test-update hook
Create the hook on your own project, trigger it with a harmless edit, and judge honestly whether the tests it wrote check anything. The judgement matters more than the screenshot.
About 30 min
You are done when: A screenshot of the hook firing plus a short note saying whether the generated tests really check anything, with the result of your break test.
Want more? Rewrite the hook instruction so it requires each new test to name the wrong behaviour it would catch, run it again and compare the quality of the tests.
Quiz
4 questions. Your score is saved with your progress.
1. Why is a narrow file pattern a safety feature and not just tidiness?
The pattern is the first limit on when the agent acts without being asked, so a narrow one keeps its reach small.
2. A hook edits src/tasks.js and its pattern is src/**/*.js. What is likely to happen?
The edited file matches the trigger pattern, so saving it fires the hook again.
3. Coverage reports 100 percent after the hook writes tests. What does that tell you?
Coverage counts lines executed, not whether assertions would detect wrong results.
4. Which hook instruction is safest?
It has a narrow scope, a clear boundary and a report, which makes the run easy to review.
Finish the day
Mark the day complete to save your progress.
Next: Day 7 connects the agent to outside tools through MCP. Today you saw how much an automatic agent can do on its own, so tomorrow's question of which servers to trust will make more sense.
Your notes
Private to this device. Write what you want to remember from today.