Spaces:
Sleeping
Sleeping
File size: 8,465 Bytes
6f49197 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# Task Context: HuggingFace API Routing Error
## Current Work: Debugging Agent Display Failure on HuggingFace Deployment
### Critical Error Summary
The SAAP platform deployed on HuggingFace is experiencing complete failure of agent display functionality. The frontend loads successfully, but agents cannot be retrieved from the backend.
**Error Pattern:**
```
GET /api/v1/agents HTTP/1.1" 500 Internal Server Error
RuntimeError: Not found (from spa_static_files.py line 27)
```
**Success Indicators:**
- Platform initializes successfully β
- 7 agents loaded into database β
- colossus connection successful β
- OpenRouter client initialized β
- Frontend static assets serve correctly β
- API endpoint fails catastrophically β
### Key Technical Concepts
**Architecture:**
- **Backend:** Python FastAPI with hybrid agent support (colossus + OpenRouter)
- **Frontend:** Vue.js SPA with Vite build system
- **Deployment:** Docker containerized on HuggingFace Spaces
- **Database:** SQLite with SQLAlchemy ORM
- **Agents:** 7 base templates (Jane, John, Lara, Theo, Justus, Leon, Luna Alesi)
**Critical Components:**
1. `backend/main.py` - FastAPI application initialization and middleware ordering
2. `backend/spa_static_files.py` - Custom static file handler (line 27 = error source)
3. `backend/api/agents.py` - Agent API endpoints (should handle /api/v1/agents)
4. `backend/services/agent_manager_hybrid.py` - Hybrid agent manager service
**Pydantic Warning (Secondary Issue):**
```
UserWarning: Valid config keys have changed in V2:
* 'schema_extra' has been renamed to 'json_schema_extra'
```
### Relevant Files and Code
**Error Source Location:**
- File: `backend/spa_static_files.py`
- Line: 27
- Method: `get_response`
- Error: `raise RuntimeError("Not found")`
**Stack Trace Analysis:**
```
Starlette Middleware Chain:
1. proxy_headers.py
2. CORS middleware
3. Exception middleware
4. Routing (fails here - goes to StaticFiles instead of API route)
5. StaticFiles handler (spa_static_files.py line 105)
6. get_response method (spa_static_files.py line 27) β RuntimeError
```
**Request Flow (ACTUAL):**
```
Frontend β GET /api/v1/agents
β Starlette routing
β StaticFiles middleware catches route (WRONG!)
β spa_static_files.py:get_response()
β RuntimeError("Not found")
```
**Request Flow (EXPECTED):**
```
Frontend β GET /api/v1/agents
β Starlette routing
β FastAPI agent router
β agent_api.py:get_agents()
β Return agent list JSON
```
### Problem Solved Thus Far
**Initialization Success:**
- Database connection established
- 7 agents registered from templates
- colossus server connection verified (14.06s test successful)
- OpenRouter client initialized with free model fallback
- Hybrid agent manager operational
**Configuration Verified:**
- Environment: `OPENROUTER_API_KEY` warning present (expected in free mode)
- Database: `sqlite:///./saap_production.db` created successfully
- Providers: Both colossus (β
) and OpenRouter (β
) initialized
### Pending Tasks and Next Steps
**Immediate Investigation Required:**
1. **Examine spa_static_files.py implementation**
```python
# Need to review line 27 in get_response method
# Understand why it's intercepting API routes
```
2. **Check main.py middleware ordering**
```python
# Verify API router mounted BEFORE static file handler
# Pattern should be:
app.include_router(api_router) # FIRST
app.mount("/", SPAStaticFiles()) # LAST (catch-all)
```
3. **Verify route registration**
```python
# Confirm /api/v1/agents endpoint properly registered
# Check FastAPI route debugging output
```
4. **Fix Pydantic V2 deprecation warning**
```python
# Replace 'schema_extra' with 'json_schema_extra' in Pydantic models
```
**Direct Quote from Error Logs:**
> "ich bekomme folgende Fehler in huggingface und die Aagenten werden aus diesem Grund nicht angezeigt:
>
> INFO: 10.20.38.235:38000 - "GET /api/v1/agents HTTP/1.1" 500 Internal Server Error
> ERROR: Exception in ASGI application
> Traceback (most recent call last):
> File "/app/spa_static_files.py", line 27, in get_response
> raise RuntimeError("Not found")
> RuntimeError: Not found"
**Where Development Left Off:**
The user encountered this error in production HuggingFace deployment. The platform starts successfully, all backend services initialize correctly, but the critical `/api/v1/agents` endpoint fails with a routing error. The static file handler is incorrectly intercepting API requests that should be handled by FastAPI routers.
**Root Cause Hypothesis:**
The middleware/route mounting order in `backend/main.py` likely has the SPA static file handler registered before (or with higher priority than) the API routes, causing all requests to be evaluated by the static file handler first. When `/api/v1/agents` doesn't match a static file, the handler raises `RuntimeError("Not found")` instead of allowing the request to continue to FastAPI routers.
**Required Fix Pattern:**
```python
# backend/main.py - CORRECT PATTERN
app = FastAPI()
# 1. Register API routes FIRST (specific routes)
app.include_router(agent_router, prefix="/api/v1")
app.include_router(hybrid_router, prefix="/api/v1")
# 2. Mount static files LAST (catch-all fallback)
app.mount("/", SPAStaticFiles(directory="frontend/dist"))
```
**Test Verification Needed:**
- Confirm `/api/v1/agents` returns JSON agent list (not 500)
- Verify static assets still load (/, /assets/*)
- Check agent display in frontend UI
- Validate both API and SPA work without conflicts
**Technical Context - File Structure:**
```
backend/
βββ main.py (β FIX NEEDED: middleware ordering)
βββ spa_static_files.py (β line 27 raising error)
βββ api/
β βββ agents.py (β should handle /api/v1/agents)
β βββ hybrid_endpoints.py
βββ services/
βββ agent_manager_hybrid.py
```
**Environment Details:**
- Python: 3.11
- FastAPI: Latest (from requirements.txt)
- Uvicorn: Running on 0.0.0.0:7860
- Platform: HuggingFace Spaces
- Deployment: Docker container
**Error Frequency:**
Continuous - every frontend refresh attempts to fetch agents and fails. The error repeats every few seconds as the frontend retries the request.
## Debugging Methodology to Apply
Per Rules/debugging-workflows.md - Four-Phase Framework:
**Phase 1: Root Cause Investigation** (IN PROGRESS)
- β
Read error messages - RuntimeError from spa_static_files.py line 27
- β
Stack trace analyzed - Starlette routing β StaticFiles β error
- β οΈ Need to read spa_static_files.py and main.py to confirm hypothesis
- β οΈ Need to trace middleware ordering
**Phase 2: Pattern Analysis** (PENDING)
- Find working FastAPI + SPA patterns
- Compare against current implementation
- Identify middleware ordering differences
**Phase 3: Hypothesis Testing** (PENDING)
- Hypothesis: Middleware ordering causes route interception
- Test: Reorder middleware in main.py
- Verify: API routes accessible, SPA still works
**Phase 4: Implementation** (PENDING)
- Create failing test for /api/v1/agents endpoint
- Fix middleware ordering
- Verify all tests pass
- Deploy and validate on HuggingFace
## Integration Requirements
**Security Considerations:**
- API endpoints must not expose sensitive data
- CORS properly configured for HuggingFace domain
- Error messages sanitized (no stack traces to frontend)
**Code Quality Standards:**
- Fix Pydantic V2 deprecation warnings
- Follow FastAPI best practices for middleware ordering
- Ensure clear separation: API routes vs static file serving
**Testing Requirements:**
- Integration test: GET /api/v1/agents returns 200 + agent list
- Integration test: GET / returns 200 + SPA HTML
- Integration test: GET /assets/* returns 200 + static assets
- Coverage target: β₯75% (MCP/tool-heavy architecture acceptable)
## Critical Files to Examine
1. **backend/spa_static_files.py** - Line 27 error source
2. **backend/main.py** - Middleware mounting order
3. **backend/api/agents.py** - Agent endpoint implementation
4. **backend/services/agent_manager_hybrid.py** - Agent retrieval logic
## Success Criteria
- [ ] `/api/v1/agents` returns 200 status with valid JSON
- [ ] Agents display in frontend UI
- [ ] Static assets continue loading correctly
- [ ] No RuntimeError exceptions
- [ ] Pydantic deprecation warning resolved
- [ ] Route precedence clearly documented
|