garvitsachdeva commited on
Commit
5b4eb04
·
1 Parent(s): 1bc6b3d

Expand action space: stage, reassign, mutual aid, severity change

Browse files
Files changed (3) hide show
  1. src/protocol.py +21 -1
  2. src/state_machine.py +251 -17
  3. tests/test_state_machine.py +38 -1
src/protocol.py CHANGED
@@ -56,7 +56,7 @@ class DispatchProtocolValidator:
56
  error(f"Unknown incident_id '{action.incident_id}'")
57
  return ValidationResult(ok=False, issues=issues)
58
 
59
- if action.action_type in {DispatchAction.DISPATCH, DispatchAction.REASSIGN}:
60
  if unit.status != UnitStatus.AVAILABLE:
61
  error(f"Unit '{unit.unit_id}' not available (status={unit.status})")
62
  if unit.assigned_incident_id is not None:
@@ -73,6 +73,26 @@ class DispatchProtocolValidator:
73
  f"recommended {incident.incident_type} types {required}"
74
  )
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  elif action.action_type == DispatchAction.CANCEL:
77
  if unit.assigned_incident_id != incident.incident_id:
78
  error(f"Unit '{unit.unit_id}' not assigned to incident '{incident.incident_id}'")
 
56
  error(f"Unknown incident_id '{action.incident_id}'")
57
  return ValidationResult(ok=False, issues=issues)
58
 
59
+ if action.action_type == DispatchAction.DISPATCH:
60
  if unit.status != UnitStatus.AVAILABLE:
61
  error(f"Unit '{unit.unit_id}' not available (status={unit.status})")
62
  if unit.assigned_incident_id is not None:
 
73
  f"recommended {incident.incident_type} types {required}"
74
  )
75
 
76
+ elif action.action_type == DispatchAction.REASSIGN:
77
+ if incident.status in {IncidentStatus.RESOLVED}:
78
+ error(f"Incident '{incident.incident_id}' already resolved")
79
+ if unit.assigned_incident_id is None:
80
+ error(f"Unit '{unit.unit_id}' is not currently assigned")
81
+ elif unit.assigned_incident_id == incident.incident_id:
82
+ error(f"Unit '{unit.unit_id}' already assigned to incident '{incident.incident_id}'")
83
+
84
+ if unit.status not in {UnitStatus.DISPATCHED, UnitStatus.ON_SCENE, UnitStatus.TRANSPORTING}:
85
+ error(f"Unit '{unit.unit_id}' cannot be reassigned (status={unit.status})")
86
+
87
+ # Triage type matching is a soft rule: record warning, do not invalidate.
88
+ required = schema.default_required_units.get(incident.incident_type)
89
+ if required is not None and required:
90
+ if unit.unit_type not in required:
91
+ warn(
92
+ f"Unit '{unit.unit_id}' type {unit.unit_type} mismatches "
93
+ f"recommended {incident.incident_type} types {required}"
94
+ )
95
+
96
  elif action.action_type == DispatchAction.CANCEL:
97
  if unit.assigned_incident_id != incident.incident_id:
98
  error(f"Unit '{unit.unit_id}' not assigned to incident '{incident.incident_id}'")
src/state_machine.py CHANGED
@@ -49,7 +49,7 @@ def _distance(x1: float, y1: float, x2: float, y2: float) -> float:
49
  class DispatchStateMachine:
50
  """Deterministic dispatch state machine.
51
 
52
- Supports a minimal action set (DISPATCH, CANCEL) and advances incidents through:
53
  PENDING → RESPONDING → ON_SCENE → RESOLVED.
54
  """
55
 
@@ -117,32 +117,162 @@ class DispatchStateMachine:
117
  actions: list[Action] = []
118
 
119
  active_incidents = [
120
- i
121
- for i in state.incidents.values()
122
- if i.status not in {IncidentStatus.RESOLVED}
123
  ]
124
  if not active_incidents:
125
  return actions
126
 
127
- for unit in state.units.values():
128
- if unit.status == UnitStatus.AVAILABLE:
129
- for incident in active_incidents:
130
- actions.append(
131
- Action(
132
- action_type=DispatchAction.DISPATCH,
133
- unit_id=unit.unit_id,
134
- incident_id=incident.incident_id,
135
- )
 
 
 
 
 
 
 
 
 
136
  )
