From Resource Flow to Executable Tests: Petri-Net-Guided LLM Test Generation for Concurrent Stateful Rust APIs
Summary
SyncPetri introduces a novel methodology for generating executable tests for concurrent stateful Rust APIs by combining Petri nets and large language models (LLMs). This approach addresses the challenge of testing APIs with evolving resource ownership and complex interleavings, where LLMs alone often produce invalid or shallow tests. The method represents API resources and dependencies as colored Petri nets to derive legal deep-state, near-legal, and partial-order concurrent scenarios. These scenarios serve as a constrained intermediate representation for LLM-based code synthesis. A local-faithfulness contract and structural repair loop ensure modeled intent is preserved, while a multi-layer semantic oracle distinguishes synthesis failures from actual API bugs. A prototype for Rust concurrency libraries was evaluated, successfully generating 45 lines of code for a 7-event MPSC channel scenario with 2 legal linearizations, demonstrating executability and structural fidelity.
Key takeaway
For AI Engineers developing or testing concurrent Rust APIs, SyncPetri offers a structured approach to overcome the limitations of purely LLM-driven test generation. You should consider adopting this Petri-net-guided methodology to ensure your tests accurately reflect complex resource protocols and expose subtle race conditions. This method helps you generate semantically meaningful, executable tests that maintain fidelity to the intended API behavior, reducing false positives from concretization errors.
Key insights
Petri nets specify semantic intent and resource flow, while LLMs realize library-specific syntax and assertions for concurrency testing.
Principles
- Separate semantic intent from code realization in test generation.
- Use Petri nets to model resource flow, causality, and conflict.
- Enforce a local faithfulness contract at modeled event boundaries.
Method
SyncPetri synthesizes deep-state, near-legal, and partial-order scenarios from a colored Petri net, concretizes them via a constrained LLM prompt and repair loop, then judges execution with a multi-layer semantic oracle.
In practice
- Generate tests for Rust APIs with complex resource ownership.
- Explore deep-state traces and near-legal boundary conditions.
- Prioritize high-conflict concurrency skeletons for schedule exploration.
Topics
- Petri Nets
- LLM Test Generation
- Rust Concurrency
- Stateful API Testing
- Test Generation
- Tokio MPSC
Best for: Research Scientist, AI Scientist, Software Engineer, AI Engineer
Related on AIssential
See Counsel's argued verdicts on the open AI decisions leaders are weighing →
Editorial summary, takeaway, and curation by AIssential. Original article published by cs.SE updates on arXiv.org.