# Engine context: Bridge Triage Desk Give this whole file to your coding agent before it writes anything. Owner of `engine/rules.py`: Person B1. Owner of `engine/outputs.py` and `map/`: Person B2. ## 1. What we're building and why Hackathon: Grok Bot Hackathon, Massachusetts Infrastructure, Bridges track (Dec 5, 2026). Owner: MassDOT. Their problem: "inspection reports pile up faster than engineers can act on them." We build an agent that reads inspection reports, sorts every bridge into a group with evidence, estimates cost, picks the legal buying path, schedules the work, and drafts paperwork that a human engineer approves. Three Grok Bots (Inspector, Risk Engineer, Clerk) call THIS engine code. **The engine is plain Python + pandas. No LLM calls.** It must give the same answer every time (judges score "works on real data", "auditability", "human sign-off"). Our two selling points, both found in the data: 1. **The database lags the field.** Real 2025 inspection reports show bridges worse than the NBI database record (e.g. A-13-024 is "fair" in NBI but superstructure 4 = poor in its 2025 report). So we trust the NEWEST REPORT first and flag disagreements. 2. **Urgent structural findings have nowhere to go.** ~40 of 88 urgent findings need contractor work (crews aren't allowed to do it), but the work-order system only has 13 maintenance job types and none of those bridges is in the project list. We route them to a contractor request. ## 2. Repo layout ``` data/raw/ event data, unzipped here, NOT committed (in .gitignore) data/parsed/ reports.csv, defs.csv (already parsed from the simulated report PDFs) engine/ reference.py (helpers, tested), rules.py (B1), outputs.py (B2), output/ map/ bridge-map.html (B2) bots/ knowledge/ docs/ other people's folders: don't edit ``` Raw data path after unzipping the event zip into `data/raw/`: `data/raw/Grokathon-main/data/bridges/` (call this `RAW` below) ## 3. Input files and the columns you need **Join key everywhere:** NBI structure number, e.g. `A130240EFMUNNBI`. Report ID ("BR. DEPT. NO.") `A-13-024` = characters 1-6; BIN `0EF` = characters 7-9. Use `reference.bdept_from_sn()` and `bin_from_sn()`. | File | Use | Key columns | |---|---|---| | `RAW/DERIVED_bridges.csv` | 200 bridges, NBI 2025 record | `STRUCTURE_NUMBER_008`, `TOWN`, `LAT_016`, `LONG_017`, `DECK_COND_058`, `SUPERSTRUCTURE_COND_059`, `SUBSTRUCTURE_COND_060`, `CHANNEL_COND_061`, `CULVERT_COND_062`, `BRIDGE_CONDITION` (G/F/P), `LOWEST_RATING`, `OPEN_CLOSED_POSTED_041`, `DATE_OF_INSPECT_090` (MMYY int), `INSPECT_FREQ_MONTHS_091`, `HIGHWAY_DISTRICT_002`, `FACILITY_CARRIED_007`, `FEATURES_DESC_006A`, `YEAR_BUILT_027`, `ADT_029`, `MAIN_UNIT_SPANS_045` | | `data/parsed/reports.csv` | one row per simulated report | `file`, `cycle` (current/prior), `bdept`, `bin`, `town`, `sn` (= structure number), `date`, `type`, `r58` `r59` `r60` `r61` `r62` (ratings, 'N' = n/a), `freq` (months), `adt`, `status`, `scour`, `recs` | | `data/parsed/defs.csv` | one row per finding | `file` (joins reports.csv), `cycle`, `bdept`, `comp` (DECK/SUPER/SUB/CHANNEL/CULVERT), `elem` (e.g. Beams/girders, Bearings, Joints), `desc`, `qty`, `unit`, `sev` (Minor/Moderate/Severe/Critical), `urg` (Immediate/ASAP/Prioritize/Schedule) | | `RAW/SIMULATED_work_orders.csv` | ~3,000 maintenance jobs | `WORK_ORDER_ID`, `STRUCTURE_NUMBER_008`, `BDEPT`, `DATE_OPENED`, `WORK_TYPE`, `CATEGORY` (Routine/Repair/Preventive/Emergency), `CREW`, `EST_HOURS`, `STATUS` (Closed/Scheduled/Open/Deferred - winter), `DATE_CLOSED`, `ACTUAL_HOURS` | | `RAW/SIMULATED_crews_calendar.csv` | crews and contractors | `CREW`, `DISTRICT`, `CREW_SIZE`, `HOURS_PER_WEEK_AVAILABLE`, `CAN_DO`, `CANNOT_DO`, `WINTER_RESTRICTION`, `BOOKED_THROUGH` | | `RAW/DERIVED_unit_costs.csv` | real MassDOT bid prices (3 contracts) | `item_no`, `item_description`, `unit`, `quantity`, `office_estimate_unit`, `low_bid_unit`, `avg_other_bids_unit` | Use only `cycle == "current"` reports for current condition; use the newest one per bridge. Prior-cycle reports (29 bridges) are for "what changed" comparisons. **Real 2025 town reports** (PDFs in `RAW/docs/PUBLIC_inspection_reports/`, not in reports.csv). Use these ratings for these bridges, marked "to verify" until Person C confirms: | Structure number | ID | 2025 report deck / super / sub | Report date | |---|---|---|---| | A130240EFMUNNBI | A-13-024 | 6 / 4 / 6 | 2025-07-08 | | A130200FMMUNNBI | A-13-020 | 7 / 4 / 4 | 2025-07-07 | | A130150FKMUNNBI | A-13-015 | 5 / 5 / 6 | 2025-07 | | A130230FLMUNNBI | A-13-023 | 5 / 5 / 6 | 2025-07 | | A130250FJMUNNBI | A-13-025 | 7 / 6 / 7 | 2025-07 | | A13028C6VMUNNBI | A-13-028 | 8 / 8 / 8 | 2025-07-08 | ## 4. Rules (DRAFT: Person C will edit; keep them in one place so they're easy to change) Implemented in `engine/reference.py`: `needs_contractor()`, `assign_group()`, `records_disagree()`. Groups, checked in this order (first match wins): 1. `act`: any rating (report or NBI) of 3 or lower, OR any finding with urgency Immediate. 2. `contract`: any Immediate/ASAP finding that needs a contractor (structural steel on beams or diaphragms, frozen or tipped bearings, scour or undermining, abutment/pier/wingwall concrete with rebar, cracks, spalls, section loss, settlement, tilt). 3. `verify`: newest report's deck/super/sub differs from NBI on any one of them. 4. `plan`: any Immediate/ASAP/Prioritize finding crews can do. 5. `none`: only Schedule-level findings. 6. `noreport`: no report on file (NBI record only). Always set `records_disagree` and `contract_needed` as their own fields, even when a higher group wins. ## 5. Cost, buying path, schedule (B2) - **Cost:** map each finding to a bid pay item and multiply by quantity. Draft picks (Person C confirms): structural steel repairs `$50/LB` (item 107.97); rapid-hardening concrete repair `$5,000/CY` (item 909.5); asphalt patching `$339/TON` (item 451); anchor bolt corrective action at bearings `$2,500 EA` (item 913.21). No clear match yet for bearing replacement, scour protection, joints: leave `est_cost` null and say so in `reasons`. Never invent a price. - **Buying path** (`reference.buying_path`): under $10K sound business practices; $10K to $50K written scope + three written quotes; over $50K advertised sealed bid. Prevailing wage always. - **Schedule** (`reference.earliest_start`): today is 2026-12-05. In-house crews can't do deck concrete or joint work Dec 1 - Mar 31. Crews booked through `BOOKED_THROUGH` (District 1 crews 2026-12-20, District 6 crews 2027-01-15, both contractors 2027-02-28). Contractor work goes to the district's `*-CONTRACTOR` crew. Get the district from `HIGHWAY_DISTRICT_002` (60 bridges in District 1, 130 in District 6, 10 in District 4; the crew calendar only covers Districts 1 and 6, so flag District 4 bridges as "no crew data"). - Known data issue: 127 work orders use crew `D1-BRG3`, which isn't in the crew calendar. Flag it; don't crash. ## 6. The output file: `engine/output/bridges.json` A JSON list, one object per bridge (all 200). The bots, the map and the Excel sheet all read this. ```json { "structure_number": "A130240EFMUNNBI", "bdept": "A-13-024", "bin": "0EF", "town": "Ashfield", "lat": 42.5084, "lon": -72.776, "group": "verify", "reasons": ["Newest report superstructure 4 (poor); NBI still 6 (fair)"], "evidence": [{"source": "ashfield_A-13-024_williamsburg_rd_2025.pdf", "page": 1, "text": "Overall current condition 6 4 6"}], "nbi_ratings": {"deck": 6, "super": 6, "sub": 6, "date": "2023-03"}, "report_ratings": {"deck": 6, "super": 4, "sub": 6, "date": "2025-07-08", "source": "real town report, to verify"}, "records_disagree": true, "contract_needed": false, "findings": [{"part": "Super / Beams/girders", "desc": "...", "sev": "Severe", "urg": "ASAP", "contract": true}], "est_cost": null, "buying_path": "unknown", "earliest_start": null, "crew": null } ``` B1 fills everything through `findings`. B2 adds `est_cost`, `buying_path`, `earliest_start`, `crew`. Until B1's file exists, B2 makes a small fake file with these exact fields. ## 7. Test bridges (must pass; expected groups are drafts until Person C confirms) | ID | What's true | Expected | |---|---|---| | A-13-024 | NBI fair (6/6/6), 2025 report super 4 | `verify`, `records_disagree: true` | | C-20-004 | Report super 2, three Immediate critical findings; NBI status E (temporary structure in place) | `act`; reasons must mention the temporary structure | | N-14-007 | Report super 2, Immediate findings; NBI status K (closed) | `act`; reasons must mention the bridge is closed | | B-16-586 | Graffiti finding coded Immediate with severity Minor | `act` by the rule, plus a reason flagging "urgency/severity mismatch, check coding" | Run the engine twice; output must be identical. ## 8. Tasks **B1: `engine/rules.py`** produces `engine/output/bridges.json` (fields through `findings`) 1. Load the three inputs (section 3). Newest current report per bridge. 2. Override report ratings for the six real Ashfield bridges (table in section 3). 3. Compute `records_disagree`, `contract_needed`, `group`, `reasons`, `evidence`. 4. Add reasons for NBI status E (temporary structure) and K (closed), and for urgency/severity mismatches. 5. Pass the test bridges. `python engine/rules.py` writes the file. **B2: `engine/outputs.py`** reads bridges.json and adds cost, buying path, schedule; then produces: 1. `engine/output/review.xlsx`: one row per bridge that isn't `none`/`noreport`. Columns: Bridge ID, Town, Group, Why, Evidence, Suggested action, Estimated cost, Buying path, Earliest start, Decision (Approve / Edit / Reject, a dropdown), Engineer's note. Follow Person D's layout if it differs. 2. `engine/output/decision_log.csv`: who, when, bridge, decision, original vs edited. 3. `engine/output/work_orders_draft.csv`: same columns as `SIMULATED_work_orders.csv`, `STATUS = Draft - awaiting approval`. 4. Connect `map/bridge-map.html` to bridges.json (it currently has data embedded; replace with a load of the JSON or regenerate the embedded block from it). ## 9. Rules for the agent - Don't call any LLM or external API from the engine. - Don't invent prices, ratings or dates. Missing = null + a reason. - Keep all thresholds and keyword lists at the top of the file as constants so Person C's edits are one-line changes. - Simulated findings and work orders are synthetic; ratings and bridge identities are real. Say so in any output text that quotes numbers. - Don't commit `data/raw/`, API keys or `.env` files. - Git: work on your branch (`b1-rules` or `b2-outputs`), commit small, `gh pr create --fill` when something works. Person A merges.