File size: 10,023 Bytes
3f6526a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Step 1: Testing Guide for EV2 Service

## 🎯 What We Built

A **minimal HTTP service wrapper** around `ev2.py` that:
- βœ… Receives generation completion notifications
- βœ… Autonomously decides when to trigger EV2 agent
- βœ… Maintains persistent state across generations
- βœ… Requires minimal changes to ShinkaEvolve

## πŸ“‹ File Overview

```
eval_agent/
β”œβ”€β”€ ev2_service.py              # The HTTP service (NEW)
β”œβ”€β”€ ev2_service_config.yaml     # Configuration file (NEW)
β”œβ”€β”€ test_ev2_service.py         # Test script (NEW)
β”œβ”€β”€ ev2.py                      # Original agent logic (UNCHANGED)
└── ev2_prompt.j2               # Agent prompt (UNCHANGED)
```

## πŸš€ Step-by-Step Testing

### Step 1: Install Dependencies

```bash
cd /home/tengxiao/pj/ShinkaEvolve
source venv/bin/activate

# Install FastAPI and Uvicorn
pip install fastapi uvicorn pyyaml
```

### Step 2: Configure the Service

Edit `eval_agent/ev2_service_config.yaml` if needed:

```yaml
experiment:
  results_dir: "examples/circle_packing/results/results_circle_packing_NO_vision_WITH_refined_aux_20260118_205215"
  primary_evaluator: "examples/circle_packing/evaluate_ori.py"

strategy:
  trigger_mode: "periodic"  # Options: always, periodic, plateau, mixed
  trigger_interval: 10       # Run agent every 10 generations
```

**Trigger Modes:**
- `always`: Run agent every generation (for testing)
- `periodic`: Run every N generations
- `plateau`: Run when score plateaus
- `mixed`: Run on periodic OR plateau (whichever comes first)

### Step 3: Start the Service

**Terminal 1** (Service):

```bash
cd /home/tengxiao/pj/ShinkaEvolve
source venv/bin/activate

# Start the service
python eval_agent/ev2_service.py --config eval_agent/ev2_service_config.yaml
```

Expected output:
```
INFO:     Started server process [12345]
INFO:     Waiting for application startup.
2026-02-02 15:30:00 - __main__ - INFO - πŸš€ Starting EV2 Evaluation Service...
2026-02-02 15:30:00 - __main__ - INFO - βœ… Service started
2026-02-02 15:30:00 - __main__ - INFO -    Experiment: circle_packing_NO_vision
2026-02-02 15:30:00 - __main__ - INFO -    Trigger mode: periodic
2026-02-02 15:30:00 - __main__ - INFO -    Trigger interval: 10
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8765 (Press CTRL+C to quit)
```

### Step 4: Test the Service

**Terminal 2** (Test):

```bash
cd /home/tengxiao/pj/ShinkaEvolve
source venv/bin/activate

# Test 1: Check service status
python eval_agent/test_ev2_service.py \
    --results-dir "examples/circle_packing/results/results_circle_packing_NO_vision_WITH_refined_aux_20260118_205215" \
    --test-mode status
```

Expected output:
```
πŸ” Testing service status...
βœ… Service is running!
   Uptime: 12.3s
   Trigger mode: periodic
   Trigger interval: 10
```

```bash
# Test 2: Simulate evolution (25 generations)
python eval_agent/test_ev2_service.py \
    --results-dir "examples/circle_packing/results/results_circle_packing_NO_vision_WITH_refined_aux_20260118_205215" \
    --test-mode simulate \
    --num-gens 25
```

Expected output:
```
🧬 Simulating evolution with 25 generations...
======================================================================

πŸ“€ Sending notification: gen=0, score=2.4000
   Status: skipped
   Agent triggered: False
   Reason: Not yet (last trigger at gen -1)
   Processing time: 5.2ms

πŸ“€ Sending notification: gen=1, score=2.4050
   Status: skipped
   Agent triggered: False
   Reason: Not yet (last trigger at gen -1)
   Processing time: 3.1ms

...

πŸ“€ Sending notification: gen=10, score=2.4500
   Status: success
   Agent triggered: True
   Reason: Periodic trigger (interval=10)
   Processing time: 15234.5ms
   Insights: 3 found

...

πŸ“€ Sending notification: gen=20, score=2.4950
   Status: success
   Agent triggered: True
   Reason: Periodic trigger (interval=10)
   Processing time: 12456.7ms
   Insights: 3 found

======================================================================
βœ… Simulation complete!
```

### Step 5: Check Results

The service creates/updates:

```
examples/circle_packing/results/.../
└── eval_agent_memory/
    β”œβ”€β”€ EVAL_AGENTS.md              # Updated by agent
    β”œβ”€β”€ auxiliary_metrics.py        # Created by agent
    └── service_state.json          # Service state (NEW)
```

Check service state:
```bash
cat examples/circle_packing/results/results_circle_packing_NO_vision_WITH_refined_aux_20260118_205215/eval_agent_memory/service_state.json
```

### Step 6: Test Manual Trigger (Optional)

```bash
# Manually trigger agent for generation 5
python eval_agent/test_ev2_service.py \
    --results-dir "examples/circle_packing/results/results_circle_packing_NO_vision_WITH_refined_aux_20260118_205215" \
    --test-mode manual \
    --generation 5
```

