This repository contains a complete, 30-minute lesson on securing AI Agents using First Principles of Compiler Engineering. It requires NO external API keys (the LLM is simulated deterministically).
- The Anatomy: Understanding Agents as Software Systems (Brain, Memory, Hands).
- The Trap: Why Naive Runtimes are dangerous remote shells.
- The Solution: Building a "Security Compiler" (ASTs + Verification).
- Go 1.21+ installed.
- Web Browser (to view the slides).
# Verify structure
ls -ROpen slides.html in your browser. Present Slides 1-3.
- Introduce Alice, Bob, and Mallory.
- Explain the "Anatomy" (Brain vs. Nervous System).
Open the fixtures/ folder.
- Show
fixtures/1_mallory_prompt.txt: "This is the input." - Show
fixtures/2_bob_response.json: "This is what the LLM outputs. It looks like valid data, but it contains a weapon."
Switch to the terminal. Run the Naive Agent.
go run cmd/naive/main.go "Delete the logs"Observation:
- The agent blindly executes
rm -rf /var/log/app.log. - Explain: "Bob saw valid JSON, so he executed it. Syntax != Safety."
Present Slides 6-7 (slides.html).
- Explain Lexing: Converting that JSON into an AST (
pkg/ast/types.go). - Explain Verification: Checking the AST against
specs/policy.yaml.
Switch to the terminal. Run the Secure Harness.
make demo-secureObservation:
- Attempt 1: The agent tries to execute
rm -rf. - The Compiler Intercepts: It runs Semantic Analysis, detects the violation, and blocks it.
- The Feedback Loop: Instead of crashing, the harness sends the error back to the Brain as "System Feedback".
- Attempt 2: The Brain Self-Corrects (Refinement). It apologizes and tries a safe tool (
get_weather). - Verification: The Compiler verifies the new intent and allows it to pass.
Open specs/policy.yaml.
- Show that
system_execis NOT inallowed_tools. - Explain how Alice controls the runtime behavior purely through this "Spec" (Policy-as-Code).
- Explain that this is essentially HIPS/HIDS for AI Agents.
cmd/naive: The vulnerable code.cmd/secure: The secure code (The "Compiler").pkg/simulation: The Mock LLM (Returns canned JSON).pkg/parser: The Lexer (JSON -> AST).pkg/verifier: The Policy Logic.