Build a Rail-Crossing Safety Sandbox¶
| Category | Creative & Real-World AI Builds |
| Status | โ Completed |
| Repo | nextwork-build-a-rail-crossing-safety-sandbox |
| Original NextWork guide | nextwork.ai |
Learning project
Practice work following a NextWork guide, separate from my production projects.
๐ NextWork learning project. This is a practice project I built while working through NextWork's Build a Rail-Crossing Safety Sandbox guide. It is part of my Learning portfolio and is fully separate from my production projects.
Category: Learning โบ Creative & Real-World AI Builds
Status: โ
Completed
Overview¶
This is a small, deterministic safety simulation. Each fictional scene (late signal activation, blocked exit, low-confidence weather, and two labelled reconstructions) is replayed through a naive planner and a safety-first planner. The report shows where the naive planner ends up stopped inside the protected zone over the tracks.
Summary¶
Five deterministic replays, a naive-vs-safe planner report (Markdown, HTML, SVG), and the speed ร deceleration sweep (secret mission: the commit boundary) all run. The vision step runs on a real vision model: two drawn dashcam stills become validated 7-field Observation objects, which feed the same planners. The model is qwen2.5vl:3b, a local open model through Ollama, standing in for the guide's hosted model. Its answers are cached, so replays never send another image request. 9 tests pass.
The checks behind this are in docs/verification.txt.
Skills & tools¶
Python, Safety-case thinking, Simulation, SVG/HTML reports, pytest
What I built¶
sandbox.py: the scenes, stopping-distance physics, both planners, scoring, the report writer (report.md,report.html,report.svg), and the sweep.docs/report.*: the generated reports.test_sandbox.py: determinism, the naive planner's in-zone stop, the safe planner never scoring lower, blocked-exit handling, and the STOP/CLEAR switch in the sweep.make_frames.py: draws two fictional dashcam stills (gates down + lamps lit + train approaching; gates up + clear), so no real footage or licensing is involved.vision.py(my own code): picture โObservation(lamps_on, barrier, train_in_view, car_position, road_ahead_clear, certainty, reason). It uses my own prompt and schema; pydantic rejects any answer outside the schema, and answers are saved inperceptions/.perceive_and_plan.py+docs/vision-replay.md: saved Observation โ Scene โ both planners, with each model field next to my own label. On the 3 Dec rerun the 3B model got 6 of 10 fields right. It missed the train, put the car on the rails in the active frame (so the safe planner said CLEAR where STOP was right), and saw lit lamps and a lowered barrier in the clear frame. The point of the table is that these mistakes are visible.test_vision.py: the schema refuses bad answers and file types, observations steer the planner the right way (STOP before the barrier, CLEAR once on the rails, STOP with a queue ahead), and the replay never calls the model.
How to run¶
pip install pytest
python sandbox.py # writes report.md / .html / .svg
pytest -q
Verification¶
I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.
python3 sandbox.py && mkdir -p docs && cp report.md report.html report.svg docs/ && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider && /workspace/nextwork-portfolio/.venv/bin/python make_frames.py && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider test_vision.py && /workspace/nextwork-portfolio/.venv/bin/python perceive_and_plan.py | grep -q 'Fields where the model and I disagree'
Notes¶
Model substitution: the guide uses a hosted vision model with structured output. I don't have that API key, so a local qwen2.5vl:3b in Ollama answers in my own JSON schema instead.
Rewritten 3 Dec 2026: the earlier vision adapter followed the guide's code and prompt. vision.py, its schema and prompt, the glue and the tests are now my own, and the model answers were regenerated (the previous run scored 6 of 8; this one scored 6 of 10 on more fields).
Original project: https://nextwork.ai/projects/512ec31c-f113-4a33-ac8b-0fbce928bdae ยท Part of my NextWork Learning portfolio (hub).
Verification output¶
From docs/verification.txt:
$ python3 sandbox.py && mkdir -p docs && cp report.md report.html report.svg docs/ && PATH=/workspace/nextwork-portfolio/.venv/bin:$PATH python -m pytest -q -p no:cacheprovider && /workspace/nextwork-portfolio/.venv/bin/python make_frames.py && /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider test_vision.py && /workspace/nextwork-portfolio/.venv/bin/python perceive_and_plan.py | grep -q 'Fields where the model and I disagree'
# Rail-crossing safety report (fictional sandbox)
| Scene | Naive | Score | Safe | Score |
|---|---|---|---|---|
| late_activation | CLEAR | 70 | CLEAR | 70 |
| traffic_blockage | CLEAR (in zone!) | 0 | STOP | 100 |
| low_confidence_weather | CLEAR | 40 | STOP | 100 |
| reconstruction_A | STOP | 100 | STOP | 100 |
| reconstruction_B | STOP (in zone!) | 0 | CLEAR | 70 |
Naive total: 210 / Safe total: 440
## Speed x deceleration sweep (active signal, 30 m from the line)
| km/h | 2.0 m/s2 | 3.0 m/s2 | 4.0 m/s2 | 5.0 m/s2 | 6.0 m/s2 |
|---|---|---|---|---|---|
| 20 | STOP | STOP | STOP | STOP | STOP |
| 30 | STOP | STOP | STOP | STOP | STOP |
| 40 | CLEAR | STOP | STOP | STOP | STOP |
| 50 | CLEAR | CLEAR | CLEAR | STOP | STOP |
| 60 | CLEAR | CLEAR | CLEAR | CLEAR | CLEAR |
| 70 | CLEAR | CLEAR | CLEAR | CLEAR | CLEAR |
| 80 | CLEAR | CLEAR | CLEAR | CLEAR | CLEAR |
| 90 | CLEAR | CLEAR | CLEAR | CLEAR | CLEAR |
| 100 | CLEAR | CLEAR | CLEAR | CLEAR | CLEAR |
......... [100%]
9 passed in 0.03s
wrote frames/active.png frames/clear.png
.... [100%]
4 passed in 0.02s
[exit code 0]