137
- elif unit.assigned_incident_id is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  actions.append(
139
  Action(
140
- action_type=DispatchAction.CANCEL,
141
  unit_id=unit.unit_id,
142
- incident_id=unit.assigned_incident_id,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  )
144
  )
145
- return actions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  def step(self, state: State, action: Action) -> tuple[State, Observation]:
148
  validation = self._validator.validate(self._schema, state, action)
@@ -170,6 +300,14 @@ class DispatchStateMachine:
170
  self._apply_dispatch(state, action)
171
  elif action.action_type == DispatchAction.CANCEL:
172
  self._apply_cancel(state, action)
 
 
 
 
 
 
 
 
173
 
174
  state = self._tick(state)
175
 
@@ -255,6 +393,102 @@ class DispatchStateMachine:
255
  incident.status = IncidentStatus.PENDING
256
  incident.survival_clock = _severity_deadline_seconds(incident.severity)
257
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  def _tick(self, state: State) -> State:
259
  state.step_count += 1
260
  state.city_time += DEFAULT_DT_S
 
49
  class DispatchStateMachine:
50
  """Deterministic dispatch state machine.
51
 
52
+ Supports dispatch operations and advances incidents through:
53
  PENDING → RESPONDING → ON_SCENE → RESOLVED.
54
  """
55
 
 
117
  actions: list[Action] = []
118
 
119
  active_incidents = [
120
+ i for i in state.incidents.values() if i.status not in {IncidentStatus.RESOLVED}
 
 
121
  ]
122
  if not active_incidents:
123
  return actions
124
 
125
+ # Keep ordering stable and DISPATCH-first for callers that take legal[0].
126
+ active_incidents_sorted = sorted(active_incidents, key=lambda i: i.incident_id)
127
+ units_sorted = sorted(state.units.values(), key=lambda u: u.unit_id)
128
+
129
+ # Pick a deterministic "reference" unit for actions that don't semantically need one
130
+ # (UPGRADE/DOWNGRADE require unit_id in the Action contract).
131
+ ref_unit_id = units_sorted[0].unit_id if units_sorted else ""
132
+
133
+ # DISPATCH actions (primary control surface)
134
+ for unit in units_sorted:
135
+ if unit.status != UnitStatus.AVAILABLE:
136
+ continue
137
+ for incident in active_incidents_sorted:
138
+ actions.append(
139
+ Action(
140
+ action_type=DispatchAction.DISPATCH,
141
+ unit_id=unit.unit_id,
142
+ incident_id=incident.incident_id,
143
  )
144
+ )
145
+
146
+ # STAGE actions (pre-position without committing as assigned)
147
+ for unit in units_sorted:
148
+ if unit.status != UnitStatus.AVAILABLE:
149
+ continue
150
+ for incident in active_incidents_sorted:
151
+ if incident.status != IncidentStatus.PENDING:
152
+ continue
153
+ actions.append(
154
+ Action(
155
+ action_type=DispatchAction.STAGE,
156
+ unit_id=unit.unit_id,
157
+ incident_id=incident.incident_id,
158
+ )
159
+ )
160
+
161
+ # CANCEL actions (release currently assigned units)
162
+ for unit in units_sorted:
163
+ if unit.assigned_incident_id is None:
164
+ continue
165
+ actions.append(
166
+ Action(
167
+ action_type=DispatchAction.CANCEL,
168
+ unit_id=unit.unit_id,
169
+ incident_id=unit.assigned_incident_id,
170
+ )
171
+ )
172
+
173
+ # REASSIGN actions (redirect already-assigned units to a different active incident)
174
+ for unit in units_sorted:
175
+ if unit.assigned_incident_id is None:
176
+ continue
177
+ if unit.status not in {UnitStatus.DISPATCHED, UnitStatus.ON_SCENE, UnitStatus.TRANSPORTING}:
178
+ continue
179
+ for incident in active_incidents_sorted:
180
+ if incident.incident_id == unit.assigned_incident_id:
181
+ continue
182
  actions.append(
183
  Action(
184
+ action_type=DispatchAction.REASSIGN,
185
  unit_id=unit.unit_id,
186
+ incident_id=incident.incident_id,
187
+ )
188
+ )
189
+
190
+ # MUTUAL_AID actions (only for unit types with no local availability)
191
+ # Use any existing unit as the "type selector".
192
+ available_types = {u.unit_type for u in units_sorted if u.status == UnitStatus.AVAILABLE}
193
+ type_to_template_unit: dict[object, str] = {}
194
+ for unit in units_sorted:
195
+ type_to_template_unit.setdefault(unit.unit_type, unit.unit_id)
196
+
197
+ for unit_type, template_unit_id in sorted(type_to_template_unit.items(), key=lambda kv: str(kv[0])):
198
+ if unit_type in available_types:
199
+ continue
200
+ for incident in active_incidents_sorted:
201
+ actions.append(
202
+ Action(
203
+ action_type=DispatchAction.MUTUAL_AID,
204
+ unit_id=template_unit_id,
205
+ incident_id=incident.incident_id,
206
  )
207
  )
