replay
agentevac.utils.replay
Record and replay LLM-driven actions for deterministic re-runs.
This module provides RouteReplay, a class that operates in one of two modes:
record — During a live simulation run, every replay-relevant action is logged
to a JSONL file (one JSON record per line) along with agent cognition snapshots
and LLM dialog transcripts. Replay currently consumes:
- departure_release events for vehicle release timing
- route_change events for route application
Cognition and dialog events are write-only metadata for research/debugging.
Three output files are created:
- ``routes_<run_id>.jsonl`` — Replayable route-change schedule.
- ``routes_<run_id>.dialogs.log`` — Human-readable LLM dialog transcript.
- ``routes_<run_id>.dialogs.csv`` — Machine-readable LLM dialog table.
replay — Loads a previously recorded JSONL file and, on each simulation step,
releases vehicles according to the recorded departure_release schedule and
applies the scheduled route_change events to the matching vehicle via
traci.vehicle.setRoute(). This allows exact behavioural reproduction without
making any OpenAI API calls.
Important constraint: traci.vehicle.setRoute() requires that the first edge in the
supplied route is the vehicle's current edge. Both record and replay logic enforce
this by slicing the stored route to start from the current edge when needed.
RouteReplay
Record/replay LLM-applied routes. We log explicit edge lists and replay by calling traci.vehicle.setRoute(). Note: setRoute requires that the first edge is the vehicle's current edge. :contentReference[oaicite:3]{index=3}
Source code in agentevac/utils/replay.py
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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
apply_step(step, controlled_vehicle_ids)
Apply recorded actions scheduled for this step (if any), for vehicles currently in the sim.
Source code in agentevac/utils/replay.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
close()
Flush and close all open file handles. Safe to call more than once.
Source code in agentevac/utils/replay.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
departure_record_for_step(step, veh_id)
Return the recorded departure-release event for one vehicle at one step.
Source code in agentevac/utils/replay.py
323 324 325 326 327 | |
get_edge_trace(veh_id)
Return the recorded edge trace for a vehicle, or None.
Source code in agentevac/utils/replay.py
350 351 352 | |
has_departure_schedule()
Whether the loaded replay log contains explicit departure-release events.
Source code in agentevac/utils/replay.py
329 330 331 | |
has_edge_traces()
Whether the loaded replay log contains edge-trace events.
Source code in agentevac/utils/replay.py
354 355 356 | |
record_agent_cognition(step, sim_t_s, veh_id, control_mode, phase, belief, psychology, env_signal=None, social_signal=None, context=None)
Record the agent's subjective state for research/debugging. This is write-only metadata and is ignored by route replay.
Source code in agentevac/utils/replay.py
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 | |
record_departure_release(step, sim_t_s, veh_id, from_edge, to_edge, reason=None)
Log one vehicle release event to the JSONL file.
This is replayable metadata used to reproduce the actual departure timing of each vehicle in replay mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
SUMO simulation step index. |
required |
sim_t_s
|
float
|
Simulation time in seconds. |
required |
veh_id
|
str
|
Vehicle ID. |
required |
from_edge
|
str
|
Spawn edge used to initialize the route. |
required |
to_edge
|
str
|
Initial destination edge used to initialize the route. |
required |
reason
|
Optional[str]
|
Optional departure reason. |
None
|
Source code in agentevac/utils/replay.py
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 | |
record_edge_trace(veh_id, edges)
Log the complete sequence of edges a vehicle actually traversed.
Written once per vehicle (at arrival or simulation end). In replay mode,
the edge trace is loaded by _load_schedule and used to set the vehicle's
route to the exact edge sequence from the original run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
veh_id
|
str
|
Vehicle ID. |
required |
edges
|
List[str]
|
Ordered list of SUMO edge IDs the vehicle crossed. |
required |
Source code in agentevac/utils/replay.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
record_llm_dialog(step, sim_t_s, veh_id, control_mode, model, system_prompt, user_prompt, response_text=None, parsed=None, error=None)
Record one LLM interaction for audit/debugging. This is write-only metadata and is not used for replay routing actions.
Source code in agentevac/utils/replay.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
record_metric_snapshot(step, sim_t_s, snapshot_type, metrics_row)
Record aggregate metrics snapshots over the course of a run. This is write-only metadata and is ignored by route replay.
Source code in agentevac/utils/replay.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
record_route_change(step, sim_t_s, veh_id, control_mode, choice_idx, chosen_name, chosen_edge, current_edge_before, applied_route_edges, reason=None)
Log one LLM-applied route change to the JSONL file.
Trims applied_route_edges to start from current_edge_before so the
stored route is immediately compatible with traci.vehicle.setRoute() during
replay without additional adjustment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
SUMO simulation step index. |
required |
sim_t_s
|
float
|
Simulation time in seconds. |
required |
veh_id
|
str
|
Vehicle ID. |
required |
control_mode
|
str
|
|
required |
choice_idx
|
int
|
Index of the chosen option in the menu. |
required |
chosen_name
|
str
|
Name of the chosen destination/route. |
required |
chosen_edge
|
Optional[str]
|
Target destination edge (destination mode) or |
required |
current_edge_before
|
str
|
Edge the vehicle was on when the decision was applied. |
required |
applied_route_edges
|
List[str]
|
Full edge list passed to SUMO. |
required |
reason
|
Optional[str]
|
Optional human-readable reason string from the LLM. |
None
|
Source code in agentevac/utils/replay.py
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 | |
record_system_observation(step, sim_t_s, veh_id, observation)
Record one simulator-authored observation update for an agent.
These events are written for audit/debugging only and are ignored by replay action application.
Source code in agentevac/utils/replay.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |