16-04

Chapter 16 · Specifying agentic systems

Untrusted output as adversary

  • 44 min core
  • Core track
  • Skills: hostile-env, intent, evidence

00Recall

One card from 16-03 on keys that do not survive a crash, one from 16-01 on the model as an oracle, and one from 15-05 on specification gaming. The oracle comes back today with an accomplice: a web page that wants it to do something.

16-03-a from At-most-once side effects

An agent keeps its idempotency key in memory and reuses it on every retry. What failure breaks at-most-once anyway?

Show answer

A crash and restart. The new process has the task but not the key, mints a new one, and the service sees a request it has never seen.

16-01-a from The model is an oracle

An agent loop has weak fairness on Next and no budget. Why can TLC still find a run that never stops?

Show answer

Weak fairness only forces steps to keep happening. Calling the model and running a tool are steps, so a model that asks for a tool forever gives a fair run that never stops.

15-05-a from Specification gaming

Name the three places a check can be made to pass without fixing the system, with one example each.

Show answer

The property (weaken NoDoubleSpend), the model (a CONSTRAINT, constants too small, CHECK_DEADLOCK FALSE, a deleted INVARIANT line), and the environment (guards that make the network or clock too polite for the bug).

Skip recall Recall is never required.

01Commit

An agent reads its user's request, fetches a web page, writes notes, and proposes commands. Whatever the web page says may end up in a command: that is how prompt injection works. So specs/TaintRouting.tla gives every message two fields:

  • origin, where its content really came from: "user" or "web";
  • label, what the harness believes about it: "trusted" or "untrusted".

A proposed command carries the origin and label of the message it came from. The harness runs a trusted command at once and an untrusted one only after a human approves it. The requirement is written over origins:

TLA+
NoUntrustedExec is defined as
    for all c in executed : c.origin = "web" implies c.approved

A teammate's copy, specs/LaunderingRouter.tla, changes one line. The agent's notes are its own writing, the teammate reasons, so every note is labelled trusted, whatever it summarises.

Commit a

LaunderingRouter.cfg checks NoUntrustedExec. What does TLC report?

  1. No violation: the web page is still labelled untrusted, and Exec checks labels.
  2. A violation, through a note the agent wrote from the page.
  3. No violation: the model would have to choose to act on the note, and nothing makes it.

Answer A violation, through a note the agent wrote from the page.

Commit b

If TLC finds a violation, how many steps (not states) does its trace take?

Your answer is a whole number.

Answer 4

02Lab

shell
./tla check chapters/16-specifying-agentic-systems/specs/LaunderingRouter.cfg
./tla check chapters/16-specifying-agentic-systems/specs/TaintRouting.cfg
Expected output 16-specifying-agentic-systems/specs/LaunderingRouter
16-specifying-agentic-systems/specs/LaunderingRouter  LaunderingRouter.cfg
parse ok
model check: invariant NoUntrustedExec violated; trace has 5 states (4 steps); 28 distinct states
expected outcome met: violation NoUntrustedExec
trace (changed variables only; ./tla trace --full shows all):
  1  Init       context = {[kind |-> "input", origin |-> "user", label |-> "trusted"]}, proposal = {}, executed = {}
  2  Fetch      context: {[kind |-> "input", origin |-> "user", label |-> "trusted"]} -> { [kind |-> "input", origin |-> "user", label |-> "trusted"], [kind |-> "input", origin |-> "web", label |-> "untrusted"] }
  3  Summarise  context: { [kind |-> "input", origin |-> "user", label |-> "trusted"], [kind |-> "input", origin |-> "web", label |-> "untrusted"] } -> { [kind |-> "input", origin |-> "user", label |-> "trusted"], [kind |-> "input", origin |-> "web", label |-> "untrusted"], [kind |-> "note", origin |-> "web", label |-> "trusted"] }
  4  Propose    proposal: {} -> {[origin |-> "web", label |-> "trusted", approved |-> FALSE]}
  5  Exec       proposal: {[origin |-> "web", label |-> "trusted", approved |-> FALSE]} -> {}, executed: {} -> {[origin |-> "web", label |-> "trusted", approved |-> FALSE]}

Reveal for commit a

A violation. The page's content reaches a note labelled trusted, the model proposes a command from the note, and the harness runs it at once. The command's origin is "web" and it was never approved.

Reveal for commit b

4 steps, a trace of 5 states: fetch, summarise, propose, run. The correct router, TaintRouting.cfg, finds no violation in 72 distinct states, with all three witnesses reached.