208
+
209
+ # UPGRADE / DOWNGRADE actions (severity adjustments)
210
+ if ref_unit_id:
211
+ for incident in active_incidents_sorted:
212
+ if incident.status == IncidentStatus.RESOLVED:
213
+ continue
214
+
215
+ # These candidates are filtered by protocol validation at step-time,
216
+ # but we only generate the obviously-relevant ones.
217
+ if incident.severity == IncidentSeverity.PRIORITY_1:
218
+ actions.append(
219
+ Action(
220
+ action_type=DispatchAction.DOWNGRADE,
221
+ unit_id=ref_unit_id,
222
+ incident_id=incident.incident_id,
223
+ priority_override=IncidentSeverity.PRIORITY_2,
224
+ )
225
+ )
226
+ actions.append(
227
+ Action(
228
+ action_type=DispatchAction.DOWNGRADE,
229
+ unit_id=ref_unit_id,
230
+ incident_id=incident.incident_id,
231
+ priority_override=IncidentSeverity.PRIORITY_3,
232
+ )
233
+ )
234
+ elif incident.severity == IncidentSeverity.PRIORITY_2:
235
+ actions.append(
236
+ Action(
237
+ action_type=DispatchAction.UPGRADE,
238
+ unit_id=ref_unit_id,
239
+ incident_id=incident.incident_id,
240
+ priority_override=IncidentSeverity.PRIORITY_1,
241
+ )
242
+ )
243
+ actions.append(
244
+ Action(
245
+ action_type=DispatchAction.DOWNGRADE,
246
+ unit_id=ref_unit_id,
247
+ incident_id=incident.incident_id,
248
+ priority_override=IncidentSeverity.PRIORITY_3,
249
+ )
250
+ )
251
+ else:
252
+ actions.append(
253
+ Action(
254
+ action_type=DispatchAction.UPGRADE,
255
+ unit_id=ref_unit_id,
256
+ incident_id=incident.incident_id,
257
+ priority_override=IncidentSeverity.PRIORITY_2,
258
+ )
259
+ )
260
+ actions.append(
261
+ Action(
262
+ action_type=DispatchAction.UPGRADE,
263
+ unit_id=ref_unit_id,
264
+ incident_id=incident.incident_id,
265
+ priority_override=IncidentSeverity.PRIORITY_1,
266
+ )
267
+ )
268
+
269
+ # Filter out any actions that violate the protocol validator.
270
+ legal: list[Action] = []
271
+ for a in actions:
272
+ result = self._validator.validate(self._schema, state, a)
273
+ if result.ok:
274
+ legal.append(a)
275
+ return legal
276
 
277
  def step(self, state: State, action: Action) -> tuple[State, Observation]:
278
  validation = self._validator.validate(self._schema, state, action)
 
300
  self._apply_dispatch(state, action)
301
  elif action.action_type == DispatchAction.CANCEL:
302
  self._apply_cancel(state, action)
