Execution-Grounded Agents: Enforcing Physical Constraints in AI Code Generation via Oracle Search
Abstract
Modern large language models are increasingly used inside scientific workflows, where the central failure mode is not syntax but reward misalignment: code can compile, execute, and look plausible while violating the physical semantics that make a result scientifically valid. This paper reframes scientific code assistance as reinforcement learning at inference time: the LLM is a base policy over candidate programs, and a deterministic scientific oracle acts as the environment that returns world feedback through execution exceptions, tolerance-checked numerical deviations, and domain-invariant violations. We present Oracle-Guided Trajectory Search (OGTS), a test-time compute algorithm that uses this feedback as a directional environmental reward by sampling candidate trajectories, evaluating them against scientific constraints, and greedily collapsing toward the most informative oracle trace. We evaluate this framing on a 50-task suite of scientific coding oracles and a reproduction of the 2014 ATLAS Higgs Boson Machine Learning Challenge, where Monte Carlo weights, missingvalue sentinels, and the regulated AMS discovery metric define deterministic environmental constraints that syntactic benchmarks do not enforce. OGTS raises an open-weight model, Llama-3.1- 70b, from a 58% to 84% pass rate by using oracle feedback to recover from systematic semantic errors, while retrieval remains useful as an audit layer for project evidence. We analyze the conditions under which OGTS outperforms sequential repair, showing that the standard failure-type taxonomy (exec/num/inv) is insufficient to predict strategy ordering; instead, within-category score diversity across branches emerges as the operationally relevant signal.