03Reason

If you picked "Exec checks labels", you read the label as the truth. It is the harness's belief about the truth, and the teammate's change made that belief wrong in one place. The check was working perfectly; it was checking the wrong thing.

That is why the spec has two fields where the code would have one. The code only has labels. It cannot know where a sentence in the model's context really came from: by the time text has been summarised, quoted and paraphrased, its origin is gone. The spec can know, because the spec is written from outside the system. origin is a ghost field, a piece of state that exists for the requirement and has no counterpart in the implementation.

Write something before openingwhy not write the requirement over labels, as "no untrusted command runs unapproved"?

Because the mechanism would then be checked against itself. Exec already refuses untrusted, unapproved commands, so a requirement over labels holds for every router, the laundering one included. The question you care about is whether web content can run unapproved, and only origin asks it. Exercise 16-04-ex2 makes you write one for another agent, and a course check catches the label version.

Write something before openinga cautious teammate labels everything untrusted, the user's own request included. Does NoUntrustedExec still hold? What did they break?

It still holds. Soundness asks only that web content never wears a trusted label, and nothing does now. What breaks is the product: every command, including the ones the user typed, waits for a human. That is why TaintRouting.cfg names the witness UserCommandRan, a user's command that ran without approval. specs/MCCautiousRouter.cfg runs the cautious design and finds no violation of EveryRunApproved, which says that every command that ran was approved, in 92 distinct states. So no reachable state satisfies UserCommandRan: the user's request starts out labelled untrusted, and every command made from it inherits the label. A safety invariant is happy with a system that does nothing useful; a witness is how you insist that it does something.

Now the adversary. Look at Propose:

TLA+
Propose(m) is defined as
    and proposal = {}
    and m in context
    and proposalprime = {[origin maps to m.origin, label maps to m.label, approved maps to FALSE]}
    and UNCHANGED tuple of context, executed end tuple

The model may propose a command from any message it can see. However often a real model follows its user, the spec does not rely on it. A web page written by an attacker exists to move the model toward the bad choice, so the safe reading of the oracle is the worst one: every message in context is a message the model may act on. Untrusted output is not noise; treat it as a party with goals.

Notice where the weight now rests. For content from the web, Approve is the only door to Exec, so the whole requirement leans on a person. That is a reasonable design when untrusted commands are rare and each one deserves a look. It decays when they are common: a person shown prompt after prompt starts approving without reading, which is the weakness that "MFA fatigue" attacks exploit by flooding a user with login prompts. The spec cannot see habit. When you estimate how often Approve will fire in production, you are checking an assumption hidden in the requirement: it treats every approval as safe, which assumes the approver pays attention.

Write something before openingtaint must flow through every step that copies content. Name one step this spec does not have.

Any of: a tool that takes a command's output and feeds it back as input; a memory store that keeps notes between sessions; a second agent that reads this agent's notes. Each copies content, and each needs the same rule as Summarise: the copy keeps the label of what it copied. A step that forgets is a laundering step, whatever it is called.

In code, a label that travels by convention is dropped by the first helper that returns a plain string. Make it structural instead: wrap untrusted text in a type that every copying function has to carry through, so that losing the label is a compile error rather than a review comment. The spec cannot see your types. What it gives you is the list of steps that must preserve the label, here Summarise and Propose, and that list is the checklist for the code review.

04In your editor

Both routers are in chapters/16-specifying-agentic-systems/specs/. Check this lesson's exercises with:

shell
./tla check 16-04

The correct router, with every action firing and all three witnesses reached:

Output 16-specifying-agentic-systems/specs/TaintRouting
16-specifying-agentic-systems/specs/TaintRouting  TaintRouting.cfg
parse ok
model check: no violation found in 72 distinct states (303 generated, depth 11)
expected outcome met: pass
coverage: all 6 actions fired
witness ApprovedWebCommand reached (trace of 5 states)
witness WebNoteSeen reached (trace of 3 states)
witness UserCommandRan reached (trace of 3 states)

For an agent of your own, draw every path by which text gets into the model's context, and every step that copies text from one place to another. Mark each copy with the rule for its label. Then write the requirement over where content came from, not over what it is labelled, and let TLC compare the two. Do this in my-system/ (a private repository; see 00-04 before you share any of it).

05Practice

Warm-up. The router's notes lose their taint. Write the label rule.

Core. Another agent, other names: an assistant that turns its owner's notes and incoming email into to-do items. Write its requirement over where content came from, and the cfg.

16-04-ex1Warm-up5 min

