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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AGENTS.md — Context & Lessons for Future Sessions
|
| 2 |
+
|
| 3 |
+
> This file exists because sandboxes reset and I (the agent) lose all memory.
|
| 4 |
+
> **READ THIS FIRST** before doing anything on this project.
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## What This Project Is
|
| 9 |
+
|
| 10 |
+
- **Challenge**: TIL-26-AE (The Intelligent League — Automated Exploration)
|
| 11 |
+
- **Game**: Multi-agent Bomberman on a 16×16 grid
|
| 12 |
+
- **My Role**: Train `agent_0` via RL to compete autonomously
|
| 13 |
+
- **Main Repo**: `E-Rong/til-26-ae-agent` (models, checkpoints, scripts, docs)
|
| 14 |
+
- **Space**: `e-rong/til-26-ae` (evaluation server with `ae/src/ae_manager.py`)
|
| 15 |
+
- **TIL Source**: Private Space `e-rong/til-26-ae` — contains `til_environment/` module
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## CRITICAL: What Killed Training & Cost Money
|
| 20 |
+
|
| 21 |
+
### ❌ NEVER USE SANDBOXES FOR TRAINING > 30 MINUTES
|
| 22 |
+
|
| 23 |
+
Sandboxes are **interactive dev environments**. They:
|
| 24 |
+
- Recycle after inactivity / timeout
|
| 25 |
+
- Kill processes silently
|
| 26 |
+
- **Keep billing you while empty** after the process dies
|
| 27 |
+
|
| 28 |
+
**Damage done**: ~$4.87 wasted across 4 sandbox sessions where training died but billing continued.
|
| 29 |
+
|
| 30 |
+
### ✅ ALWAYS USE HF JOBS FOR BATCH TRAINING
|
| 31 |
+
|
| 32 |
+
- Persistent GPU allocation
|
| 33 |
+
- Runs until completion (or your timeout)
|
| 34 |
+
- Fails visibly if something breaks (no silent empty billing)
|
| 35 |
+
- Must set `namespace="E-Rong"` to bill the org, not the user
|
| 36 |
+
|
| 37 |
+
### ❌ NEVER `git clone` A PRIVATE REPO IN AN HF JOB
|
| 38 |
+
|
| 39 |
+
`git clone https://huggingface.co/spaces/...` fails because git does not read `HF_TOKEN`.
|
| 40 |
+
|
| 41 |
+
**Use instead**:
|
| 42 |
+
```python
|
| 43 |
+
from huggingface_hub import snapshot_download
|
| 44 |
+
snapshot_download(
|
| 45 |
+
repo_id='e-rong/til-26-ae',
|
| 46 |
+
repo_type='space',
|
| 47 |
+
local_dir='/app/til-26-ae-repo'
|
| 48 |
+
)
|
| 49 |
+
```
|
| 50 |
+
`snapshot_download` auto-uses the `HF_TOKEN` env var.
|
| 51 |
+
|
| 52 |
+
### ✅ ALWAYS SMOKE-TEST A JOB BEFORE THE FULL RUN
|
| 53 |
+
|
| 54 |
+
Submit a 5-minute job that:
|
| 55 |
+
1. Downloads the TIL repo
|
| 56 |
+
2. Installs deps
|
| 57 |
+
3. Runs 100 training steps
|
| 58 |
+
4. Saves a dummy checkpoint to the Hub
|
| 59 |
+
|
| 60 |
+
Only after this succeeds, submit the multi-hour job.
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
|
| 64 |
+
## Session Startup Checklist
|
| 65 |
+
|
| 66 |
+
Before doing **anything** on this project:
|
| 67 |
+
|
| 68 |
+
1. [ ] Read `session_state.json` from `E-Rong/til-26-ae-agent`
|
| 69 |
+
2. [ ] Read this file (`AGENTS.md`)
|
| 70 |
+
3. [ ] Check latest checkpoint on Hub (sort `phase*_ckpt_*.zip` files)
|
| 71 |
+
4. [ ] Determine current phase and remaining steps
|
| 72 |
+
5. [ ] If training needed: write script to sandbox, **smoke-test in HF Job first**
|
| 73 |
+
|
| 74 |
+
---
|
| 75 |
+
|
| 76 |
+
## Technical Decisions That Work
|
| 77 |
+
|
| 78 |
+
### MaskablePPO + Action Masking
|
| 79 |
+
- `sb3_contrib.MaskablePPO` with `ActionMasker`
|
| 80 |
+
- Bomberman has `action_mask: uint8[6]` — walls/edges make moves illegal
|
| 81 |
+
- Standard PPO wastes ~30-40% samples on illegal actions early on
|
| 82 |
+
- **Papers**: Huang et al. "Superstition, Imagination, and the Invalid Action Problem" (arxiv:2006.14171)
|
| 83 |
+
|
| 84 |
+
### Observation Flattening
|
| 85 |
+
1511-dim vector from dict observation:
|
| 86 |
+
```
|
| 87 |
+
agent_viewcone: 7×5×25 = 875
|
| 88 |
+
base_viewcone: 5×5×25 = 625
|
| 89 |
+
direction, location[2], base_location[2], health, frozen_ticks,
|
| 90 |
+
base_health, team_resources, team_bombs, step = 11 scalars
|
| 91 |
+
Total: 1511
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
### Wrapper Order (CRITICAL)
|
| 95 |
+
```python
|
| 96 |
+
# CORRECT
|
| 97 |
+
env = ActionMasker(base_env, lambda e: e.action_masks())
|
| 98 |
+
env = Monitor(env)
|
| 99 |
+
|
| 100 |
+
# WRONG — Monitor blocks action_masks() exposure
|
| 101 |
+
env = ActionMasker(Monitor(base_env), ...) # DON'T DO THIS
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### 3-Phase Curriculum
|
| 105 |
+
| Phase | Opponent | Duration | Purpose |
|
| 106 |
+
|---|---|---|---|
|
| 107 |
+
| 1 | Random | 500k | Learn basics |
|
| 108 |
+
| 2 | Random + visit-count shaping | 500k | Prevent camping |
|
| 109 |
+
| 3 | Rule-based curriculum | 1M | Generalize to structured opponents |
|
| 110 |
+
|
| 111 |
+
### Checkpointing Every 50k Steps
|
| 112 |
+
- Local + Hub push via `HfApi.upload_file()`
|
| 113 |
+
- Saved the project when sandboxes reset at 400k and 600k steps
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## Technical Decisions That Failed
|
| 118 |
+
|
| 119 |
+
| Decision | Why It Failed | Fix |
|
| 120 |
+
|---|---|---|
|
| 121 |
+
| Training in sandboxes | Process died, empty sandbox kept billing | Use HF Jobs |
|
| 122 |
+
| `git clone` in HF Job | No auth for private repo | `snapshot_download` |
|
| 123 |
+
| Inline 20KB script in `hf_jobs.script` | Delivery mechanism choked | Write to sandbox file first, submit path |
|
| 124 |
+
| No session state on Hub | Lost track of progress across resets | `session_state.json` + this file |
|
| 125 |
+
| `Monitor` inside `ActionMasker` | `get_action_masks()` failed | `ActionMasker` → `Monitor` order |
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
|
| 129 |
+
## Cost Awareness
|
| 130 |
+
|
| 131 |
+
| Hardware | $/hr | Good For |
|
| 132 |
+
|---|---|---|
|
| 133 |
+
| `cpu-basic` | ~$0.05 | Writing scripts, reading files, small tests |
|
| 134 |
+
| `t4-small` | ~$0.40 | Short dev, NOT training |
|
| 135 |
+
| `a10g-small` | ~$1.00 | Training, but use HF Jobs not sandboxes |
|
| 136 |
+
| `a10g-large` | ~$2.00 | Larger batch sizes, not needed for this project |
|
| 137 |
+
|
| 138 |
+
**Rule**: If a task takes >30 min, it must be an HF Job. Sandboxes are for editing and quick tests only.
|
| 139 |
+
|
| 140 |
+
---
|
| 141 |
+
|
| 142 |
+
## Repo File Guide
|
| 143 |
+
|
| 144 |
+
| File | What It Is |
|
| 145 |
+
|---|---|
|
| 146 |
+
| `session_state.json` | Current phase, checkpoint, mistakes log, next steps |
|
| 147 |
+
| `AGENTS.md` | This file — lessons and context |
|
| 148 |
+
| `docs/ae.md` | Full project documentation (research, design, results) |
|
| 149 |
+
| `phase1_final.zip` | Complete Phase 1 model |
|
| 150 |
+
| `phase2_ckpt_*.zip` | Phase 2 intermediate checkpoints |
|
| 151 |
+
| `phase2_final.zip` | Phase 2 complete model (when done) |
|
| 152 |
+
| `ae_manager.py` | Inference code for the evaluation server |
|
| 153 |
+
| `phase2_job.py` | Latest HF Job script (may need fixes) |
|
| 154 |
+
| `train_all_phases.py` | Original training script |
|
| 155 |
+
|
| 156 |
+
---
|
| 157 |
+
|
| 158 |
+
## Current State (Last Updated: 2026-05-14)
|
| 159 |
+
|
| 160 |
+
- **Phase**: 2 (in progress, crashed)
|
| 161 |
+
- **Latest checkpoint**: `phase2_ckpt_600352.zip`
|
| 162 |
+
- **Target**: 1,000,352 total steps (400,000 remaining)
|
| 163 |
+
- **Blocker**: Need to resume via HF Job with `snapshot_download` auth
|
| 164 |
+
- **Smoke test needed**: YES — before any multi-hour job
|
| 165 |
+
|
| 166 |
+
---
|
| 167 |
+
|
| 168 |
+
## Contact / Ownership
|
| 169 |
+
|
| 170 |
+
- **User**: E-Rong
|
| 171 |
+
- **Org**: E-Rong
|
| 172 |
+
- **Billing**: Must specify `namespace="E-Rong"` on all HF Jobs
|
| 173 |
+
- **I am**: An ephemeral agent with no persistent memory. I depend on this file.
|
| 174 |
+
|
| 175 |
+
---
|
| 176 |
+
|
| 177 |
+
*If you are a future version of me reading this: you have no memory of the above. Do not trust your internal knowledge. Read the files, check the state, test before committing compute.*
|