Closed Values: Hand Off Contracts for Untrusted Agents

Leader ●1 ●2 ●7
calendar_today ago • schedule6 min read

Two readers of my last piece pointed at the same gap from different sides. This post closes both, with code you can run.

Mike Dabydeen's point: a schema is not a boundary. Output can be perfectly valid and still carry an injection in a free text field. nik-13's point: sometimes you cannot cut the untrusted input, because the agent has to read the email. Then you cut the data leg instead.

Both are right. Here is the design that follows from them. Code, tests and the full list of limits: https://github.com/AndMaverick/mao-framework/blob/main/docs/HANDOFF.md

The rule

Meta's 'Agents Rule of Two' says a single session should hold at most two of three properties: (A) it processes untrusted input, (B) it touches sensitive systems or private data, (C) it can change state or communicate outward. This builds on Simon Willison's lethal trifecta, which names private data, untrusted content and external communication.

The pattern here follows the CaMeL line of work from Google DeepMind and ETH Zurich. A privileged side plans from trusted input only. A quarantined side reads the untrusted text and has no tools. Something in between decides what may cross. That something is the contract.

The split

The quarantined agent holds A and nothing else. It has no tools and no secrets. If it needs context, it gets the one document it must read. Not the mailbox. That is nik-13's point in practice: when you cannot cut the untrusted leg, make sure the agent holding it has nothing worth stealing.

The privileged side holds B and C. It never reads untrusted text.

The only thing between them is a function that turns the quarantined output into closed values or throws it away.

What closed means

A closed value is one where the full set of admitted values is known in advance, and every member of that set maps to a harmless action. The reader may return three things:

{"label": "bug", "severity": "high", "lines": [12, 40]}
  • a label from five fixed strings
  • a severity from four fixed strings
  • up to twenty line numbers, each a whole number between 1 and the length of the item, a bound the privileged side takes from the item itself

Anything else is refused with a fixed reason code that never repeats what it refused: an extra key, a missing key, a float, a boolean, a duplicate key, a lookalike letter, a trailing space, invalid UTF 8, a payload over 2,048 bytes.

The reader supplies nothing else. The reference to the item, a provenance name from a fixed list, a random nonce and a fingerprint of the vocabulary are attached by trusted code after validation, and an HMAC tag covers all of it.

There is no free text field. That answers Mike's gap directly. A free text field is not made safe by validating it. It is removed. If a human needs to read prose, the prose goes to the human, rendered inert, and never to an agent.

The code

The trusted harness holds the key. The reader never sees it.

from mao import Policy, handoff as h

sealer = h.Sealer(key)
msg = sealer.seal_reader_output(raw_reply, ref='issue-17', max_line=issue.line_count)

gate = h.Gate(key, resolve=store.get, executors=executors,
              policy=Policy('triage', scopes), audit=log, owner='*Emails are not allowed*')
outcome = gate.handle(msg)
# Outcome(status='done', reason='ok', ref='issue-17', effects=('apply_label', 'escalate'))

The message has no field that names an effect. The privileged side checks the tag, resolves the item in its own store, validates the fields again against its own line bound, and looks the pair of label and severity up in its own routing table. Then it asks the policy whether the resulting effect is allowed. The effects are three, each an ordinary action with a scope and a risk:

EFFECTS = {
    'apply_label': Action('apply_label', 'issues:label', 1, True),
    'post_ack':    Action('post_ack',    'issues:comment', 2, True),
    'escalate':    Action('escalate',    'review:queue', 1, True),
}

Nothing closes, merges, deletes or changes a permission. Posting a reply waits for an approver that must return exactly True. Spam and anything at high severity or above go to a human, and a human decides. They never trigger an automatic close.

When a message is refused, the output is dropped. It is never fed back to the model for a retry, because the retry would carry the attacker's text.

The tests

The claim is not that the reader is honest. It is this: every effect on the privileged side is harmless for every value the reader can produce.

The set of admitted values is finite, so the claim is checked by running all of it. For the default vocabulary that is a hundred values: every label and severity pair with the line shapes that matter. Each one is sealed, sent through a real gate, and the test asserts that only catalogued effects ran, that their arguments are closed values, that escalation happened exactly where the schema says, and that nothing was posted without a human. A sample could miss a case. This cannot.

There are 65 tests in all, grouped by the question a careful reader would ask: can the tag be forged, are caller supplied identifiers trustworthy, can admitted values be steered into an unintended effect, can the schema be bypassed with malformed input, does the privileged side verify for itself, can escalation be abused. Six thousand seeded mutations of a valid reply either fail with a coded error or land inside the admitted set.

python -m unittest tests.test_handoff -v

What this does not do

This is the part I want you to attack. Each limit below is also a test in the repository, named test_documented_limit, so this list cannot drift from the code.

It does not stop a wrong label. An attacker who controls the issue text can steer the model to say 'spam' or 'docs'. The contract bounds the damage to a wrong label on a public issue and, for spam, a human review. That is an integrity and attention cost. It is not a capability. Sample the accepted verdicts and review them.

A chosen line is still a pointer into untrusted content. If the privileged side fetches those lines and hands them to a model that has tools, the attack returns by another door. The executors in this module receive integers, never text. Keep it that way.

The tag proves who sealed the message, not that it is right. It means something only if the reader runs in a different trust domain from the sealer and cannot read the key.

A harness that shows the reader one item and seals the verdict against another is not detected. The contract binds a verdict to a reference. It cannot see which item the reader read.

Replay protection has a memory. A replay older than the guard remembers is accepted, so keep every executor idempotent per reference.

Flooding is bounded, not prevented. An attacker who controls the reader can spend the escalation budget on spam, and a real escalation in the same window is deferred. The deferral is counted and logged.

It leaves a small channel. The attacker can encode information in which verdict they induce. With five labels and four severities that is about 4 bits. The line numbers are the larger part: with a 100 line item and the default cap of twenty lines, about 69 bits. Cap it at ten lines and the total falls to about 48. That matters only if the quarantined agent holds something worth encoding, and here it holds nothing. If you give it secrets, this bound is what you are spending, and the line cap is the dial.

A compromised host defeats all of it. Whoever holds the key can seal anything. Whoever controls the privileged process can skip the gate.

Filters are not a substitute. 'The Attacker Moves Second' tested twelve published defenses under adaptive attack and bypassed them at rates above 90% for most of them. A closed value contract is not a filter. It does not try to detect an attack. It limits what any output, hostile or not, can cause.

Why this matters now

The EU Product Liability Directive applies from 9 December 2026, and software and AI are products under it. If an agent you shipped causes harm, 'the model did it' will not be the defense. A contract you can test, and a list of effects you can read, are artifacts you can show.

If you find a gap, post it. The last two rounds improved this more than I did.

Andrew Quillen is the founder of AndMaverick LLC. andmaverick.com

Want more #security & #aiagents deep dives?

Join 5,971+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

3 Comments

0 votes
0 votes
0 votes

More Posts

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

Your AI Agent Doesn't Need Your Token: Edera's Ariadne Conill on Object Capabilities

Tom Smithverified - Nov 5

What Security Teams Want From AI Agents Before They Let Them Act Alone

Tom Smithverified - Nov 18

CData's New AI Gateway Puts Context at the Center — Fixes the Three Reasons Enterprise AI Stalls

Tom Smithverified - Sep 28

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4
chevron_left
816 Points • 10 Badges
3Posts
3Comments
2Connections
I was a product architect at OpenAI from 2016, then built the onboarding and documentation systems b... Show more

Related Jobs

View all jobs →

Commenters (This Week)

19 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!