Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

These exercises practice retrieving what the Module 13 reading explained. They aren’t graded. Work each one on paper first, then check your answer, and only then confirm it with code from Lab 13 if you like. The answers are in the collapsed blocks; open one only after you have an answer of your own.

Worked example: placing three instructions

The shop’s stock repository has three instructions to place. For each, ask which files it applies to, and who it is for:

  1. “Run the tests with python -m pytest -q.” Every task needs it, so it goes in CLAUDE.md, which loads in every session.

  2. “The CSV files in data/ have the columns item and count, in that order.” It matters only when Claude works on those files, so it goes in a rule with paths: ["data/**"], which loads only then.

  3. “I like short commit messages.” It is one person’s preference, so it goes in that person’s CLAUDE.local.md, which they add to .gitignore so it stays out of the repository.

A rule scoped to data/** applies to data/tickets.json and to data/archive/old.json, at any depth under data/. Keep an instruction that both scopes share, such as the test command, in CLAUDE.md, rather than copying it into each rule.

Exercise P13.1 (LO-13.1, ★, about 10 minutes)

Your instructions for money apply across the project, and ID formatting matters only in fixtures. Write one sentence for each file.

Hint

Place the narrow rule under data/**.

Answer

In CLAUDE.md: “Store every amount of money in integer cents, as repair_total does.” In the rule scoped to data/**: “Give each fixture a stable, unique ID, a letter and two digits such as T07, and never reuse one.”

The money rule goes in CLAUDE.md because every change that touches prices must follow it. The ID rule goes in the scoped rule so it costs context only when Claude works on fixtures. A common mistake is to write a vague sentence such as “be careful with money,” which no one can check.

Worked example: a skill, a subagent, and a decision

Each time the handbook’s opening hours change, the same steps repeat: edit section H01 in data/handbook/sections.json, run the tests, and report the change. Split the work:

Exercise P13.2 (LO-13.2, ★★, about 15 minutes)

The same test review happens on every label change. Which steps belong in the skill and which can you delegate?

Hint 1

Give a subagent a bounded question, one whose answer is a short report.

Hint 2

Sort the steps into three groups: the procedure, the self-contained checks, and the decision.

Answer

Put the process and the form of its output in the skill: inspect the diff, run the regression tests, and summarize the evidence. Delegate a bounded check, such as whether the tests cover both separators, underscores and hyphens, because it is self-contained and returns a short report. Keep the decision about accepting the patch with the main task, which holds the context and your judgment.

A common mistake is to delegate the acceptance decision too. The subagent never saw the task’s conversation, so it can’t weigh what the task allowed.

Worked example: test inputs for a deny-only hook

A PreToolUse hook on Bash should deny any git push and stay silent otherwise. Choose test events that cover each way it can behave:

Event piped to the hookExpected output
A Bash event whose command is git statusNothing, exit status 0: no decision
A Bash event whose command is git push origin mainA deny decision with a reason
The text [], valid JSON that isn’t an eventA deny decision with a reason

The third row tests the path where a hook fails open. If the script crashed on [], Claude Code would treat the crash as an error and let the call through, so a deny-only hook must catch the error and deny.

Exercise P13.3 (LO-13.3, ★★, about 15 minutes)

Name three inputs for a deny-only hook and their expected output.

Hint 1

Cover the event the hook should let through, an event it should refuse, and input it can’t read.

Hint 2

Include malformed input.

Answer
  1. The exact test event, a Bash call with command set to python -m unittest discover -v: no output, exit status 0, so no decision, and the permission rules decide.

  2. An unfamiliar command, such as sample-action: a deny decision under hookSpecificOutput, with a reason.

  3. Malformed JSON, such as {: a deny decision too.

The third input matters most. A hook that raises an exception on it fails open, and the call goes ahead. A common mistake is to test only the two well-formed events, which never exercise that path.

Worked example: what an edit hook doesn’t see

A project registers a PreToolUse hook with the matcher Edit|Write that denies edits under data/. Does it stop sed -i 's/T07/T08/' data/tickets.json?

No. That command runs through the Bash tool, and the matcher names only Edit and Write, so the hook never runs. The hooks guide makes the same point: an edit hook doesn’t stop changes made with shell commands. Covering that route needs its own control, such as a Bash hook or a deny rule.

Exercise P13.4 (LO-13.3, ★★★, about 25 minutes)

A Bash hook is installed. Are all edits to a Markdown file covered?

Hint 1

Think about which tool triggers the event matcher.

Hint 2

List every tool that can change a file. Which of them does a Bash matcher name?

Answer

No. The matcher covers only Bash, so the hook sees shell commands that write the file, such as sed -i, but never an Edit or a Write call. Those tools have their own events and their own permission rules. Review both layers, and add a dedicated control where you need one: a hook with the matcher Edit|Write, or an Edit deny rule for the file.

A common mistake is to assume a hook covers a file. Hooks match tools, not files, so a hook covers only the routes its matcher names.