# QWEN UNIFICATION PRD
**Version:** 1.0
**Date:** 2026-01-16
**Model:** Gemini Flash 2.0 via Ralph Wiggum Loop
**Location:** `/mnt/e/genesis-system/AIVA/qwen-unified/`

---

## PRE-MORTEM ANALYSIS

### What Could Fail?
1. **Elestio Connectivity**: Server may be unreachable or misconfigured
2. **Ollama API Compatibility**: Version differences in Ollama API
3. **Context Window Limits**: Qwen 32K vs needs for larger contexts
4. **Model Loading**: QwenLong model may need specific parameters

### What Assumptions Are We Making?
1. Elestio server is running at 152.53.201.152
2. Ollama is installed and accessible on port 23405
3. QwenLong-L1.5-30B-A3B model is available
4. Network allows API communication between local and Elestio

### What Do We Not Fully Understand?
1. Optimal num_ctx setting for 32GB RAM (32K vs 64K)
2. Best temperature/top_p settings for Genesis tasks
3. Integration patterns with existing Triple Gate validator

### Rollback Plan
- All files in `/mnt/e/genesis-system/AIVA/qwen-unified/` can be deleted
- No modifications to Elestio server configuration
- Local-only changes

---

## PROJECT METADATA

| Field | Value |
|-------|-------|
| Project | Qwen Unification for Genesis AIVA |
| Location | `/mnt/e/genesis-system/AIVA/qwen-unified/` |
| Target | Unified Qwen access from Claude Code and Genesis |
| Execution | Gemini Flash 2.0 (~$0.001/attempt) |
| Max Iterations | 30 per story |
| Total Stories | 6 |

---

## USER STORIES

### US-001: Qwen Client Library
**Goal:** Create Python client for Elestio Qwen access

**Acceptance Criteria:**
```
GIVEN Genesis needs to query Qwen
WHEN the client is initialized
THEN it connects to Elestio Ollama and can generate responses
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/qwen_client.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/__init__.py`

**Dependencies:** None

**Technical Hints:**
- Elestio URL: http://152.53.201.152:23405
- Model: qwenlong-l1.5:30b-a3b or qwen-long:latest
- Use Ollama API format: POST /api/generate
- Include streaming and non-streaming modes
- Handle connection errors gracefully

---

### US-002: Context Window Optimizer
**Goal:** Create optimal context window configuration

**Acceptance Criteria:**
```
GIVEN Qwen needs context window tuning
WHEN the optimizer runs
THEN it configures num_ctx for 32GB RAM optimally
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/context_optimizer.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/modelfile_generator.py`

**Dependencies:** US-001

**Technical Hints:**
- 32K tokens optimal for 32GB RAM
- Create Ollama Modelfile for genesis-queen
- Parameters: num_ctx=32768, num_predict=8192, temperature=0.7
- Include memory usage estimation

---

### US-003: Genesis Integration Bridge
**Goal:** Bridge Qwen to Genesis core systems

**Acceptance Criteria:**
```
GIVEN Genesis systems need Qwen
WHEN bridge is initialized
THEN Triple Gate, RLM, and other systems can use Qwen
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/genesis_bridge.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/config.py`

**Dependencies:** US-001, US-002

**Technical Hints:**
- Integrate with `/mnt/e/genesis-system/AIVA/triple_gate_validator.py`
- Support RLM memory queries
- Include task routing (simple vs complex)
- Log all queries for audit trail

---

### US-004: Health Monitor
**Goal:** Monitor Qwen/Elestio health and availability

**Acceptance Criteria:**
```
GIVEN Elestio services are running
WHEN health check executes
THEN returns detailed status of Ollama and Qwen model
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/health_monitor.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/metrics.py`

**Dependencies:** US-001

**Technical Hints:**
- Check Ollama API status
- Verify model is loaded
- Monitor response times
- Track token usage
- Alert on degradation

---

### US-005: CLI Tool
**Goal:** Create command-line tool for Qwen interaction

**Acceptance Criteria:**
```
GIVEN user wants to query Qwen from terminal
WHEN CLI tool is invoked
THEN it sends query and displays response
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/cli.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/chat.py`

**Dependencies:** US-001

**Technical Hints:**
- Support: `python cli.py "query"`
- Support: `python chat.py` for interactive mode
- Include context persistence for conversations
- Color output for terminal

---

### US-006: Test Suite
**Goal:** Create comprehensive test suite

**Acceptance Criteria:**
```
GIVEN all components are created
WHEN tests run
THEN all components pass validation
```

**Files to Create:**
- `/mnt/e/genesis-system/AIVA/qwen-unified/test_qwen.py`
- `/mnt/e/genesis-system/AIVA/qwen-unified/README.md`

**Dependencies:** US-001 through US-005

**Technical Hints:**
- Test connection to Elestio
- Test model response quality
- Test context window handling
- Generate test report
- Document usage in README

---

## EXECUTION PHASES

### Phase 1: Core
- US-001: Qwen Client Library
- US-002: Context Optimizer

### Phase 2: Integration
- US-003: Genesis Bridge
- US-004: Health Monitor

### Phase 3: Tools
- US-005: CLI Tool
- US-006: Test Suite

---

## SOURCE FILES

| File | Purpose |
|------|---------|
| `/mnt/e/genesis-system/AIVA/ARCHITECTURE_DECISION.md` | Architecture reference |
| `/mnt/e/genesis-system/AIVA/elestio_connection.env` | Connection credentials |
| `/mnt/e/genesis-system/AIVA/triple_gate_validator.py` | Integration target |
| `/mnt/e/genesis-system/config/elestio/` | Elestio config files |

---

## SUCCESS CRITERIA

1. ✅ Qwen client connects to Elestio successfully
2. ✅ Context window optimized for 32GB RAM
3. ✅ Genesis systems can use Qwen via bridge
4. ✅ Health monitoring active
5. ✅ CLI tools functional
6. ✅ All tests passing

---

*PRD Generated: 2026-01-16*
*Ralph Wiggum Loop: Fresh Gemini Flash 2.0 context per story*