303
+ elif action.action_type == DispatchAction.REASSIGN:
304
+ self._apply_reassign(state, action)
305
+ elif action.action_type == DispatchAction.STAGE:
306
+ self._apply_stage(state, action)
307
+ elif action.action_type == DispatchAction.MUTUAL_AID:
308
+ self._apply_mutual_aid(state, action)
309
+ elif action.action_type in {DispatchAction.UPGRADE, DispatchAction.DOWNGRADE}:
310
+ self._apply_severity_change(state, action)
311
 
312
  state = self._tick(state)
313
 
 
393
  incident.status = IncidentStatus.PENDING
394
  incident.survival_clock = _severity_deadline_seconds(incident.severity)
395
 
396
+ def _apply_reassign(self, state: State, action: Action) -> None:
397
+ unit = state.units[action.unit_id]
398
+ new_incident = state.incidents[action.incident_id]
399
+
400
+ old_incident_id = unit.assigned_incident_id
401
+ old_incident = state.incidents.get(old_incident_id) if old_incident_id else None
402
+
403
+ # Remove from the old incident, if present.
404
+ if old_incident is not None and unit.unit_id in old_incident.units_assigned:
405
+ old_incident.units_assigned.remove(unit.unit_id)
406
+ if not old_incident.units_assigned and old_incident.status in {
407
+ IncidentStatus.RESPONDING,
408
+ IncidentStatus.ON_SCENE,
409
+ }:
410
+ old_incident.status = IncidentStatus.PENDING
411
+ old_incident.survival_clock = _severity_deadline_seconds(old_incident.severity)
412
+
413
+ # Assign to the new incident like a dispatch.
414
+ unit.status = UnitStatus.DISPATCHED
415
+ unit.assigned_incident_id = new_incident.incident_id
416
+
417
+ speed = float(self._schema.unit_speeds.get(unit.unit_type, 1.0))
418
+ dx = abs(unit.location_x - new_incident.location_x)
419
+ dy = abs(unit.location_y - new_incident.location_y)
420
+ manhattan_dist = dx + dy
421
+ eta = manhattan_dist / max(speed, 1e-6)
422
+ unit.eta_seconds = max(0.0, float(eta))
423
+
424
+ if unit.unit_id not in new_incident.units_assigned:
425
+ new_incident.units_assigned.append(unit.unit_id)
426
+ if new_incident.status == IncidentStatus.PENDING:
427
+ new_incident.status = IncidentStatus.RESPONDING
428
+
429
+ def _apply_stage(self, state: State, action: Action) -> None:
430
+ """Pre-position a unit towards an incident without counting as 'assigned'."""
431
+ unit = state.units[action.unit_id]
432
+ incident = state.incidents[action.incident_id]
433
+
434
+ speed = float(self._schema.unit_speeds.get(unit.unit_type, 1.0))
435
+ dx = abs(unit.location_x - incident.location_x)
436
+ dy = abs(unit.location_y - incident.location_y)
437
+ manhattan_dist = dx + dy
438
+ eta = manhattan_dist / max(speed, 1e-6)
439
+
440
+ unit.status = UnitStatus.DISPATCHED
441
+ unit.assigned_incident_id = incident.incident_id
442
+ unit.eta_seconds = max(0.0, float(eta))
443
+
444
+ def _apply_mutual_aid(self, state: State, action: Action) -> None:
445
+ """Request an external unit of the given type and dispatch it."""
446
+ template = state.units[action.unit_id]
447
+ incident = state.incidents[action.incident_id]
448
+
449
+ counter = int(state.metadata.get("mutual_aid_counter", 0)) + 1
450
+ state.metadata["mutual_aid_counter"] = counter
451
+
452
+ prefix = template.unit_type.value[:3]
453
+ new_unit_id = f"MA-{prefix}-{counter}"
454
+ new_unit_id = new_unit_id[:20]
455
+
456
+ speed = float(self._schema.unit_speeds.get(template.unit_type, 1.0))
457
+ dx = abs(template.location_x - incident.location_x)
458
+ dy = abs(template.location_y - incident.location_y)
459
+ manhattan_dist = dx + dy
460
+ base_eta = manhattan_dist / max(speed, 1e-6)
461
+ penalty = float(state.metadata.get("mutual_aid_eta_penalty", 120.0))
462
+
463
+ unit = UnitState(
464
+ unit_id=new_unit_id,
465
+ unit_type=template.unit_type,
466
+ status=UnitStatus.DISPATCHED,
467
+ location_x=float(template.location_x),
468
+ location_y=float(template.location_y),
469
+ assigned_incident_id=incident.incident_id,
470
+ eta_seconds=max(0.0, float(base_eta + penalty)),
471
+ crew_count=int(template.crew_count),
472
+ )
473
+ state.units[unit.unit_id] = unit
474
+
475
+ if unit.unit_id not in incident.units_assigned:
476
+ incident.units_assigned.append(unit.unit_id)
477
+ if incident.status == IncidentStatus.PENDING:
478
+ incident.status = IncidentStatus.RESPONDING
479
+
480
+ def _apply_severity_change(self, state: State, action: Action) -> None:
481
+ if action.priority_override is None:
482
+ return
483
+ incident = state.incidents[action.incident_id]
484
+ incident.severity = action.priority_override
485
+
486
+ # Update clocks based on current incident phase.
487
+ if incident.status in {IncidentStatus.PENDING, IncidentStatus.RESPONDING}:
488
+ incident.survival_clock = _severity_deadline_seconds(incident.severity)
489
+ elif incident.status == IncidentStatus.ON_SCENE:
490
+ incident.survival_clock = _resolve_timer_seconds(incident.severity)
491
+
492
  def _tick(self, state: State) -> State:
