---
description: [tdd_feature_impl]: How to perform a close-loop check pattern utilizing Production DB locally for Test Driven Development (TDD)
---

This workflow ensures AI agents implement robust, context-safe features backed by real production data, strictly avoiding regressions.

# Execution Pattern

// turbo
1. **Sync Production DB to Local**: Execute the pre-configured script to copy the production SQLite database (`192.168.68.113`) to your local workspace.
```bash
chmod +x /app/scripts/sync_prod_db.sh && bash /app/scripts/sync_prod_db.sh
```

2. **Start Local Backend**: Ensure the local backend server is running in the background. Note: Verify if it's already running using `ps aux | grep uvicorn`, otherwise start it.
```bash
cd /app/ai-hub && uvicorn app.main:app --host 0.0.0.0 --port 8000 &
```

3. **Define the Integration Test**: Based on the `to-do` task, write an explicit integration test locally (e.g., in `/tmp/test_feature.py`). Use python `requests` targeting `http://localhost:8000` to simulate an end-to-end client API call testing the *desired* functionality that doesn't exist yet. The test MUST fail initially (Red step of Red-Green-Refactor).

4. **Pause & Switch Sessions (CRITICAL)**: Because iterating on complex implementations causes extreme token bloating and AI context amnesia, you must PAUSE here. Explicitly provide the user with a copy-pasteable prompt template to carry into the new session:

> *"I have synced the production DB and staged the integration test (`/tmp/...`). To prevent cross-impact and memory pollution during execution, **please switch to a new chat session** and copy-paste this prompt:"*
>
> ```text
> Hi! We are working on the Cortex AI-Hub backend (FastAPI + SQLAlchemy) and following a strict TDD close-loop workflow.
>
> Please execute **[Task Name]** as defined in the execution plan here: @/app/docs/...
>
> 1. I have already synced the production SQLite database locally.
> 2. An integration test has been staged at `[Test File Path]` which hits `http://localhost:8000/...`.
> 3. Start the local development server in the background:
>    `cd /app/ai-hub && uvicorn app.main:app --host 0.0.0.0 --port 8000 &`
> 4. Review the requirements from the execution plan.
> 5. [Insert specific files to modify here...]
>
> You are not finished until you can successfully execute `python3 [Test File Path]` against the local server and it passes all tests automatically. Iterate on your code until it works!
> ```

5. **Start Implementation (In New Session)**: Once respawned in the new session, begin architectural coding. 

6. **Validate & Loop**: Every time you modify the codebase and believe you are "finished", immediately execute the integration test you defined in Step 3. 
```bash
python3 /tmp/test_feature.py
```
If it fails, fix the code natively. You cannot declare the task completed to the user until this script runs cleanly against the local server containing the production DB dump.
