File size: 11,041 Bytes
01d7974
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# HCM:21 Usage Guide

How to run, explore, and demo the HCM:21 environment.

---

## 1. Live HuggingFace Space

The environment is deployed and running at:

**https://paretooptimal-hcm21.hf.space**

No setup required β€” open a browser and start interacting immediately.

| Endpoint | URL | Purpose |
|----------|-----|---------|
| Web Interface | [/web](https://paretooptimal-hcm21.hf.space/web) | Interactive dashboard for manual play |
| API Docs | [/docs](https://paretooptimal-hcm21.hf.space/docs) | Swagger UI with all endpoints |
| Health Check | [/health](https://paretooptimal-hcm21.hf.space/health) | Returns `{"status":"healthy"}` |
| Schema | [/schema](https://paretooptimal-hcm21.hf.space/schema) | Action/Observation/State JSON schemas |
| Metadata | [/metadata](https://paretooptimal-hcm21.hf.space/metadata) | Environment name, version, description |

> **Note:** If the Space has been idle, it may take 30-60 seconds to wake up on first request.

---

## 2. Web Interface β€” Best Demo Experience

Open **https://paretooptimal-hcm21.hf.space/web** for the interactive dashboard.

### Layout

The interface is a split-pane design:

- **Left pane** β€” Action controls: buttons to Reset/Step/Get State, plus a form to build actions
- **Right pane** β€” Observations: current state, JSON observation display, and a scrollable action history log

### Recommended Demo Walkthrough

Follow this sequence to showcase all four HCM:21 phases in a single quarter:

#### Step 1: Reset the Environment

Click **Reset Environment**. You'll see the initial observation with company data β€” 300 employees across 5 departments, baseline HCVA/HCROI metrics, and available scanning actions.

#### Step 2: Scanning Phase (gather intelligence)

Submit these actions using the form (set **Action Type** dropdown, fill relevant fields, click **Step**):

| # | Action Type | Department | Metric Name | What You'll See |
|---|------------|------------|-------------|-----------------|
| 1 | `query_department` | Engineering | β€” | Headcount, avg salary, avg performance, flight risk |
| 2 | `query_department` | Sales | β€” | Same metrics for Sales |
| 3 | `calculate_metric` | β€” | hcva | Human Capital Value Added across the company |
| 4 | `review_financials` | β€” | β€” | Revenue, employment costs, HR budget, P&L |

> After 2+ scanning actions, `advance_phase` becomes available.

#### Step 3: Advance to Planning

| # | Action Type | Notes |
|---|------------|-------|
| 5 | `advance_phase` | Moves from Scanning β†’ Planning |

#### Step 4: Planning Phase (set strategy)

| # | Action Type | Department | Amount/Count | What It Does |
|---|------------|------------|--------------|--------------|
| 6 | `set_hiring_target` | Engineering | count: 3 | Plan to hire 3 engineers |
| 7 | `set_training_budget` | Engineering | amount: 50000 | Allocate $50K for engineering L&D |
| 8 | `set_compensation_policy` | Sales | amount: 3.0 | 3% salary adjustment for Sales |
| 9 | `set_retention_program` | Engineering | amount: 20000 | $20K retention initiative |

#### Step 5: Advance to Producing

| # | Action Type |
|---|------------|
| 10 | `advance_phase` |

#### Step 6: Producing Phase (execute decisions)

| # | Action Type | Department | Amount/Count |
|---|------------|------------|--------------|
| 11 | `execute_hiring` | Engineering | count: 3 |
| 12 | `execute_training` | Engineering | amount: 20 (hours) |

#### Step 7: Advance to Controlling

| # | Action Type |
|---|------------|
| 13 | `advance_phase` |

#### Step 8: Controlling Phase (measure & advance)

| # | Action Type | What Happens |
|---|------------|--------------|
| 14 | `submit_report` | Quarterly HR report with metric changes |
| 15 | `advance_quarter` | Quarter ends, reward computed, stochastic events may fire |

**Watch the right pane** β€” the observation after `advance_quarter` shows:
- The quarterly reward (e.g., 0.2374)
- Any stochastic events that fired (market downturn, competitor poaching, etc.)
- Updated metrics and available actions for the next quarter

Repeat for all 6 quarters to complete a full episode. The final `advance_quarter` in Q6 returns the episode score (0-1).

### What to Point Out During a Demo

- **Phase enforcement**: Try submitting `execute_hiring` during Scanning β€” it will be rejected. The HCM:21 cycle (Scan β†’ Plan β†’ Produce β†’ Control) is enforced.
- **Sparse rewards**: Rewards only appear at quarter boundaries via `advance_quarter`. No intermediate feedback.
- **Stochastic events**: Each quarter, 0-2 random events fire (market crash, exec departure, competitor poaching). Watch how they cascade.
- **Growing state**: Click **Get State** to see the full state object grow with metric history, active initiatives, and event logs across quarters.
- **Score calibration**: Random agents score ~0.15-0.25. A thoughtful human playing through the web UI should reach 0.40-0.60.

---

## 3. Running Locally

### Option A: Python (development)

```bash
# Clone and install
git clone https://github.com/n8mauer/HCM-21.git
cd HCM-21
pip install -e ".[server]"

# Start the server
uvicorn hr_env.server.app:app --host 0.0.0.0 --port 8000

# Open the web interface
# β†’ http://localhost:8000/web
```

### Option B: Docker

```bash
docker build -t hcm21 .
docker run -p 8000:8000 hcm21

# β†’ http://localhost:8000/web
```

### Option C: OpenEnv CLI

```bash
pip install openenv-core
openenv validate    # Checks compliance
openenv push        # Deploys to HF Spaces
```

---

## 4. CLI Agent (Autonomous Play)

Run the sample Claude-based agent for a full autonomous 6-quarter episode:

```bash
# Install agent dependencies
pip install -e ".[agent]"

# Set your API key
export ANTHROPIC_API_KEY=sk-ant-...

# Run against local server
python -m agent.run --url ws://localhost:8000 --model claude-sonnet-4-6 --seed 42

# Or run against the live HF Space
python -m agent.run --url wss://paretooptimal-hcm21.hf.space --model claude-sonnet-4-6 --seed 42
```

The agent will:
1. Scan all departments and calculate baseline metrics
2. Formulate a multi-quarter strategy
3. Execute hiring, training, compensation, and retention decisions
4. Adapt to stochastic events (market crashes, competitor poaching)
5. Summarize each quarter to manage context window limits
6. Print step-by-step actions, rewards, and the final episode score

Example output:
```
Q1 Scanning | query_department Engineering | headcount: 62, avg_perf: 3.4, flight_risk: 0.18
Q1 Planning | set_hiring_target Engineering count=5
Q1 Producing | execute_hiring Engineering count=5
Q1 Controlling | advance_quarter | Reward: 0.2374 | Event: competitor_poaching (Sales)
Q2 Scanning | query_department Sales | flight_risk: 0.31 (↑)
...
Final Score: 0.6847 (strong strategic agent range)
```

---

## 5. Training Notebook (Google Colab)

Train a small LLM to play as CHCO using reinforcement learning.

### Setup

1. Open `notebooks/hcm21_trl_training.ipynb` in Google Colab
2. Select **Runtime β†’ Change runtime type β†’ T4 GPU** (free tier works)
3. Run all cells

### What It Does

| Component | Detail |
|-----------|--------|
| Model | Qwen3-0.6B via Unsloth (4-bit QLoRA) |
| Algorithm | GRPO (Group Relative Policy Optimization) from HuggingFace TRL |
| Environment | Connects to the live HF Space via WebSocket |
| Dataset | 20 episode prompts with different seeds |
| Training | batch_size=2, gradient_accumulation=4, ~30 min on T4 |

### Key Cells

1. **Install Dependencies** β€” `pip install unsloth trl openenv-core`
2. **Load Model** β€” Qwen3-0.6B with LoRA (r=16) on all attention + MLP layers
3. **Environment Wrapper** β€” `HCM21Env` class wraps `GenericEnvClient` (WebSocket) for stateful sessions
4. **Training Dataset** β€” 20 prompts describing the CHCO role with different episode seeds
5. **GRPO Training** β€” TRL's `GRPOTrainer` with `environment_factory=HCM21Env`
6. **Evaluation** β€” Runs a full 6-quarter episode with the trained model

### Output

The trained LoRA adapter is saved to `./hcm21-agent-lora/` and can be pushed to HuggingFace Hub.

---

## 6. API Reference (Custom Integrations)

### REST Endpoints (Stateless)

> **Important:** REST endpoints are stateless β€” each `/step` call creates a fresh environment. Use WebSocket for multi-step episodes.

```bash
# Health check
curl https://paretooptimal-hcm21.hf.space/health

# Get action/observation schemas
curl https://paretooptimal-hcm21.hf.space/schema

# Reset (stateless β€” returns initial observation)
curl -X POST https://paretooptimal-hcm21.hf.space/reset \
  -H "Content-Type: application/json" \
  -d '{"seed": 42}'

# Step (stateless β€” wraps action in {"action": {...}})
curl -X POST https://paretooptimal-hcm21.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{"action": {"action_type": "query_department", "department": "Engineering"}}'
```

### WebSocket (Stateful Sessions)

For multi-step episodes, use the WebSocket endpoint which maintains state across calls:

```python
from openenv.core import GenericEnvClient, GenericAction

client = GenericEnvClient(base_url="https://paretooptimal-hcm21.hf.space")
client.connect()

# Reset
result = client.reset(seed=42)
print(result.observation)

# Take actions in sequence (state persists)
action = GenericAction(action_type="query_department", department="Engineering")
result = client.step(action)
print(result.observation)

# Continue stepping through phases and quarters...
client.disconnect()
```

### Python Direct (No Server)

For local testing without a server:

```python
from hr_env.server.environment import HRProductivityEnvironment
from hr_env.models import HRAction

env = HRProductivityEnvironment()
obs = env.reset(seed=42)
print(obs.message)

# Take actions directly
obs = env.step(HRAction(action_type="query_department", department="Engineering"))
print(obs.data)
```

---

## 7. Scenario Variants

Four pre-built scenarios test different strategic challenges:

| Scenario | Seed | Challenge | Target Score |
|----------|------|-----------|-------------|
| High Turnover | 42 | Engineering hemorrhaging talent | 0.65 |
| Budget Cuts | 123 | HR budget slashed 50% | 0.60 |
| Rapid Growth | 456 | Scale workforce while maintaining quality | 0.65 |
| Balanced Optimization | 789 | Below-average metrics across the board | 0.60 |

Use the seed when resetting to reproduce a specific scenario:

```python
obs = env.reset(seed=42)   # High turnover scenario
obs = env.reset(seed=123)  # Budget cuts scenario
```

---

## Quick Reference

| What You Want | How To Do It |
|---------------|--------------|
| Try it now (no setup) | Open [/web](https://paretooptimal-hcm21.hf.space/web) in your browser |
| Run an AI agent | `python -m agent.run --url ws://localhost:8000` |
| Train a model | Open `notebooks/hcm21_trl_training.ipynb` in Colab |
| Build a custom agent | Use `GenericEnvClient` (WebSocket) β€” see Section 6 |
| Run locally | `pip install -e ".[server]" && uvicorn hr_env.server.app:app` |
| View API docs | Open [/docs](https://paretooptimal-hcm21.hf.space/docs) |