arXiv:2610.32648cs.SEOct 2026

Manuscript Offprint

arXiv:2610.32648

StateMirror: Pre-Execution Detection of Hallucinated Tool Calls in Coding Agents

Shiwen Shan, Yintong Huo, Zhiying Wu, Yuxin Su

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

SHIWEN SHAN, School of Software Engineering, Sun Yat-sen University, China

YINTONG HUO, Singapore Management University, Singapore

ZHIYING WU, School of Software Engineering, Sun Yat-sen University, China

YUXIN SU, School of Software Engineering, Sun Yat-sen University, China

Large language model (LLM)-based coding agents may propose invalid actions that are not applicable in the execution environment, such as accessing non-existent paths or missing packages. We term this mismatch hallucinated tool use. A preliminary study of 4,398 agent trajectories reveals that these hallucinations are pervasive and costly, requiring a median of 3 additional interaction steps and 22.4K tokens to recover. Existing frameworks rely on reactive, post-execution feedback, wasting runtime resources. Nevertheless, since an agent's execution environment is grounded in concrete codebase and harness components, many action prerequisites can be extracted and validated prior to execution.

To this end, we introduce StateMirror, a lightweight pre-execution detector. StateMirror intercepts actions at the harness boundary, extracts state predicates via AST and command analysis, and deterministically validates them against the live repository. By pinpointing exact environment mismatches before execution, StateMirror preempts invalid actions and subsequent recovery cycles. Evaluation on 2,508 actions shows StateMirror achieves 0.940 precision and 0.817 F1 in detecting such hallucinations, surpassing the strongest LLM judge by 35.3% in F1 while reducing median detection latency from 17.73 s to 1.50 ms with zero token consumption. For practical mitigation, StateMirror's pre-execution feedback enables agents to rectify over 95% of hallucinated tool calls. During live mitigation on 79 tasks, StateMirror reduces agent execution time by 15.8% and token consumption by 11.8% while maintaining task resolution.

1 Introduction

Modern coding agents solve software engineering tasks by iteratively interacting with their environments [2, 44]. These agents typically follow the reasoning-and-acting (ReAct) paradigm [50], using tool calls to inspect repository contents, modify files, and run tests. However, these actions often lack grounding in the workspace, leading to failures such as referencing invalid filesystem paths, missing modules, or undefined symbols.

We define this failure mode as hallucinated tool use, occurring when an agent's proposed action violates preconditions imposed by the actual environment [10, 24]. Unlike reasoning flaws or semantic code bugs that execute successfully but fail the task, hallucinated tool use embeds invalid references (e.g., non-existent entities) into tool calls, making them fundamentally unexecutable as intended. Figure 1 illustrates two hallucinated tool uses: (a) assumes an unexported symbol within a target namespace, (b) misinterprets the filesystem layout.

To understand the impact of hallucinated tool use, we conduct a preliminary study analyzing 4,398 SWE-agent trajectories on SWE-bench-Extra [26, 48], identifying 9,595 hallucinated tool calls across 120,801 total calls. Our study reveals three key findings. First, hallucinated tool use is pervasive, occurring in 46.6% of failed and 32.3% of resolved trajectories. Second, recovering from these hallucinations is costly. When a hallucinated tool call fails (e.g., with a ModuleNotFoundError), the agent may take several steps before reaching a grounded retry, incurring a median recovery

Authors' Contact Information: Shiwen Shan, School of Software Engineering, Sun Yat-sen University, Zhuhai City, China, shanshw@mail2.sysu.edu.cn; Yintong Huo, Singapore Management University, Singapore, ythuo@smu.edu.sg; Zhiying Wu, School of Software Engineering, Sun Yat-sen University, Zhuhai City, China, wuzhy286@mail.sysu.edu.cn; Yuxin Su, School of Software Engineering, Sun Yat-sen University, Zhuhai City, China, suyx35@mail.sysu.edu.cn.

  1. ACM XXXX-XXXX/2026/10-ART https://doi.org/10.1145/nnnnnnn.nnnnnnn

Shiwen Shan, Yintong Huo, Zhiying Wu, and Yuxin Su

(a) Missing export

(b) Layout confusion

Figure

→ predicate extracted from action → repository evidence

Fig. 1. Examples of hallucinated tool calls. (a) The action imports env, but the module exports only Env. (b) The action imports textual.src, but the layout is src/textual.

cost of 3 steps and 22.4K tokens. Furthermore, 30.3% of these hallucinations never reach a successful grounded retry. Third, their root causes are concentrated: filesystem mismatches (such as missing files or paths) account for 43.3% of the cases, and namespace mismatches (such as missing modules or undefined symbols) account for 41.9%. This concentration of verifiable error types indicates that most hallucinations can be validated before the step proceeds, thereby avoiding costly post-failure recovery.

However, existing approaches are not designed for such pre-execution grounding. They primarily target high-level inference flaws and often overlook tool hallucinations caused by inconsistencies with the execution environment. Some methods detect reasoning failures only through post-execution feedback [7, 9, 36], incurring runtime overhead after the erroneous action has already been executed. Others rely on LLM-based judges over execution histories [52], which introduce additional inference cost and may suffer from reasoning biases [56].

To address this gap, we present StateMirror, a lightweight detector that catches proposed hallucinated tool use before it proceeds. Because tool preconditions are grounded in verifiable environment artifacts, such as the repository codebase and runtime harness, we can validate them statically without executing the action. Specifically, StateMirror intercepts actions at the harness boundary and verifies their preconditions through a two-step process. First, a predicate extractor statically parses the proposed action to derive typed state predicates, such as importable or symbol_defined. Second, a read-only prober deterministically checks these predicates against the live execution environment. If any predicate is violated, StateMirror returns a typed warning detailing the exact mismatch. Operating deterministically without LLM invocation, StateMirror consumes no tokens, incurs minimal latency, and preempts costly post-execution recovery cycles.

We evaluate StateMirror on a benchmark of 2,508 expert-labeled actions from 1,197 public failed trajectories of four agent frameworks with different harness designs: SWE-agent, OpenHands, Sonar, and Honeycomb. Following the annotation workflow of our preliminary study, we retrieve candidate actions via failure keywords and tracebacks, replay prior edits to reconstruct the exact pre-action workspace, and have annotators label each action independently. StateMirror achieves a hallucination detection precision of 0.940 and an F1 score of 0.817, exceeding the strongest LLM judge by 0.213 F1 and a scaffold linter by 0.772 F1. Furthermore, StateMirror is also fast and cheap to run, completing each detection in a median of 1.50 ms with no token cost.

To show the practicality, we evaluate StateMirror on both immediate action repair and end-to-end task intervention. For action repair, StateMirror's diagnostic result enables agents to rectify 95.0% of hallucinated tool calls under controlled revision and 97.8% during live SWE-agent execution. In end-to-end intervention across 79 hallucination-affected tasks, StateMirror reduces execution time by 15.8% and token consumption by 11.8%, while maintaining task resolution. Contributions. This paper makes three contributions:

• Problem Formulation. We formalize the concept of hallucinated tool use, and empirically analyze 4,398 agent trajectories, characterizing its prevalence, costly recovery loops, and error patterns.

• Approach. We present StateMirror, a deterministic pre-execution detector that derives state predicates and validates them against the execution environment.

• Evaluation. We show that StateMirror achieves 0.940 precision in hallucination detection at millisecond-level latency. Furthermore, its pre-execution feedback rectifies 95% of invalid tool calls, reducing end-to-end task time by 15.8% and token costs by 11.8%.

References (21)