A Rule the Model Must Not Break
Why This Matters
A prompt rule is a request; a code rule is a guarantee. Deciding which one a requirement needs is one of the most important judgement calls in this course, and on the exam.
Experiment 1: a prompt rule that matters
TestPilot’s system prompt contains:
Prefer “unknown” with low confidence over a confident guess.
Remove that line from src/prompt.ts and triage t-006, the avatar upload that failed with nothing but expect(received).toBeTruthy(): no log, no history. Then put the line back and run it again.
With the rule, a real run answers unknown (confidence 0.10): the cause cannot be determined from this data, and suggests re-running the test with tracing (see the run in lesson 8). Without the rule you will often get a confident category built on almost nothing. With it, TestPilot is far more likely to say “unknown”. Prompt rules clearly shape behaviour. But far more likely is not always.
Experiment 2: a rule that must always hold
Sometimes TestPilot labels a failure “flaky” while the test’s history shows no failures at all. That is the most dangerous mistake it can make: a real product bug, hidden behind a label that tells everyone to rerun and move on.
You have four options:
| Option | What it does | Reliable? |
|---|---|---|
| Louder wording in the prompt | Makes the mistake rarer | No guarantee |
| Few-shot examples | Shows what flaky looks like | Can backfire (lesson 20) |
| Different model settings | Changes behaviour unpredictably | No guarantee |
| A check in code, with a retry | Rejects the verdict and asks again | Yes |
Build the guard
In src/triage.ts, after validation:
const triage = parsed.data;
if (triage.category === "flaky" && failureRate(result) === 0) {
if (attempt >= 1) {
return { ...triage, category: "unknown", confidence: 0, summary: "Model said flaky without any failure history; needs a human." };
}
return triageFailure(client, run, result, options, {
feedback: "You answered flaky, but this test has no failures in its history. Flaky requires earlier intermittent failures. Choose another category.",
attempt: attempt + 1,
});
}
Three design choices:
- The check uses code (
failureRate), not the model’s opinion. - The retry includes feedback, so the model knows what was wrong instead of repeating the same answer.
- One retry, then fail safe: “unknown” with zero confidence and a clear note for a human. No endless loops.
Test it
With the fake client, answer “flaky” first and a valid product_bug second, and check that the second request contains your feedback:
const parse = vi.fn()
.mockResolvedValueOnce({ parsed_output: { ...validTriage, category: "flaky" } })
.mockResolvedValueOnce({ parsed_output: validTriage });
Add a second test where the model says flaky twice, and check that the result is “unknown”.
A rule of thumb
If breaking the rule has a real cost, enforce it in code. Use the prompt to make the right answer likely; use code to make the wrong answer impossible.
Exercise
docs/exercises.md, lesson 10. Compare with solution/lesson-10-flaky-guard.
Show suggested answers
1. Without the “prefer unknown” rule, t-006 usually gets a confident-sounding category (often test_bug or product_bug, with confidence around 0.6–0.7) built on almost nothing: no log, no history, one generic assertion. Run it a few times and the category may change between runs. With the rule back, it returns unknown with low confidence. That is the point of the rule: when evidence is thin, a calibrated “I don’t know” is more useful than a guess.
2. The scenario: B. A and D make the wrong answer less likely but cannot rule it out. C is not even an option any more: current models only accept the default sampling settings, and API version 2026-10-21 rejects temperature altogether. And it never was a fix: less randomness is not more knowledge, and a model can be consistently wrong. B checks the rule in code, where it cannot be ignored, and the retry with feedback lets the model correct itself. If the retry breaks the rule again, it falls back to unknown with zero confidence for a human to check.
3. See solution/lesson-10-flaky-guard: a checkVerdict function returns the rule violation, and triageFailure retries once, showing the model its rejected answer plus that message. The tests cover all three paths: flaky with real failure history is accepted; flaky without history is retried and the second request contains the feedback; flaky twice ends as unknown with confidence 0.