## πŸ”Œ API Documentation

The service provides these endpoints:

### 1. Generation Notification (Main)

```bash
curl -X POST http://localhost:8765/api/v1/notify/generation_complete \
  -H "Content-Type: application/json" \
  -d '{
    "generation": 42,
    "results_dir": "/path/to/results",
    "primary_score": 2.5407
  }'
```

Response:
```json
{
  "status": "success",
  "message": "Periodic trigger (interval=10)",
  "generation": 42,
  "agent_triggered": true,
  "trigger_reason": "Periodic trigger (interval=10)",
  "insights": ["..."],
  "auxiliary_metrics": {...},
  "processing_time_ms": 15234.5
}
```

### 2. Service Status

```bash
curl http://localhost:8765/api/v1/status
```

### 3. Manual Trigger

```bash
curl -X POST "http://localhost:8765/api/v1/trigger/manual?generation=10"
```

### 4. Interactive Docs

Open in browser: http://localhost:8765/docs

## πŸ”§ Integration with ShinkaEvolve

To integrate with ShinkaEvolve, add this to `EvolutionRunner`:

```python
# shinka/core/runner.py

class EvolutionRunner:
    def __init__(self, config: EvolutionConfig):
        self.config = config
        
        # Initialize eval service client (optional)
        self.eval_service_url = config.eval_service_url if hasattr(config, 'eval_service_url') else None
    
    def _evaluate_generation(self, generation: int, code_path: str, results_dir: str):
        # Run normal evaluation (unchanged)
        results, score = self.scheduler.run(code_path, results_dir)
        
        # Notify eval service (NEW, non-blocking)
        if self.eval_service_url:
            try:
                import requests
                requests.post(
                    f"{self.eval_service_url}/api/v1/notify/generation_complete",
                    json={
                        "generation": generation,
                        "results_dir": results_dir,
                        "primary_score": score
                    },
                    timeout=1  # Short timeout, fire-and-forget
                )
            except Exception as e:
                self.logger.warning(f"Eval service notification failed: {e}")
                # Continue regardless
        
        return results, score
```

**Changes required**: ~10 lines of code!

## πŸ“Š Service Decision Logic

The service decides autonomously when to trigger the agent:

```python
Generation 0:  score=2.40  β†’ SKIP (not yet, interval=10)
Generation 1:  score=2.41  β†’ SKIP
...
Generation 10: score=2.45  β†’ TRIGGER (periodic, interval=10) βœ…
Generation 11: score=2.46  β†’ SKIP
...
Generation 20: score=2.49  β†’ TRIGGER (periodic, interval=10) βœ…
...
```

With `trigger_mode: "mixed"`:
```python
Generation 0:  score=2.40  β†’ SKIP
Generation 5:  score=2.40  β†’ TRIGGER (plateau detected!) βœ…
Generation 10: score=2.45  β†’ TRIGGER (periodic) βœ…
...
```

## 🎯 What This Achieves

### Before (without service):
```python
# In ShinkaEvolve
for gen in range(num_generations):
    score = evaluate(gen)
    # No auxiliary metrics
    # No intelligent analysis
```

### After (with service):
```python
# In ShinkaEvolve (minimal change)
for gen in range(num_generations):
    score = evaluate(gen)
    notify_service(gen, score)  # ← Just one line!
    
# Service independently:
# - Decides when to analyze
# - Runs EV2 agent
# - Creates auxiliary metrics
# - Accumulates insights
```

## βœ… Success Criteria

You've successfully tested Step 1 if:

1. βœ… Service starts without errors
2. βœ… Service responds to notifications
3. βœ… Service correctly skips some generations (based on strategy)
4. βœ… Service triggers agent at the right times
5. βœ… Agent creates/updates EVAL_AGENTS.md and auxiliary_metrics.py
6. βœ… Service state persists (check service_state.json)

## πŸ› Troubleshooting

### Service won't start

**Error**: `ModuleNotFoundError: No module named 'fastapi'`
**Fix**: `pip install fastapi uvicorn pyyaml`

### Service starts but test fails

**Error**: `Cannot connect to service`
**Fix**: Check if service is running on port 8765. Try: `curl http://localhost:8765/`

### Agent doesn't trigger

**Check**:
1. Is `agent_enabled: true` in config?
2. Are you sending enough generations? (interval=10 means trigger at gen 10, 20, 30...)
3. Check service logs in Terminal 1

### Agent fails to run

**Error in service logs**: `Primary evaluator not found`
**Fix**: Check `primary_evaluator` path in config is correct

## πŸš€ Next Steps

After Step 1 works:

**Step 2**: Add intelligent decision-making
- More sophisticated trigger strategies
- Plateau detection improvements
- Alert levels

**Step 3**: Add persistent memory
- SQLite database for history
- Metric tracking
- Correlation analysis

**Step 4**: Add MetricUnit management
- Object-oriented metrics
- Lifecycle management
- Validation system

## πŸ“ Notes

- The service is **stateless regarding ShinkaEvolve** - it doesn't block or affect the evolution process
- If the service crashes, ShinkaEvolve continues normally (fire-and-forget)
- Service state is saved to disk, so it survives restarts
- All agent logic from `ev2.py` is preserved and unchanged

---

Ready to test? Start the service and run the tests! πŸš€