The short answer#
AI coding can produce plausible code while filling gaps in an ambiguous request with assumptions. A small specification, or spec, makes intended behaviour, repository constraints and acceptance cases explicit; tests then check the implementation against those decisions. This reduces interpretation gaps, but the spec still needs human challenge, relevant context and code review.
The code works. Which requirement did it implement?#
Imagine asking an AI coding assistant: “Reject an invitation when it has expired.”
That sounds clear until someone asks what happens at the exact expiry instant. Does the invitation remain valid at that instant, or must it already be rejected? What are the timestamp units? Where does the current time come from?
Here is a deliberately invented implementation, not a recorded AI response:
function isExpired(expiresAtMs: number, nowMs: number): boolean {
return nowMs > expiresAtMs;
}
It accepts an invitation before expiry and rejects one after expiry. Yet if the intended rule is “expired at or after the deadline”, it fails exactly at equality. A review that checks only the ordinary before-and-after cases can miss it.
The function does not reveal a mysterious model failure. It reveals an unresolved decision. A developer could make the same assumption. Generating more code before settling it only spreads that assumption into callers and tests.
GitHub's guidance on Copilot inline suggestions explicitly recognises that generated code may be incorrect or fail to reflect developer intent, and calls for review and testing. A convincing explanation beside the function is not evidence that its boundary is right.
Separate three different causes#
Use the symptom to decide what to fix. “The AI hallucinated” is too broad to be a useful diagnosis for every wrong result.
| Cause | Example in a coding task | Useful response |
|---|---|---|
| Missing intent | Nobody specified whether equality counts as expired | Ask the requirement owner and record the decision |
| Unsupported assumption | The assistant calls a repository helper without checking that it exists | Inspect the real implementation and verify the signature |
| Missing or stale context | A later attempt follows an older boundary rule | Supply the current spec and relevant files again; check for conflicts |
An invented helper presented as real is an example of hallucination: plausible output without support in the available evidence. A missing boundary decision is an ambiguity. Both can produce wrong code, but they need different corrections.
Context means the information available to the model for its current response. Saving a spec in a repository helps only when the assistant actually reads the relevant version. GitHub's context-management documentation says that compaction summarises a conversation and can omit fine details, including minor earlier decisions. Do not rely on a decision surviving only in a long chat.
Anthropic's context-engineering guidance also emphasises selecting useful information within a limited context budget. The practical lesson here is to supply the approved rule and the files needed to implement it, rather than assume the assistant shares the team's background knowledge.
Write the smallest spec that settles the decision#
For this exercise, assume the requirement owner has chosen the inclusive expiry boundary. These are teaching decisions for this example; your application may need different ones.
Feature: invitation expiry predicate
Behaviour:
- isExpired(expiresAtMs, nowMs) returns a boolean.
- Both arguments are finite epoch timestamps in milliseconds.
- Before expiresAtMs: false.
- At or after expiresAtMs: true.
- A non-finite argument throws RangeError.
Constraints:
- The caller supplies nowMs; do not read the system clock here.
- Keep the function pure: no network, storage or other side effects.
- Do not add dependencies or change callers in this exercise.
Acceptance cases:
- expiry 1000, now 999 -> false
- expiry 1000, now 1000 -> true
- expiry 1000, now 1001 -> true
- non-finite input -> RangeError
Scope:
- This predicate does not authenticate users or validate invitations.
- Parsing timestamps and enforcing access belong to the caller.
The numbers are teaching values in milliseconds, not a suggested invitation lifetime. “Finite” excludes NaN and positive or negative infinity. The spec deliberately leaves timestamp parsing outside this function so the example has one boundary to teach.
In a real repository, add the actual file path, public interface and existing error conventions after inspecting them. If the repository already uses another error type, resolve that conflict before coding. A spec grounded in imaginary architecture is another source of wrong code.
Run the acceptance cases#
Save the following complete example as expiry.test.ts. It contains the implementation and its tests so you can run it in isolation.
import assert from "node:assert/strict";
import { test } from "node:test";
function isExpired(expiresAtMs: number, nowMs: number): boolean {
if (!Number.isFinite(expiresAtMs) || !Number.isFinite(nowMs)) {
throw new RangeError("Expected finite timestamps");
}
return nowMs >= expiresAtMs;
}
const cases = [
{ name: "before expiry", expiry: 1000, now: 999, expected: false },
{ name: "at expiry", expiry: 1000, now: 1000, expected: true },
{ name: "after expiry", expiry: 1000, now: 1001, expected: true },
];
for (const c of cases) {
test(c.name, () => {
assert.equal(isExpired(c.expiry, c.now), c.expected);
});
}
test("rejects non-finite arguments", () => {
for (const invalid of [NaN, Infinity, -Infinity]) {
assert.throws(() => isExpired(invalid, 1000), RangeError);
assert.throws(() => isExpired(1000, invalid), RangeError);
}
});
In a scratch directory with Node.js 22 and npm, install the TypeScript runner and execute the file:
npm init -y
npm install --save-dev tsx
npx tsx --test expiry.test.ts
All four tests should pass. Now replace only >= with > and run them again: the “at expiry” test should fail. That is the important check. The acceptance case distinguishes the agreed behaviour from a plausible alternative.
Supplying nowMs also keeps the tests independent of when they run. For an application, a trusted caller must supply the appropriate time; accepting a user's chosen timestamp would require a separate design decision.
These tests cover the stated predicate contract. They do not prove invitation security, correct timestamp conversion or correct integration with the rest of your application.
What this means for your next coding task#
Use this short sequence before generating a change:
- Settle the behaviour. Write the normal case, the boundary and a counterexample. Ask the owner to resolve any missing decision.
- Ground the task. Inspect the relevant implementation, callers and repository conventions. Name the files the assistant should read.
- Ask for uncertainties. Have the assistant identify conflicts and assumptions before implementing the change.
- Check against the spec. Review the diff and run acceptance cases whose expected results come from the approved decisions.
For delivery heads#
Have the team practise settling a boundary decision before generating code, then tracing it through the spec, the changed code and an acceptance case. In reviews, ask someone to explain which plausible wrong behaviour that case would reject. Look for unresolved assumptions being raised before implementation and for boundary decisions remaining visible in the review. A passing test suite is more useful when the reviewer can show how it checks the agreed requirement.
Keep the effort proportionate. An exploratory prototype may first help the team discover what it wants. Do not write a large, rigid spec around behaviour nobody has agreed. For a small deterministic change, a compact contract and a few discriminating cases may be enough. For broader changes, this exercise is a starting point, not a complete delivery process.
Questions developers ask#
Will a spec stop hallucinations?#
No. It makes decisions available for implementation and review. An assistant can still misunderstand the spec, invent a helper or produce a bug. Verify repository claims, run tests and review the code.
Can the same assistant write the spec and the tests?#
It can help draft both, but a person must challenge the assumptions and expected results. If implementation and tests share the same wrong interpretation, they can agree with each other and still miss the requirement. The equality case above is useful because its expected result was decided before the implementation.
Where can a team practise this workflow?#
The Day 1 opening topic, “From Vibe Coding to Spec-Driven AI Delivery & the SDD Tool Landscape”, in Specs-Driven Development includes comparing a vague request with a structured spec. The expiry exercise gives you a small version to try before working through repository context, contracts and review as a team. Enquire about Specs-Driven Development.
