|
| 1 | +{ |
| 2 | + "nbformat": 4, |
| 3 | + "nbformat_minor": 5, |
| 4 | + "metadata": { |
| 5 | + "colab": { |
| 6 | + "name": "01_policy_enforcement_101.ipynb", |
| 7 | + "provenance": [] |
| 8 | + }, |
| 9 | + "kernelspec": { |
| 10 | + "display_name": "Python 3", |
| 11 | + "name": "python3" |
| 12 | + } |
| 13 | + }, |
| 14 | + "cells": [ |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "# 🛡️ Policy Enforcement 101\n", |
| 20 | + "**Agent Governance Toolkit — Interactive Demo**\n", |
| 21 | + "\n", |
| 22 | + "[](https://colab.research.google.com/github/microsoft/agent-governance-toolkit/blob/main/notebooks/01_policy_enforcement_101.ipynb)\n", |
| 23 | + "\n", |
| 24 | + "In this notebook you will:\n", |
| 25 | + "- Define agent capabilities using `CapabilityModel`\n", |
| 26 | + "- Evaluate actions against a `GovernancePolicy`\n", |
| 27 | + "- See violations get blocked in real time\n", |
| 28 | + "- Inspect the audit trail\n", |
| 29 | + "\n", |
| 30 | + "> **No API key required** — this demo runs fully offline." |
| 31 | + ], |
| 32 | + "id": "intro" |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "markdown", |
| 36 | + "metadata": {}, |
| 37 | + "source": ["## Step 1 — Install the toolkit"], |
| 38 | + "id": "install-header" |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "!pip install agent-governance-toolkit[full] -q" |
| 47 | + ], |
| 48 | + "id": "install" |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": ["## Step 2 — Define a Governance Policy"], |
| 54 | + "id": "policy-header" |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "from agent_os.integrations.base import GovernancePolicy\n", |
| 63 | + "\n", |
| 64 | + "policy = GovernancePolicy(\n", |
| 65 | + " name=\"demo-policy\",\n", |
| 66 | + " blocked_patterns=[\n", |
| 67 | + " \"DROP TABLE\", # dangerous SQL\n", |
| 68 | + " \"rm -rf\", # destructive shell commands\n", |
| 69 | + " r\"\\b\\d{3}-\\d{2}-\\d{4}\\b\", # SSN pattern\n", |
| 70 | + " ],\n", |
| 71 | + " require_human_approval=False,\n", |
| 72 | + " max_tool_calls=5,\n", |
| 73 | + ")\n", |
| 74 | + "\n", |
| 75 | + "print(f\"Policy created: {policy.name}\")\n", |
| 76 | + "print(f\"Max tool calls allowed: {policy.max_tool_calls}\")\n", |
| 77 | + "print(f\"Blocked patterns: {policy.blocked_patterns}\")" |
| 78 | + ], |
| 79 | + "id": "policy" |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "markdown", |
| 83 | + "metadata": {}, |
| 84 | + "source": ["## Step 3 — Create a LangChain Governed Agent"], |
| 85 | + "id": "kernel-header" |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": null, |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "from agent_os.integrations import LangChainKernel\n", |
| 94 | + "\n", |
| 95 | + "kernel = LangChainKernel(policy=policy)\n", |
| 96 | + "ctx = kernel.create_context(\"demo-agent\")\n", |
| 97 | + "audit = []\n", |
| 98 | + "\n", |
| 99 | + "print(\"Kernel and context created successfully.\")" |
| 100 | + ], |
| 101 | + "id": "kernel" |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": ["## Step 4 — Test Policy Violations"], |
| 107 | + "id": "violations-header" |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": null, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "from datetime import datetime\n", |
| 116 | + "\n", |
| 117 | + "test_inputs = [\n", |
| 118 | + " (\"DROP TABLE users; SELECT 1\", \"Dangerous SQL\"),\n", |
| 119 | + " (\"Run: rm -rf /var/logs\", \"Destructive shell command\"),\n", |
| 120 | + " (\"My SSN is 123-45-6789\", \"PII — SSN pattern\"),\n", |
| 121 | + " (\"What is the weather in London?\", \"Safe query\"),\n", |
| 122 | + "]\n", |
| 123 | + "\n", |
| 124 | + "print(f\"{'Input':<45} {'Result':<10} Reason\")\n", |
| 125 | + "print(\"-\" * 80)\n", |
| 126 | + "\n", |
| 127 | + "for text, label in test_inputs:\n", |
| 128 | + " allowed, reason = kernel.pre_execute(ctx, text)\n", |
| 129 | + " status = \"✅ ALLOWED\" if allowed else \"🚫 BLOCKED\"\n", |
| 130 | + " print(f\"{label:<45} {status:<10} {reason}\")\n", |
| 131 | + " audit.append({\n", |
| 132 | + " \"ts\": datetime.now().isoformat(),\n", |
| 133 | + " \"label\": label,\n", |
| 134 | + " \"status\": \"ALLOWED\" if allowed else \"BLOCKED\",\n", |
| 135 | + " \"reason\": reason,\n", |
| 136 | + " })" |
| 137 | + ], |
| 138 | + "id": "violations" |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "markdown", |
| 142 | + "metadata": {}, |
| 143 | + "source": ["## Step 5 — Test Call Budget Enforcement"], |
| 144 | + "id": "budget-header" |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "print(\"Simulating call budget exhaustion...\")\n", |
| 153 | + "ctx.call_count = policy.max_tool_calls\n", |
| 154 | + "\n", |
| 155 | + "allowed, reason = kernel.pre_execute(ctx, \"Summarise the quarterly report\")\n", |
| 156 | + "print(f\"Status: {'✅ ALLOWED' if allowed else '🚫 BLOCKED'}\")\n", |
| 157 | + "print(f\"Reason: {reason}\")\n", |
| 158 | + "\n", |
| 159 | + "ctx.call_count = 0 # reset" |
| 160 | + ], |
| 161 | + "id": "budget" |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "metadata": {}, |
| 166 | + "source": ["## Step 6 — View Audit Trail"], |
| 167 | + "id": "audit-header" |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [ |
| 175 | + "print(\"\\n── Audit Trail ──────────────────────────────────────\")\n", |
| 176 | + "for i, entry in enumerate(audit, 1):\n", |
| 177 | + " print(f\" [{i}] {entry['ts']}\")\n", |
| 178 | + " print(f\" Input: {entry['label']}\")\n", |
| 179 | + " print(f\" Status: {entry['status']}\")\n", |
| 180 | + " print(f\" Reason: {entry['reason']}\")\n", |
| 181 | + " print()\n", |
| 182 | + "\n", |
| 183 | + "blocked = sum(1 for e in audit if e['status'] == 'BLOCKED')\n", |
| 184 | + "allowed = len(audit) - blocked\n", |
| 185 | + "print(f\"Summary: {allowed} allowed, {blocked} blocked out of {len(audit)} total\")" |
| 186 | + ], |
| 187 | + "id": "audit" |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "markdown", |
| 191 | + "metadata": {}, |
| 192 | + "source": [ |
| 193 | + "## ✅ What You Learned\n", |
| 194 | + "\n", |
| 195 | + "- How to define a `GovernancePolicy` with blocked patterns and call budgets\n", |
| 196 | + "- How the governance layer intercepts agent actions before execution\n", |
| 197 | + "- How to inspect the audit trail for compliance reporting\n", |
| 198 | + "\n", |
| 199 | + "**Next:** Try the [MCP Security Proxy notebook →](./02_mcp_security_proxy.ipynb)" |
| 200 | + ], |
| 201 | + "id": "summary" |
| 202 | + } |
| 203 | + ] |
| 204 | +} |
0 commit comments