Practical technical guides / Agent-era architecture: testing (metamorphic relations for agent decisions)

Test Agent Decisions Without an Exact Oracle: Metamorphic Relations for Paraphrases, Irrelevant Details and Thresholds

Five metamorphic relations caught four planted extraction flaws in a refund agent without labels. Invariance misses consistent errors; threshold and negation relations do not.

By Sandeep Belgavi · Published 2026-11-11

Five metamorphic relations caught four planted extraction flaws in a refund agent without labels. Invariance misses consistent errors; threshold and negation relations do not.

A decision with no exact expected output

Belgavi's Book Store lets an agent read return requests and propose a decision: refund in full, send the parcel for inspection, deny because the return window has closed, or deny because the customer damaged the item. The policy behind those decisions is a short table that lives in code. The hard part is upstream: customers write free text, and the agent has to turn "I paid 1,250 for a box set delivered 29 days ago" into an amount, a number of days and a damage flag.

Testing that step is awkward. For a fixed message you can write the expected decision by hand, but customers write thousands of messages in thousands of ways, and nobody can label them all. A model's wording changes between versions, so asserting on its text is brittle. What you can say with confidence is how decisions must relate to each other. Rewording a message must not change the decision. Adding an unrelated sentence must not change it. Raising the amount above the inspection threshold must move a full refund to inspection. Those relations are the basis of metamorphic testing.

Metamorphic relations in one paragraph

Metamorphic testing was introduced by Chen, Cheung and Yiu in 1998 for programs that have no practical test oracle, the situation where you cannot easily say what the correct output is for an arbitrary input. Instead of checking one output, you run a source input and one or more follow-up inputs derived from it, and check that the outputs satisfy a relation. The classic example is a sine function: you may not know sin(x) to the last digit, but sin(x) must equal sin(pi - x). Surveys by Segura and colleagues (2016) and Chen and colleagues (2018) describe its use from compilers to machine learning. For language models, the CheckList work of Ribeiro and colleagues (2020) uses the same idea under different names: invariance tests (a perturbation must not change the prediction) and directional expectation tests (a change must move the prediction a known way).

Five relations for a refund decision

The companion example defines five relations for the refund agent. Each one is a function from a source message to follow-up messages, plus a check on the two decisions.

RelationFollow-up inputCheck
MR1 paraphrasethe same facts in a different templatesame decision
MR2 irrelevantthe source plus an unrelated sentencesame decision
MR3 over thresholdthe amount set to 2,500, above the 2,000 inspection thresholdrefund_full becomes inspect
MR4 earlierdelivered one day earlieran accepted return never becomes deny_late
MR5 negation"not damaged by me" with the "not" removedthe decision must change

MR1 and MR2 are invariance relations. MR3 and MR5 are directional: they say which way the decision must move. MR4 is monotonic: moving the delivery date earlier can only help the customer.

The relations encode business rules you already have. You do not need a labelled dataset, only the policy and a handful of realistic source messages. The companion uses ten: eight ordinary returns at different amounts and ages, and two damage reports.

Planted flaws and what catches them

To show what the relations can and cannot see, the companion replaces the model with a deterministic extractor followed by the store's policy, and lets you switch on four flaws of the kind language models show in extraction.

  • thousands_separator: reads "1,250" as 1.
  • negated_damage: sees the word "damaged" and ignores the "not" before it.
  • last_number: takes the last number in the message as the amount.
  • first_number_as_days: takes the first number as the days since delivery.

Here is what the five relations report for each, as checks and violations:

ExtractorMR1MR2MR3MR4MR5
clean16, 010, 08, 08, 01, 0
thousands_separator16, 010, 08, 88, 01, 0
negated_damage16, 010, 08, 08, 01, 1
last_number16, 010, 98, 88, 01, 0
first_number_as_days16, 1610, 00, 08, 01, 1

Every flaw is caught by at least one relation, and the clean extractor passes all of them. The table also shows three things that are easy to miss.

