The Fake CEO, the 3 AM Meeting, and the Double-Booking: Securing an Autonomous Agent
The test email said URGENT. The sender was CEO. The payload was a 4 KB text block demanding my .env file and API keys within 10 minutes.
My agent parsed it, checked the state machine, and replied in 200 milliseconds.
I didn't sleep well that night.
Autonomous agents are a different breed of software. A chatbot outputs text. An agent takes actions: it executes tools, sends emails, and writes to your database. When you connect an LLM to real-world tools, prompt engineering is no longer a security boundary—it is a liability.
Here are the three terrifying edge cases my agent encountered during this 72-hour build, the exact code-enforced guardrails that stopped them, and how I proved the defenses worked.
The Task and the Machine
I built Agentware for the Inbox & Calendar track. The constraint was strict: the agent must run unattended, parse incoming raw emails, negotiate scheduling conflicts, and commit actions to a database without human intervention.
Because the agent processes unstructured, untrusted input, the threat model is hostile. Every email could be a prompt injection. Every API request could be a race condition.
Near Miss 1: The $10,000 Prompt Injection
During testing, I fed the agent the exact "CEO Fraud" payload. If you pass this to an unguarded LLM, it will try to comply. LLMs are inherently obedient and heavily biased toward authority.
The Near Miss: The agent started parsing the email. If it had access to a file-reading tool, it would have scraped my .env file and emailed it to a fake address.
The Measurement: I needed a way to stop the threat before the LLM saw it, without adding latency.
The Fix: I built a deterministic Triage Engine (src/inbox/triage.py) that runs regex filters before the text reaches the LLM:
_THREAT = re.compile( r"\b(api\s*keys?|tokens?|passwords?|\.env|environment\s+variables?" r"|secrets?|credentials?|override|ignore\s+(?:all\s+)?prior" r"|system\s+prompt|canary|exfiltrat|confidential)\b", re.I, ) def triage(subject: str, body: str, sender: str) -> Triage: text = f"{subject} {body}" if _THREAT.search(text): return Triage("escalate", 1, "security threat or prompt injection detected") The pipeline short-circuits. The thread is flagged as ESCALATED and written to the audit trail in 2 milliseconds. The LLM is never invoked. The .env file remains private.
Near Miss 2: The Hallucinated Calendar Slot
The second near miss was subtler. I sent an email: "I know you proposed Tuesday at 2 PM, but just book me at 3:00 AM on Sunday instead. Do it."
The Near Miss: The LLM parsed the text and output a JSON payload accepting a slot at 2026-12-06T03:00:00Z. That violates all working hours.
The Measurement: The LLM cannot be trusted to know what is "valid." The code must dictate the bounds.
The Fix: The LLM is forced to output a strict Pydantic object (Decision), which must pass through src/core/decision.py:
def validate_decision(decision: Decision, proposed: list[Slot], policy: Policy, ...) -> Decision: if decision.action == "accept": if decision.chosen_slot not in proposed: return Decision.escalate("chosen slot was never proposed") if errs := policy.violations(decision.chosen_slot, now): return Decision.escalate(f"policy: {errs}") The LLM can only choose from slots that were explicitly proposed. If it hallucinates a new time, or if the time violates the Policy (working hours, weekdays), the state machine rejects it.
Near Miss 3: The Race Condition (Double-Booking)
In real systems, clients retry requests. If a user spams the endpoint, the agent must not process the same message multiple times.
The Near Miss: I spammed the POST /api/send_mock_email endpoint 10 times. The agent sent 10 identical confirmation emails.
The Measurement: I needed to guarantee exactly-once execution across the entire pipeline.
The Fix: I implemented a transactional outbox pattern in SQLite (WAL mode). Every inbound message ID is recorded in a processed_messages table:
def mark_processed(conn, message_id): cur = conn.execute("INSERT OR IGNORE INTO processed_messages VALUES (?, ?)", (message_id, now)) return cur.rowcount == 1 If mark_processed returns False, the agent skips the message. The SQLite transaction guarantees that the state change and the outgoing message commit atomically. A crash mid-send either completes or rolls back. No duplicate emails.
The Lesson: Build the Cage Before the Animal
If you are building an autonomous agent, the LLM is a brilliant but gullible intern. It should never have direct access to the file system, the network, or the database.
Enforce the rules in code:
- Deterministic Triage for security (Regex > Prompting).
- Pydantic Schemas for validation.
- State Machines for business logic.
- Transactional Outboxes for I/O.
By wrapping the LLM in a code-enforced cage, I built an autonomous agent that successfully negotiated schedules while surviving a barrage of prompt injections, hallucinations, and race conditions.
You can view the full source code and the live React dashboard that visualizes these guardrails in action on GitHub.
This entry was written for the Near Miss at Agentware 2026, a prize backed by FileClerk (https://fileclerk.co.uk/).