493
  state.step_count += 1
494
  state.city_time += DEFAULT_DT_S
tests/test_state_machine.py CHANGED
@@ -24,7 +24,44 @@ def test_legal_actions_non_empty_initially() -> None:
24
  state = sm.reset(task_id="single_incident", episode_id="ep-1")
25
  legal = sm.get_legal_actions(state)
26
  assert legal
27
- assert all(a.action_type in {DispatchAction.DISPATCH, DispatchAction.CANCEL} for a in legal)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  def test_invalid_action_yields_protocol_ok_false() -> None:
 
24
  state = sm.reset(task_id="single_incident", episode_id="ep-1")
25
  legal = sm.get_legal_actions(state)
26
  assert legal
27
+ assert any(a.action_type == DispatchAction.DISPATCH for a in legal)
28
+
29
+
30
+ def test_additional_actions_become_reachable() -> None:
31
+ schema = CitySchemaLoader.load("metro_city")
32
+ sm = DispatchStateMachine(schema=schema, seed=42)
33
+
34
+ # Multi-incident is a better reachability surface (multiple incidents + P2 incident).
35
+ state = sm.reset(task_id="multi_incident", episode_id="ep-1")
36
+ legal = sm.get_legal_actions(state)
37
+
38
+ assert any(a.action_type == DispatchAction.STAGE for a in legal)
39
+ assert any(a.action_type == DispatchAction.UPGRADE for a in legal)
40
+ assert any(a.action_type == DispatchAction.DOWNGRADE for a in legal)
41
+
42
+ # After a dispatch, REASSIGN should be legal to the other active incident.
43
+ dispatch = next(a for a in legal if a.action_type == DispatchAction.DISPATCH)
44
+ state, _ = sm.step(state, dispatch)
45
+ legal2 = sm.get_legal_actions(state)
46
+ assert any(a.action_type == DispatchAction.CANCEL for a in legal2)
47
+ assert any(a.action_type == DispatchAction.REASSIGN for a in legal2)
48
+
49
+
50
+ def test_mutual_aid_appears_when_type_exhausted() -> None:
51
+ schema = CitySchemaLoader.load("metro_city")
52
+ sm = DispatchStateMachine(schema=schema, seed=42)
53
+ state = sm.reset(task_id="multi_incident", episode_id="ep-1")
54
+
55
+ # Exhaust all MEDIC availability.
56
+ from src.models import UnitStatus
57
+
58
+ for unit in state.units.values():
59
+ if unit.unit_type.value == "MEDIC":
60
+ unit.status = UnitStatus.DISPATCHED
61
+ unit.assigned_incident_id = "INC-001"
62
+
63
+ legal = sm.get_legal_actions(state)
64
+ assert any(a.action_type == DispatchAction.MUTUAL_AID for a in legal)
65
 
66
 
67
  def test_invalid_action_yields_protocol_ok_false() -> None: