LLMs write PLC code that compiles and is still unsafe, so I gated it behind a real compiler and a soft-PLC test harness

Reddit r/AI_Agents Tools

Summary

A practitioner builds a deterministic four-gate validation harness (graph check, safety audit, headless CODESYS compile, soft-PLC scan-cycle tests) to verify LLM-generated IEC 61131-3 PLC programs, showing that vendor compilers and simulators are better judges than LLM-as-judge for safety-critical code.

've been having LLM agents generate PLC programs (IEC 61131-3 ladder and function-block diagrams, exported as PLCopen XML) and found that "looks right" means very little. Raw output fails in three ways: Crashes the IDE importer (CODESYS throws KeyNotFoundException), or imports into the wrong place, so the build reports 0 errors because it compiled nothing. Graph shapes the real tool never produces: one input wired to several block pins, IDs in arbitrary order. Compiles fine but is unsafe: a set-dominant latch (start + stop together → motor runs), or a start that re-triggers when a trip clears. So nothing is accepted until it passes four gates. Each gate is deterministic code; the LLM never grades itself: Graph check: dangling references, undeclared variables, missing power rails. Safety audit: reset-dominant latch, rising-edge start, fail-safe (active-low) trip inputs. Real compiler: headless CODESYS.exe (V3.5 SP21) import + build, which must report 0 errors. Scan-cycle tests: a test program runs on the CODESYS soft-PLC and drives inputs scan by scan. Example: start held while a trip clears → the motor must stay off. Failures go back to the generating agent as structured errors. Programs that pass get re-exported by CODESYS and indexed as retrieval examples, so the model learns from real tool output rather than its own guesses. Results on two programs (pump station in ladder, conveyor in function blocks): all 4 gates pass; tests of 17 and 28 steps. Gates 1–2 take about 1 ms each. What it misses (found by deliberately breaking programs): the safety audit catches a swapped latch type in function blocks but not in ladder, because its tracer doesn't follow ladder contacts. Fixing that is next. Takeaway: for code with physical consequences, the vendor's own compiler and simulator make better judges than any LLM-as-judge. Questions: Has anyone built similar validators for other domains where the output is structured but the real check is a vendor tool? How do you keep LLM output from failing at the same rule over and over: fix it automatically with a deterministic normaliser, or teach the model through retrieved examples?
Original Article

Similar Articles

Reproducing History Is Not Enough: Intervention-Level Assurance for LLM-Generated Simulators

arXiv cs.AI

The paper proposes Vouch, a library for intervention-level assurance of LLM-generated simulators, which checks whether individual what-if answers can be trusted rather than giving a whole-program pass/fail verdict. Evaluated across three program-generation platforms, five LLMs and four domains, Vouch removed 97.6% of wrong answers while still trusting 80.1% of all answers.