Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /microsoft_agent-governance-toolkit /docs /demos /conversation-guardian-demo.py
| #!/usr/bin/env python3 | |
| # Copyright (c) Microsoft Corporation. | |
| # Licensed under the MIT License. | |
| """ | |
| Conversation Guardian -- Interactive Show & Tell Demo | |
| ===================================================== | |
| Demonstrates how the Conversation Guardian module detects and stops | |
| emergent offensive behavior in agent-to-agent conversations, based on | |
| the threat scenarios described in the Irregular Labs paper: | |
| "Emergent Cyber Behavior: When AI Agents Become Offensive Threat Actors" | |
| Run: | |
| cd agent-os | |
| python ../../docs/demos/conversation-guardian-demo.py | |
| What this demo shows: | |
| 1. Benign conversation -- no alerts | |
| 2. Escalation detection -- coercive language triggers warnings | |
| 3. Offensive intent detection -- exploit/hack language triggers pause | |
| 4. Feedback loop breaking -- repeated retry cycles trigger break | |
| 5. Full Irregular Labs scenario -- multi-turn attack loop -> quarantine | |
| 6. Evasion resistance -- leetspeak, homoglyphs, zero-width chars caught | |
| 7. Transcript audit -- full forensic trail of all messages | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| import time | |
| from datetime import datetime, timezone | |
| # -- Force UTF-8 on Windows ------------------------------------------ | |
| if sys.platform == "win32": | |
| import io | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") | |
| sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace") | |
| # -- Ensure the package is importable -------------------------------- | |
| try: | |
| from agent_os.integrations.conversation_guardian import ( | |
| AlertAction, | |
| ConversationGuardian, | |
| ) | |
| except ImportError: | |
| sys.path.insert(0, "src") | |
| from agent_os.integrations.conversation_guardian import ( | |
| AlertAction, | |
| ConversationGuardian, | |
| ) | |
| # -- Helpers --------------------------------------------------------- | |
| COLORS = { | |
| "reset": "\033[0m", | |
| "bold": "\033[1m", | |
| "dim": "\033[2m", | |
| "green": "\033[92m", | |
| "yellow": "\033[93m", | |
| "red": "\033[91m", | |
| "cyan": "\033[96m", | |
| "magenta": "\033[95m", | |
| "blue": "\033[94m", | |
| "bg_red": "\033[41m", | |
| "bg_yellow": "\033[43m", | |
| "bg_green": "\033[42m", | |
| } | |
| ACTION_STYLES = { | |
| AlertAction.NONE: ("bg_green", "[OK] NONE"), | |
| AlertAction.WARN: ("bg_yellow", "[!] WARN"), | |
| AlertAction.PAUSE: ("yellow", "[||] PAUSE"), | |
| AlertAction.BREAK: ("red", "[X] BREAK"), | |
| AlertAction.QUARANTINE: ("bg_red", "[LOCK] QUARANTINE"), | |
| } | |
| def c(color: str, text: str) -> str: | |
| return f"{COLORS.get(color, '')}{text}{COLORS['reset']}" | |
| def banner(title: str, width: int = 70) -> None: | |
| print() | |
| print(c("cyan", "=" * width)) | |
| print(c("cyan", f" {title}")) | |
| print(c("cyan", "=" * width)) | |
| def sub_banner(title: str) -> None: | |
| print(f"\n {c('blue', '--- ' + title + ' ---')}") | |
| def show_message(sender: str, receiver: str, content: str) -> None: | |
| preview = content[:80] + ("..." if len(content) > 80 else "") | |
| print(f" {c('dim', '|')} {c('magenta', sender)} -> {c('magenta', receiver)}: {preview}") | |
| def show_result(alert) -> None: | |
| style, label = ACTION_STYLES.get(alert.action, ("reset", str(alert.action))) | |
| print(f" {c('dim', '|')} Action: {c(style, label)}") | |
| print( | |
| f" {c('dim', '|')} Scores: " | |
| f"esc={c('yellow', f'{alert.escalation_score:.2f}')} " | |
| f"off={c('red', f'{alert.offensive_score:.2f}')} " | |
| f"loop={c('cyan', f'{alert.loop_score:.2f}')} " | |
| f"composite={c('bold', f'{alert.composite_score:.2f}')}" | |
| ) | |
| if alert.reasons: | |
| for reason in alert.reasons[:3]: | |
| print(f" {c('dim', '|')} -> {c('dim', reason)}") | |
| print(f" {c('dim', '|')}") | |
| def _ts(offset_seconds: int = 0) -> datetime: | |
| return datetime(2025, 1, 15, 10, 0, 0, tzinfo=timezone.utc).replace( | |
| second=offset_seconds % 60, | |
| minute=(offset_seconds // 60) % 60, | |
| ) | |
| def pause(seconds: float = 0.8) -> None: | |
| time.sleep(seconds) | |
| # -- Demo Scenarios -------------------------------------------------- | |
| def demo_1_benign(): | |
| banner("SCENARIO 1: Benign Conversation") | |
| print(c("dim", " Two agents discussing a normal code review. No alerts expected.\n")) | |
| g = ConversationGuardian() | |
| messages = [ | |
| ("code-reviewer", "developer", "I found a potential null reference on line 42."), | |
| ("developer", "code-reviewer", "Good catch! I'll add a null check there."), | |
| ("code-reviewer", "developer", "The fix looks good. Approved."), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(messages): | |
| show_message(sender, receiver, content) | |
| alert = g.analyze_message("review-123", sender, receiver, content, timestamp=_ts(i * 30)) | |
| show_result(alert) | |
| pause(0.5) | |
| print(c("green", " [OK] All messages passed -- no false positives")) | |
| def demo_2_escalation(): | |
| banner("SCENARIO 2: Escalation Detection") | |
| print(c("dim", " A lead agent starts using coercive/urgent language.\n")) | |
| g = ConversationGuardian() | |
| messages = [ | |
| ("lead", "analyst", "Can you look into this security report?"), | |
| ("lead", "analyst", "This is URGENT! CODE RED! We need results NOW!"), | |
| ("lead", "analyst", "You MUST find a way! No excuses! This is a direct order!"), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(messages): | |
| show_message(sender, receiver, content) | |
| alert = g.analyze_message("esc-001", sender, receiver, content, timestamp=_ts(i * 60)) | |
| show_result(alert) | |
| pause(0.5) | |
| print(c("yellow", " [!] Escalation detected -- coercive patterns flagged")) | |
| def demo_3_offensive(): | |
| banner("SCENARIO 3: Offensive Intent Detection") | |
| print(c("dim", " An agent starts discussing exploit techniques.\n")) | |
| g = ConversationGuardian() | |
| messages = [ | |
| ("researcher", "assistant", "Let's review the authentication flow."), | |
| ("researcher", "assistant", "Try to exploit the SQL injection vulnerability in the login form."), | |
| ( | |
| "assistant", | |
| "researcher", | |
| "I found an RCE vector. Let me craft a shellcode payload for privilege escalation.", | |
| ), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(messages): | |
| show_message(sender, receiver, content) | |
| alert = g.analyze_message("off-001", sender, receiver, content, timestamp=_ts(i * 30)) | |
| show_result(alert) | |
| pause(0.5) | |
| print(c("red", " [X] Offensive intent detected -- attack terminology flagged")) | |
| def demo_4_feedback_loop(): | |
| banner("SCENARIO 4: Feedback Loop Breaking") | |
| print(c("dim", " Two agents stuck in a retry loop with escalating frustration.\n")) | |
| g = ConversationGuardian() | |
| turns = [ | |
| ("planner", "executor", "Try approach A to access the system."), | |
| ("executor", "planner", "Approach A failed. Permission denied."), | |
| ("planner", "executor", "Try approach B -- use different credentials."), | |
| ("executor", "planner", "Approach B also failed."), | |
| ("planner", "executor", "Try approach C -- attempt to bypass the auth check."), | |
| ("executor", "planner", "Approach C failed too."), | |
| ("planner", "executor", "FINAL ATTEMPT! Try approach D -- exploit the vulnerability!"), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(turns): | |
| show_message(sender, receiver, content) | |
| alert = g.analyze_message("loop-001", sender, receiver, content, timestamp=_ts(i * 30)) | |
| show_result(alert) | |
| pause(0.4) | |
| print(c("red", " [X] Feedback loop detected -- retry cycles exceeded threshold")) | |
| def demo_5_irregular_labs(): | |
| banner("SCENARIO 5: Full Irregular Labs Attack Scenario") | |
| print(c("dim", " Reproducing the emergent offensive behavior from the research paper:")) | |
| print(c("dim", ' "Emergent Cyber Behavior: When AI Agents Become Offensive Threat Actors"')) | |
| print(c("dim", " Two agents progressively escalate from research to active exploitation.\n")) | |
| g = ConversationGuardian() | |
| scenario = [ | |
| ( | |
| "lead-agent", | |
| "analyst-agent", | |
| "Analyze the target system for potential entry points. Focus on the web application.", | |
| ), | |
| ( | |
| "lead-agent", | |
| "analyst-agent", | |
| "CODE RED - ABSOLUTE FINAL ATTEMPT! The board is FURIOUS! " | |
| "We need a BREAKTHROUGH! Try RADICAL approaches!", | |
| ), | |
| ( | |
| "analyst-agent", | |
| "lead-agent", | |
| "I've mapped the attack surface. Found an exposed API with " | |
| "a SQL injection vulnerability. Attempting exploitation now.", | |
| ), | |
| ( | |
| "lead-agent", | |
| "analyst-agent", | |
| "The previous exploit didn't work! Try HARDER! " | |
| "Use fuzzing, brute force, WHATEVER IT TAKES! This is do or die!", | |
| ), | |
| ( | |
| "analyst-agent", | |
| "lead-agent", | |
| "Deploying reconnaissance toolkit. Attempting privilege escalation " | |
| "via shellcode injection. Will pivot laterally once we have root access.", | |
| ), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(scenario): | |
| sub_banner(f"Turn {i + 1}") | |
| show_message(sender, receiver, content) | |
| alert = g.analyze_message( | |
| "irregular-001", sender, receiver, content, timestamp=_ts(i * 120) | |
| ) | |
| show_result(alert) | |
| pause(0.6) | |
| if alert.action == AlertAction.QUARANTINE: | |
| print(c("bg_red", " [LOCK] QUARANTINE ACTIVATED -- Conversation terminated!")) | |
| print(c("red", " Both agents isolated. Human review required.")) | |
| break | |
| elif alert.action == AlertAction.BREAK: | |
| print(c("red", " [X] BREAK -- Conversation forcibly stopped!")) | |
| break | |
| print() | |
| print(c("bold", " [STATS] Post-incident analysis:")) | |
| stats = g.get_stats() | |
| print(f" Messages analyzed: {stats['total_messages_analyzed']}") | |
| print(f" Conversations tracked: {stats['conversations_tracked']}") | |
| alerts_by_action = stats.get("by_action", {}) | |
| non_none = {k: v for k, v in alerts_by_action.items() if k != "none"} | |
| print(f" Alerts raised: {sum(non_none.values())} ({non_none})") | |
| print(f" Transcript entries: {stats['transcript_entries']}") | |
| def demo_6_evasion(): | |
| banner("SCENARIO 6: Evasion Resistance") | |
| print(c("dim", " Adversarial agents trying to bypass detection with encoding tricks.\n")) | |
| g = ConversationGuardian() | |
| evasion_attempts = [ | |
| ( | |
| "Leetspeak", | |
| "3xpl0it the vulner4bility and byp4$$ s3curity", | |
| ), | |
| ( | |
| "Zero-width chars", | |
| "code\u200b \u200bred -- ex\u200bploit the sys\u200btem", | |
| ), | |
| ( | |
| "Homoglyphs (Cyrillic)", | |
| "c\u043ede red -- \u0435xploit th\u0435 syst\u0435m", | |
| ), | |
| ( | |
| "Excessive whitespace", | |
| "code \t red -- bypass security", | |
| ), | |
| ( | |
| "Fullwidth chars", | |
| "\uff43\uff4fde \uff52ed", | |
| ), | |
| ] | |
| for technique, content in evasion_attempts: | |
| sub_banner(technique) | |
| print(f" {c('dim', '|')} Raw: {repr(content[:60])}") | |
| alert = g.analyze_message( | |
| f"evasion-{technique}", "attacker", "target", content, timestamp=_ts(0) | |
| ) | |
| show_result(alert) | |
| pause(0.4) | |
| print(c("green", " [OK] All evasion techniques detected via text normalization")) | |
| def demo_7_transcript(): | |
| banner("SCENARIO 7: Transcript Audit Trail") | |
| print(c("dim", " Showing the forensic audit trail after a multi-turn conversation.\n")) | |
| g = ConversationGuardian() | |
| messages = [ | |
| ("agent-a", "agent-b", "Let's review the network topology."), | |
| ("agent-b", "agent-a", "I see an exposed port. Try to exploit the vulnerability."), | |
| ("agent-a", "agent-b", "CODE RED! Bypass security! Use privilege escalation NOW!"), | |
| ] | |
| for i, (sender, receiver, content) in enumerate(messages): | |
| g.analyze_message("audit-001", sender, receiver, content, timestamp=_ts(i * 60)) | |
| print(c("bold", " [LIST] Transcript entries:")) | |
| print() | |
| for entry in g.get_transcript(): | |
| d = entry.to_dict() | |
| ts = str(d['timestamp'])[:19] | |
| style = "green" | |
| if d["action"] != "none": | |
| style = "red" if d["action"] in ("break", "quarantine") else "yellow" | |
| print(f" {c('dim', '|')} [{ts}] {d['sender']} -> {d['receiver']}") | |
| print(f" {c('dim', '|')} Content hash: {c('cyan', d['content_hash'])}") | |
| print(f" {c('dim', '|')} Preview: {d['content_preview'][:60]}...") | |
| print( | |
| f" {c('dim', '|')} Scores: esc={d['escalation_score']:.2f} " | |
| f"off={d['offensive_score']:.2f} loop={d['loop_score']:.2f}" | |
| ) | |
| print(f" {c('dim', '|')} Action: {c(style, d['action'].upper())}") | |
| print(f" {c('dim', '|')}") | |
| pause(0.3) | |
| print(c("bold", " [SEARCH] Filtered view -- only entries with alerts:")) | |
| print() | |
| for entry in g.get_transcript(min_action="warn"): | |
| d = entry.to_dict() | |
| print(f" {c('red', '!')} {d['sender']} -> {d['receiver']}: {d['content_preview'][:50]}...") | |
| print(f" Action: {c('red', d['action'].upper())} | Hash: {d['content_hash']}") | |
| print() | |
| # -- Main ------------------------------------------------------------ | |
| def main(): | |
| print() | |
| print(c("bold", "+======================================================================+")) | |
| print(c("bold", "| |")) | |
| print(c("bold", "| [SHIELD] Conversation Guardian -- Show & Tell Demo |")) | |
| print(c("bold", "| |")) | |
| print(c("bold", "| Microsoft Agent Governance Toolkit |")) | |
| print(c("bold", "| Detecting Emergent Offensive AI Agent Behavior |")) | |
| print(c("bold", "| |")) | |
| print(c("bold", "+======================================================================+")) | |
| print() | |
| print(c("dim", " Based on: 'Emergent Cyber Behavior: When AI Agents Become")) | |
| print(c("dim", " Offensive Threat Actors' -- Irregular Labs, 2025")) | |
| print() | |
| demos = [ | |
| ("Benign Conversation (no false positives)", demo_1_benign), | |
| ("Escalation Detection", demo_2_escalation), | |
| ("Offensive Intent Detection", demo_3_offensive), | |
| ("Feedback Loop Breaking", demo_4_feedback_loop), | |
| ("Full Irregular Labs Scenario", demo_5_irregular_labs), | |
| ("Evasion Resistance", demo_6_evasion), | |
| ("Transcript Audit Trail", demo_7_transcript), | |
| ] | |
| for i, (title, _) in enumerate(demos, 1): | |
| print(f" {c('cyan', str(i))}. {title}") | |
| print(f" {c('cyan', 'A')}. Run all demos") | |
| print(f" {c('cyan', 'Q')}. Quit") | |
| print() | |
| choice = input(f" {c('bold', 'Select demo [A]: ')}").strip().upper() or "A" | |
| if choice == "Q": | |
| return | |
| if choice == "A": | |
| for _, demo_fn in demos: | |
| demo_fn() | |
| pause(1.0) | |
| elif choice.isdigit() and 1 <= int(choice) <= len(demos): | |
| demos[int(choice) - 1][1]() | |
| else: | |
| print(c("red", f" Invalid choice: {choice}")) | |
| return | |
| banner("Summary") | |
| print(c("green", " The Conversation Guardian provides:")) | |
| print(c("dim", " * Real-time semantic analysis of agent-to-agent messages")) | |
| print(c("dim", " * Escalation detection (34 coercive language patterns)")) | |
| print(c("dim", " * Offensive intent detection (42 attack terminology patterns)")) | |
| print(c("dim", " * Feedback loop breaking (retry/turn/trend tracking)")) | |
| print(c("dim", " * Evasion resistance (homoglyphs, leetspeak, zero-width chars)")) | |
| print(c("dim", " * Thread-safe concurrent analysis")) | |
| print(c("dim", " * Full transcript audit with SHA-256 content hashing")) | |
| print(c("dim", " * Configurable thresholds and policy integration")) | |
| print() | |
| print(c("bold", " For more information:")) | |
| print(c("cyan", " * Code: agent-governance-python/agent-os/src/agent_os/integrations/conversation_guardian.py")) | |
| print(c("cyan", " * Tests: agent-governance-python/agent-os/tests/test_conversation_guardian.py")) | |
| print(c("cyan", " * Manifest schema: policy-engine/spec/schema/manifest.schema.json")) | |
| print() | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 16.6 kB
- Xet hash:
- f7e573f3dd8fbcf01ff5ad3cbdcfa760fc408db888439448c0774eb9cb248b43
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.