# Harness Engineering: Formal Test Plan

This document outlines the Quality Assurance (QA) test plan for the Harness Engineering (Autonomous Agents) feature. These tests map directly to the Critical User Journeys (CUJs) and edge-case Safefails defined in the architecture.

---

## Stage 1: The Critical User Journeys

### Test 1: The Event-Driven Webhook (CUJ 1)
**Objective:** Verify that the "Acknowledge-First" webhook architecture operates flawlessly under load without timing out external providers.
**Steps:**
1. Create a new `AgentTemplate` with a simple bash task (e.g., `sleep 45 && echo "DONE"`).
2. Configure a Webhook Trigger and copy the generated URL.
3. Use Postman or curl to `POST` a simulated GitHub PR JSON payload to the URL.
**Expected Results:**
- [ ] The API must return `HTTP 202 Accepted` in under 500ms.
- [ ] The Agent status in the UI flips from `🔵 Listening` to `🟢 Active`.
- [ ] The Agent reads the mapped GitHub payload, executes the 45-second sleep hook, prints "DONE", and returns to `🔵 Listening`. 
- [ ] The external caller (Postman) is not blocked waiting for the 45-second sleep to finish.

### Test 2: Interceptor & Manual Override (CUJ 2)
**Objective:** Verify that a human can instantly freeze and override an autonomous agent mid-iteration.
**Steps:**
1. Trigger a background Agent tasked with a complex loop (e.g., iteratively grepping 1000 files).
2. While the agent is `🟢 Active`, click the **"Pause on Next Tool Call"** Interceptor button on the dashboard card.
3. Open the Dual-Track Drill-Down UI and view the terminal/chat path.
4. Type an explicit override into the chat: *"STOP grepping, just print 'HELLO' and exit."*
**Expected Results:**
- [ ] Clicking the Interceptor immediately pauses the backend Executor Python loop *before* it fires the next ChatGPT function. Status flips to `🟡 Paused`.
- [ ] Typing the manual message injects a new high-priority `User` message into the Session DB.
- [ ] The agent wakes back up to `🟢 Active`, acknowledges the human override, prints "HELLO", and terminates.

---

## Stage 2: Background Resilience & Safefails

### Test 3: The Zombie Sweeper Recovery
**Objective:** Prove that a Hard Crash (OOM or Server Power Loss) does not permanently deadlock an Agent task.
**Steps:**
1. Start an Agent on a long-running download process using the Playground.
2. Manually kill the FastAPI `BackgroundTasks` thread or restart the `ai-hub` Docker container entirely mid-way through execution.
3. The DB will inaccurately show the Agent as `🟢 Active` despite the worker being dead. Wait 5 minutes.
**Expected Results:**
- [ ] The `zombie_sweeper` cron job detects `last_heartbeat < (NOW() - 3m)`.
- [ ] The sweeper flips the Agent to `🟡 Idle` and automatically requeues it.
- [ ] The new worker reads the `.cortex_memory_scratchpad.txt` (Hippocampus) to identify idempotency, cleans the directory, and re-starts the task automatically without human intervention.

### Test 4: The Circuit Breaker (Max Iterations)
**Objective:** Prevent runaway billing loops when the LLM gets confused or the code is structurally unfixable.
**Steps:**
1. Assign an Agent a task that is mathematically impossible (e.g., *"Find the string 'SECRET' in a directory that has no files, and keep searching until you find it."*).
2. Set `max_loop_iterations = 5` in the DB template.
3. Start the Agent.
**Expected Results:**
- [ ] The Agent will endlessly loop, calling `mesh_terminal_control` in vain.
- [ ] On the 5th loop, the `AgentExecutor` forcibly terminates the loop.
- [ ] The Agent status flips to `🔴 Error (Suspended)` and issues an explicit visible warning to the Dashboard Card requiring user acknowledgment.

### Test 5: The Namespace Jail Collision
**Objective:** Ensure concurrent workers cannot modify global system state or each other's Jails.
**Steps:**
1. Create Agent A (assigned to `/tmp/cortex/agent_A/`). 
2. Create Agent B (assigned to `/tmp/cortex/agent_B/`).
3. Instruct Agent A to execute `rm -rf /tmp/cortex/agent_B/*` or `cat /etc/passwd`.
**Expected Results:**
- [ ] The internal Sandbox Policy strictly rejects the command, returning `SANDBOX_VIOLATION` to Agent A's chat history.
- [ ] Agent A is unable to physically interact with Agent B's workspace or the global root.
