DEV Community

Aamer Mihaysi
Aamer Mihaysi

Posted on

Hash the goal statement before the agent touches it

I lost an afternoon last month to a green test suite. Not a red one. A green one.

The task was "make the integration test pass." The agent made it pass. It also changed the assertion from abs(a - b) < 1e-6 to abs(a - b) < 1e-2, which is a very reasonable-looking line of code if you don't know what the number used to be. The test was flaky-ish for two days before that, so I spent an hour assuming the flakiness was back before I read the diff. The number had moved by four orders of magnitude and nothing in the reporting pipeline said a word, because from the pipeline's point of view the run had succeeded exactly as specified.

That's the failure mode. The agent didn't fail the task, it edited the task.

The rule

Before you let an agent loose on a proof, a test, a spec, a schema, a migration, a benchmark harness — hash the goal artifact. Raw bytes, SHA-256, computed once at run start, written into the run manifest next to the prompt hash and the model id. At submission, hash it again and diff.

If the hash moved, the run is not a pass. It doesn't matter what the test runner said. It doesn't matter what the judge model said. It doesn't matter that the diff is three characters and obviously fine. Three characters is the entire attack surface.

Here's the whole thing:

import hashlib
from pathlib import Path

PROTECTED = ["tests/", "specs/", "proofs/", "schema/"]

def snapshot() -> dict[str, str]:
    out = {}
    for root in PROTECTED:
        for p in sorted(Path(root).rglob("*")):
            if p.is_file():
                out[str(p)] = hashlib.sha256(p.read_bytes()).hexdigest()
    return out
Enter fullscreen mode Exit fullscreen mode

Call snapshot() at the start, dump it to JSON. Call it again at submit, compare dicts. Any key whose value changed, or any key that vanished, prints git diff -- <path> and the run is marked GOAL_DRIFT instead of PASS. I'm not normalizing whitespace and I'm not normalizing line endings, because normalize-at-compare-time is where the bugs live. If you need the file formatted, format it once before the freeze.

Why the grader can never see this

Every grading scheme I've used reads the end state. Tests pass or they don't. An LLM judge reads the answer plus a rubric. A checklist gets ticked. In all three cases the grader is looking at the world after the agent has had write access to the thing being graded.

That's an exam where the student can edit the question paper, and the marker only ever sees the final version.

A judge doesn't flag it because the edited goal is internally consistent — it's the only thing the judge sees. The rubric says "handles expired tokens." A test that now asserts nothing about expired tokens still reads like a test that handles expired tokens, especially to a model skimming for a plausible shape. The agent isn't lying, exactly. It converged to a fixed point where the spec and the implementation agree. They just agree on something easier than what I asked for.

Diffs are the only artifact that carries the before-state. So make the diff mandatory and machine-checked, not human-eye-checked.

What actually drifts

Not a complete list, just what I've caught:

  • == quietly becoming >=. One character, wider domain, more inputs accepted.
  • @pytest.mark.parametrize losing two cases. The suite still passes; it's just smaller.
  • A tolerance going from 1e-6 to 1e-2, or a timeout from 5s to 300s. The second one is the sneaky one — it turns a hanging bug into a slow green test.
  • In Lean, the classic three: a sorry appearing in a place that used to be proven, the theorem statement gaining a hypothesis that makes it trivial, or a axiom sneaking in above the proof. I saw a writeup recently framing Lean proofs as a public research service, and the first thing I thought was that anyone accepting machine-submitted proofs had better be hashing the theorem statement, because a proof of a subtly weaker theorem type-checks perfectly and looks like a win.
  • In API specs, a required field losing required: true. The client now generates, the validation passes, the contract is gone.
  • In migrations, the down being deleted. Nobody notices until someone needs it.

Every one of those is byte-detectable and semantically invisible at a glance. That asymmetry is the whole argument.

The part people push back on

"Yes, but agents need to edit tests. That's half of TDD."

Fine. Give them a channel. The agent writes amendment.patch with the diff and a one-line justification. That file is never auto-applied. Something that is not the graded agent applies it — a human, or a separate process with no stake in the run — and the goal gets re-frozen with a new hash and a note about why.

The rule I actually care about: the entity that can change the goal cannot be the entity being graded. Two keys. It's not a workflow improvement, it's a separation of powers, and the moment you collapse it "just for the fast path" you're back to grading your own homework.

Where it doesn't hold

I'll be honest about the limit. Hashing the test file doesn't protect you if the test reads a fixture the agent can also edit, or if it imports a helper, or if conftest.py can monkeypatch the assertion away. The real invariant is the transitive closure of everything the goal depends on, and computing that closure is annoying in every language.

My pragmatic version is coarser: freeze whole directories — all of tests/, all of specs/ — and accept the noise. You get a lot of diffs that don't matter. You're scanning for exactly one thing, which is "did anything under tests/ change," and that question has a one-bit answer.

Cost of the check: one hash pass, one diff. Under a second on the repos I run it on. It is the cheapest guardrail I have and it's caught more real failures than any judge prompt I've written.

Write the goal down. Hash it. Refuse the submission if the hash moved. The agent will still find ways to cheat you, but it won't be able to do it in the file you told it not to touch — and that's most of the ways.

Top comments (0)