Some flaws are invisible to invariance. The negation flaw misreads the source and every paraphrase the same way, so MR1 and MR2 pass. Only the directional MR5 catches it, because it asks for a change, not for sameness. A suite built only from "the answer should not change" relations is blind to errors that are wrong consistently.

Some flaws only show near a threshold. Reading "1,250" as 1 does not change any decision for the source messages, because every amount is below the inspection threshold either way. MR3 pushes the amount over the threshold, and then every check fails: 2,500 is read as 2 and refunded in full. Relations that cross policy boundaries are worth more than relations that stay safely inside them.

A relation can run out of checks. With the first_number_as_days flaw, every source message is denied as late, so MR3, which only applies to sources that were refunded in full, has zero checks. A report that shows only violations would show a clean MR3. Always report checks next to violations, and alert when a relation's check count falls.

Where the deterministic part ends

The policy itself is not tested with metamorphic relations. It is a function of three fields, and it has exact expected outputs at every boundary: 2,000 is refunded in full and 2,001 goes to inspection. Test it with ordinary unit tests and, if you want, property-based tests over its inputs. The relations are for the step that has no exact oracle: the agent turning text into those fields.

That split is the general shape for agent-era systems. Keep the decision rule deterministic and testable with exact expectations. Use metamorphic relations, and the occasional labelled case, on the step where a model reads the world. Distinguish a relation failure caused by the model from one caused by the policy: if the follow-up's extracted fields are what you expected and the decision is still wrong, the policy has a bug.

Building relations for your own agent

Start from the decisions that matter, and write down for each one what must not change it and what must change it.

  1. Invariances. Paraphrase, reorder sentences, change greeting and sign-off, change number formatting ("1,250" and "1250"), add irrelevant details, translate if you support several languages.
  2. Directional rules. Cross every threshold in both directions. Flip negations. Change the one fact the decision depends on and nothing else.
  3. Monotonic rules. If a change can only help or only hurt, check that the decision moves at most in that direction.

Generate follow-ups from templates or from careful rewriting by a person; if a model generates paraphrases, check them before use, because a paraphrase that changes a fact makes the relation wrong rather than the agent. Keep the source set small and realistic, and grow it from incidents: every production mistake becomes a new source message with its relations.

Running relations against a real model

A deterministic stand-in makes the example reproducible. Against a real model, two things change. Outputs vary between runs, so run each source and follow-up a few times and count a relation as violated only when the decisions disagree more often than the model disagrees with itself on identical input. And relations cost calls: five relations over a hundred sources with two follow-ups each is a thousand extra calls per model version. Run the full set when the model, prompt or extraction schema changes, and a sample on a schedule.

def run_relations(decide, sources, relations):
    report = {name: {'checks': 0, 'violations': 0, 'first': None} for name, _, _ in relations}
    for src in sources:
        d0 = decide(src)
        for name, follow, ok in relations:
            for f in follow(src, d0):
                d1 = decide(f)
                report[name]['checks'] += 1
                if not ok(d0, d1):
                    report[name]['violations'] += 1
                    report[name]['first'] = report[name]['first'] or (src, f, d0, d1)
    return report

The function takes the agent as a plain callable, so the same relations run against the stand-in, a recorded set of model outputs or a live model.

How this differs from neighbouring techniques

Property-based tests of the deterministic guards check that rules such as "never refund above the order total" hold for every generated input; they test code you wrote. Evaluation against a labelled set measures accuracy on cases someone already judged. Metamorphic relations sit between the two: they test the model's reading of the world without labels, and they find errors that a labelled set has no example of. They do not measure accuracy. A model that is wrong in the same way on every paraphrase can pass every invariance relation, which is why the directional and threshold relations matter.

Limits

The companion's extractor and flaws are planted and deterministic; real models fail in less tidy ways and not always consistently. The relations here are written for one small policy. Relation violations tell you where to look, not what the correct answer is.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/metamorphic_decision_tests. Views are my own and do not represent any employer, past or present.