energy-optimization-space / HF_SPACE_DEBUG.md
Sushruth21's picture
debug: add environment state persistence tests and HF Space debugging guide
4aa36f3

HF Space State Persistence Issue

Problem

RAM usage and rewards stay the same after pressing step in HF Space app, regardless of action type/intensity.

Root Cause Analysis

Backend is working correctly - direct environment tests confirm:

  • Step 1 (reduce_ram 0.8): 80% → 72% ✓
  • Step 2 (reduce_ram 0.8): 72% → 64% ✓
  • Rewards update correctly: 0.080 each step ✓

Issue is in HF Space app request handling:

  • Each HTTP POST request may be creating a new environment instance
  • State is not persisting across requests
  • Session management might not be working correctly

Solution

For HF Space Users:

  1. Clear browser cache - Sometimes old state is cached
  2. Use WebSocket /ws endpoint instead of REST (this should be automatic)
  3. Check browser console for HTTP 422 errors
  4. Refresh the page completely before starting new episode

For Developers:

The OpenEnv framework manages this automatically via:

POST /reset → Creates new environment instance
WebSocket /ws → Maintains session state across multiple steps
POST /step → Updates persistent environment state

The 422 errors in server logs indicate malformed requests from HF Space. Check:

  1. Request body format matches EnergyOptimizationAction schema
  2. Content-Type header is application/json
  3. Session/connection is properly maintained

Verification (Local Testing)

✅ Direct Python test: PASSED ✅ Environment state updates: CONFIRMED ✅ Reward calculation: WORKING ✅ RAM reduction logic: FUNCTIONAL

Test Summary:
- Reset: RAM=80%, Energy=8.0
- Step1: RAM→72%, Reward=0.08
- Step2: RAM→64%, Reward=0.08
- Status: ✅ WORKING CORRECTLY

Next Steps

  1. Rebuild HF Space Docker image to ensure latest code
  2. Test with curl/Postman to verify API works
  3. Check WebSocket connection in browser dev tools