# Deterministic Computer-Use Automation Framework [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/) [![Playwright](https://img.shields.io/badge/tested%20with-Playwright-green.svg)](https://playwright.dev/) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) A robust, enterprise-grade framework that gives backend AI agents the ability to operate legacy, API-less web interfaces reliably. Instead of running continuous, expensive LLM visual loops on every interaction, this framework implements a **two-phase paradigm**: 1. **LLM Discovery (Probabilistic):** An LLM agent explores the target UI, overcomes layout quirks/interstitials, synthesizes multi-tier locator fallback cascades, and serializes the workflow into a typed, parameterized capability contract. 2. **Deterministic Replay (Zero LLM Calls):** Production workloads execute the compiled artifact directly via Playwright in sub-second time with zero token cost, complete determinism, and built-in safety guardrails. --- ## ๐Ÿ—๏ธ Architecture Overview ```mermaid flowchart TD subgraph p1 ["Phase 1: Probabilistic Discovery"] Goal["User / Agent Goal"] --> Agent["Discovery Agent (LLM)"] Agent -->|"Inspect DOM / Action Loop"| TargetApp[("Target Legacy UI")] TargetApp -->|"Observe State"| Agent Agent -->|"Synthesize Selectors"| Artifact["CapabilityArtifact (.json)"] end subgraph p2 ["Phase 2: Deterministic Replay"] Artifact --> ReplayEngine["Replay Engine (Playwright)"] Inputs["Runtime Parameters"] --> ReplayEngine ReplayEngine -->|"Sub-second / LLM-Free"| TargetApp ReplayEngine -->|"Happy Path"| Success["Verified Outputs Extracted"] ReplayEngine -->|"Known Domain Case"| BusinessOutcome["Business Outcome (e.g. Not Found)"] ReplayEngine -->|"Risky Step / Stuck"| HITL["Human-In-The-Loop Escalation"] end ``` --- ## โœจ Key Features * **โšก Zero-Token Deterministic Replay:** Replays workflows in <500ms without probabilistic LLM drift or ongoing API costs. * **๐Ÿ›ก๏ธ Multi-Strategy Locator Cascades:** Resilient against dynamic or obfuscated DOMs with ordered fallbacks: `CSS` $\rightarrow$ `Text Anchor` $\rightarrow$ `XPath` $\rightarrow$ `ARIA Role`. * **๐Ÿšฆ Domain Error Taxonomy:** Distinguishes between technical crashes (`HARD_FAILURE`) and expected business results (`BUSINESS_OUTCOME`, e.g., "Member ID Not Found"). * **๐Ÿ›‘ Human-in-the-Loop (HITL) Seam:** Automatically detects risky keywords (e.g., `freeze`, `transfer`), pauses execution, captures full-page evidence, and serializes session state for manual human review and handoff. * **๐Ÿ”’ Built-in PII & Privacy Redaction:** Centralized redaction interceptor strips SSNs, 16-digit payment card numbers, and auth tokens before logging or disk persistence. * **๐Ÿ“ฆ Zero-Dependency Local Sandbox:** Includes a self-contained mock legacy banking console with automated local HTTP serving for instant verification. --- ## ๐Ÿš€ Quickstart ### 1. Prerequisites - Python 3.11+ - Google Chrome / Chromium ### 2. Installation ```bash # Clone the repository git clone https://github.com/your-username/your-repo-name.git cd your-repo-name # Set up virtual environment python3 -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate # Install dependencies and browser binaries pip install -r requirements.txt playwright install chromium ``` ### 3. Run the End-to-End Demo Run the test suite with a single command (spins up a local stand-in banking server automatically): ```bash python main.py ``` --- ## โš™๏ธ LLM Provider Configuration The discovery engine uses a standard OpenAI-compatible client interface and can be pointed at any provider: ### Option A: Offline / Heuristic Mode (Zero Keys Needed) If no environment variables are set, the system runs with local heuristic discovery and fallback synthesis: ```bash python main.py ``` ### Option B: Google AI Studio (Gemini) ```bash export OPENAI_BASE_URL="https://generativelanguage.googleapis.com/v1beta/openai/" export OPENAI_API_KEY="AIzaSy..." export LLM_MODEL="gemini-1.5-flash" ``` ### Option C: OpenAI ```bash export OPENAI_API_KEY="sk-..." export LLM_MODEL="gpt-4o" ``` ### Option D: Groq / Local Ollama ```bash # Groq export OPENAI_BASE_URL="https://api.groq.com/openai/v1" export OPENAI_API_KEY="gsk_..." export LLM_MODEL="llama-3.3-70b-versatile" # Local Ollama (100% private, local inference) export OPENAI_BASE_URL="http://localhost:11434/v1" export OPENAI_API_KEY="ollama" export LLM_MODEL="llama3.2" ``` --- ## ๐Ÿงช What `main.py` Demonstrates | Phase | Description | Key Mechanism | | :--- | :--- | :--- | | **1. Discovery Run** | Explores `app/index.html`, dismisses interstitials, maps parameters, and writes `evidence/capability_member_lookup.json`. | LLM Planner + DOM Reflection | | **2. Happy Path Replay** | Executes member lookup for `member_id=10042` and extracts balances in <350ms. | Deterministic Playwright Engine | | **3. Business Outcome** | Executes lookup for `member_id=99999` and cleanly catches "Member Not Found". | 4-Tier Error Taxonomy | | **4. HITL Escalation** | Intercepts an account freeze operation, halts execution, and transfers control to an operator. | State Serialization & Handoff | --- ## ๐Ÿ“‚ Repository Structure ```text โ”œโ”€โ”€ app/ โ”‚ โ””โ”€โ”€ index.html # Hostile legacy enterprise UI (tables, dynamic IDs, popups) โ”œโ”€โ”€ evidence/ # Generated artifacts, traces, and execution logs โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ config.py # Domain allowlists, risky keyword policies, and PII redactor โ”‚ โ”œโ”€โ”€ discovery.py # LLM Observe -> Decide -> Act discovery loop โ”‚ โ”œโ”€โ”€ escalation.py # HITL state serialization and handoff manager โ”‚ โ”œโ”€โ”€ replay.py # LLM-free deterministic replay engine โ”‚ โ”œโ”€โ”€ schema.py # Pydantic v2 contract definitions (CapabilityArtifact) โ”‚ โ””โ”€โ”€ utils.py # Evidence collectors and redacting logger โ”œโ”€โ”€ main.py # Complete end-to-end execution harness โ”œโ”€โ”€ REPORT.md # Comprehensive architectural design report โ””โ”€โ”€ requirements.txt # Project dependencies ``` --- ## ๐Ÿ“œ License This project is licensed under the MIT License โ€” see the [LICENSE](LICENSE) file for details.