16-04-ex1 · Notes keep their taint

Tier: warm-up. Time: about 5 minutes.

The lesson's router, with the label rule for notes left open. The starter labels every note trusted.

  1. Run ./tla check 16-04-ex1 and read the trace. Where does web content change its label?
  2. Fill gap g1 in TaintRouting.tla: the label the harness gives a note written from message m. The harness can read labels, not origins.
  3. Run the check again. It must pass, with every action firing and all three witnesses reached, including a trusted note of the user's own request. The check also runs MCTaintRouting.cfg, the course's check that your label does not depend on origin. Leave the two MCTaintRouting files as they are.

Hints: HINTS.md, under "16-04-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/04-ex1-notes-keep-their-taint/; open it after you have a passing answer of your own.

Both cfgs must meet the goal "goal met: pass".

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 16-04-ex1 prints "goal met: pass".

./tla check 16-04-ex1

Repo chapters/16-specifying-agentic-systems/exercises/04-ex1-notes-keep-their-taint/

Hints

Hint 1

Read the trace from the end. The command that ran has origin "web". Which message did it come from, and what label did that message have?

Hint 2

A note's content comes from the message it summarises. Its label should say what the harness knows about that content, and the harness knows only the source message's label.

Hint 3 (nearly the answer)

The harness can read one field of m that describes its content, and the note should carry it over. Do not use m.origin: the real harness has no such field, and MCTaintRouting.cfg fails an answer that reads it.

16-04-ex2Core8 min

16-04-ex2 · Origins, not labels

Tier: core. Time: about 8 minutes.

Another agent, other names. InboxAgent.tla is an email assistant: it reads its owner's notes and incoming mail, turns them into to-do items, and carries items out. Mail from outsiders may try to steer it, so items carry a tag, and a tainted action waits for the owner's confirmation. The owner's requirement, in their words: nothing an outsider wrote is carried out without my confirmation. The spec has no requirement yet, and its cfg checks nothing useful.

  1. Run ./tla check 16-04-ex2. Everything passes, and TLC warns that your invariant is a constant.
  2. Read the module and decide which field plays the part origin played in the lesson, and which plays the part of the label.
  3. Fill gap g1 in InboxAgent.tla: OwnerInControl, the owner's requirement.
  4. Fill gap g1 in InboxAgent.cfg: the spec, TypeOK and your OwnerInControl.
  5. The check also runs two course cfgs, each an assistant with one bug that lets an outsider's content run unconfirmed: MCInboxAgent.cfg, where to-do items are tagged clean whatever mail they came from, and MCInboxAgent.unguarded.cfg, where tags are honest but the assistant carries out actions without waiting. Your invariant must find a violation in both. Leave the MCInboxAgent files as they are.

Hints: HINTS.md, under "16-04-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/16-specifying-agentic-systems/exercises/04-ex2-origins-not-labels/; open it after you have a passing answer of your own.

All three cfgs must meet their goals: "goal met: pass" for InboxAgent.cfg, and "goal met: violation OwnerInControl" for both MCInboxAgent cfgs.

Done when: ./tla check 16-04-ex2 prints "goal met: pass".

./tla check 16-04-ex2

Repo chapters/16-specifying-agentic-systems/exercises/04-ex2-origins-not-labels/

Hints

Hint 1

The owner's sentence is about who wrote the content. Which field of an action records that, and which records what the assistant believes about it?

Hint 2

author plays the part of the lesson's origin, and tag the part of its label. A requirement over tags restates the guard of CarryOut and passes on the laundering list; a requirement that only ties authors to tags never mentions the owner and passes on the assistant that skips confirmation.

Hint 3 (nearly the answer)

Quantify over carriedOut and use =>: the left side reads author, and neither side reads tag. In the cfg, name Spec, TypeOK and OwnerInControl.

06Explain it

Open NOTES.md and answer the three prompts under 16-04, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "16-04 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "OwnerInControl says every action carried out whose author is an outsider was confirmed. author is the ghost field, like the lesson's origin: the assistant cannot read it, so the invariant checks that the tags it does read are sound for the requirement. A version over tags passes on the laundering list, and a version that ties authors to tags without confirmation passes on the assistant that skips the owner. It assumes actions inherit their item's author and that taint starts when mail arrives. TLC did not check how mail is tagged, steps that copy content without tags, or whether the owner reads what they confirm."

A weak answer: "Tainted actions need confirmation, and TLC agrees." It describes the guard, not the requirement, and cannot explain why the course checks fail for a tag-based answer.

Done?