Preprint
Large Language Models

From Resource Flow to Executable Tests: Petri-Net-Guided LLM Test Generation for Concurrent Stateful Rust APIs

Kaiwen Zhang, Guanjun Liu
July 23, 2026

0

Citations

0

Influential Citations

Venue

2026

Year

Abstract

Concurrent stateful library APIs expose behavior through evolving resource ownership, lifecycle states, and competing interleavings. Large language models can synthesize executable Rust tests, but their outputs often violate API preconditions, remain shallow, or reduce concurrency to accidental sequential traces. Conversely, model-based and systematic testing techniques provide semantic control but commonly require substantial handwritten code to turn abstract scenarios into executable tests. This paper addresses the gap between formal scenario design and low-cost test concretization. We present a Petri-net-guided methodology for test generation over concurrent stateful Rust APIs. The method represents API resources, lifecycle conditions, and causal dependencies as colored tokens and transitions; derives legal deep-state, near-legal, and partial-order concurrent scenarios; and uses these scenarios as a constrained intermediate representation for LLM-based code synthesis. A local-faithfulness contract and structural repair loop preserve the modeled intent during concretization, while Petri-guided schedule shaping prioritizes high-conflict concurrency skeletons for systematic exploration. A layered semantic oracle then distinguishes synthesis failures from violations of the target API's expected behavior.

Analysis

Why This Paper Matters

Testing concurrent stateful APIs is notoriously difficult because of complex resource ownership, lifecycle states, and interleavings. Large language models (LLMs) can generate test code cheaply, but their outputs often violate API preconditions, remain shallow, or fail to exercise meaningful concurrency. Conversely, model-based testing provides semantic control but requires substantial handwritten code to turn abstract scenarios into executable tests. This paper addresses the critical gap between formal scenario design and low-cost test concretization, offering a principled way to combine the strengths of both approaches.

The significance lies in its potential to make systematic concurrency testing more accessible to practitioners. By using Petri nets as a formal intermediate representation, the method provides a structured way to guide LLMs toward generating tests that respect API semantics and explore interesting concurrent behaviors. This could reduce the manual effort currently required for model-based testing while improving the quality of LLM-generated tests.

Technical Contributions

  • Petri-net representation: Models API resources, lifecycle states, and causal dependencies as colored tokens and transitions, enabling formal reasoning about legal and near-legal scenarios.
  • Scenario derivation: Derives deep-state, near-legal, and partial-order concurrent scenarios from the Petri net, providing a constrained intermediate representation for LLM-based code synthesis.
  • Local-faithfulness contract and repair loop: Ensures that the generated code preserves the modeled intent during concretization, with a structural repair loop to fix violations.
  • Schedule shaping: Prioritizes high-conflict concurrency skeletons for systematic exploration, focusing testing effort on the most interesting interleavings.
  • Layered semantic oracle: Distinguishes synthesis failures (e.g., LLM errors) from violations of the target API's expected behavior, improving diagnostic clarity.

Results

The paper does not present experimental results or quantitative metrics. It is a methodology paper that describes the framework and its components. The evaluation likely appears in a future extended version or companion paper. The absence of results limits immediate assessment of effectiveness, but the conceptual contributions are clear.

Significance

This work has the potential to advance the state of the art in automated test generation for concurrent systems, particularly in the Rust ecosystem where memory safety and concurrency correctness are paramount. By combining formal methods with LLMs, it offers a path toward more reliable and cost-effective testing. The approach could generalize to other languages and domains where stateful concurrent APIs are common. For AI practitioners, it demonstrates a practical integration of symbolic reasoning (Petri nets) with neural generation (LLMs), a promising direction for improving the trustworthiness of AI-generated code.