A dual-drive class-level code generation method and system based on multi-agent collaboration
By employing a dual-drive approach involving multi-agent collaboration, a three-stage mechanism of dependency analysis, test-driven, and design-driven approaches is constructed. This addresses the issues of unreasonable dependencies and insufficient verification in class-level code generation, thereby improving the structural consistency and correctness of generated code.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHONGQING UNIV OF POSTS & TELECOMM
- Filing Date
- 2026-05-09
- Publication Date
- 2026-07-31
AI Technical Summary
Existing technologies for class-level code generation suffer from problems such as unreasonable dependencies between methods, lack of structural constraints, and lack of systematic verification mechanisms, leading to poor code generation consistency and difficulty in timely error detection.
A dual-drive approach with multi-agent collaboration is adopted. The dependency analysis module builds intra-class dependencies, the test-driven module performs iterative verification, and the design-driven module introduces UML activity diagrams for logical modeling, forming a three-stage collaborative mechanism of dependency analysis, test-driven, and design-driven. By leveraging the division of labor and cooperation among coding, testing, logic design, and expert agents, structural constraints and systematic verification of code generation are achieved.
It significantly improves the structural integrity, semantic consistency, and accuracy of class-level code generation, enhances the robustness and generalization ability of complex tasks, and solves the problems of inconsistent code generation results and difficulty in correcting errors in existing technologies.
Smart Images

Figure CN122489043A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence and software engineering technology, specifically relating to a dual-drive class-level code generation method and system based on multi-agent collaboration. Background Technology
[0002] Code generation technology refers to the automatic generation of corresponding code snippets based on tasks described in natural language, and it plays a significant role in improving development efficiency in the field of software engineering. In recent years, with the emergence of many high-performance large language models, such as the closed-source models ChatGPT and Claude, and the open-source models CodeLlama and Deepseek, these models have all demonstrated excellent code generation capabilities, making code generation research based on large language models a continuous focus of attention in the field of software engineering.
[0003] Existing research on code generation based on large language models has been extensively explored. Early studies sampled the large language model multiple times and then selected the optimal code snippets according to defined rules. Zero-shot and few-shot techniques significantly improved code generation by providing several examples to the large language model. A series of prompting strategies, such as thought chains, structured thought chains, and thought trees, have been introduced to effectively guide the large language model in generating code through explicit modeling and step-by-step reasoning. Furthermore, during the generation process, the large language model performs self-planning, self-debugging, and self-optimization operations, and through multi-stage iteration and feedback mechanisms, it can continuously drive the code towards better performance.
[0004] Recently, researchers proposed a multi-agent framework designed to guide large language models through various workflows, including task requirements analysis, code generation, and bug fixing, using different cues. This framework employs a decomposition-based technique, breaking down complex tasks into simpler, more manageable sub-steps. Each agent completes specific sub-tasks independently or collaboratively, gradually building the final solution. This not only improves response accuracy but also enhances adaptability to complex code generation tasks. Furthermore, classic software engineering methods (such as test-driven development and pair programming) are integrated into the workflow, providing iterative feedback to the large language model to optimize the correctness of the generated code.
[0005] Despite significant progress in code generation research, two key issues remain unresolved:
[0006] (1) Insufficient exploration of effective methods for class-level code generation. Class-level code generation not only requires all methods within a class to be functionally correct, but also needs to ensure reasonable dependencies between methods and between methods and fields. Currently, the performance of class-level code generation based on large language models has significantly decreased. Given that classes occupy a core position in software systems, improving the performance of class-level code generation has significant practical value and can lay the foundation for applications in more complex scenarios such as repository-level code generation.
[0007] (2) Existing research has overlooked the potential of using artifacts to improve code generation performance. Artifacts refer to all deliverables or intermediate products generated during software development. In real-world development scenarios, human developers often create various artifacts (such as test cases and unified modeling languages) to assist in coding work based on task complexity. However, existing methods typically only take task descriptions as input and generated code as output, lacking consideration for other artifacts involved in the generation process. Furthermore, if code generation methods based on large language models can produce such artifacts during the generation process, and these artifacts can effectively improve code generation performance, this will lay the foundation for future research on human-in-the-loop code generation. This research direction aims to emphasize the interactive feedback between large language models and human experts. Summary of the Invention
[0008] To address the aforementioned technical problems, this invention provides a dual-drive class-level code generation method and system based on multi-agent collaboration;
[0009] On one hand, this invention provides a dual-drive class-level code generation method based on multi-agent collaboration, comprising:
[0010] S1. Obtain the class-level code to generate the task description, perform semantic parsing on the task description, extract the class name, member attributes, member methods and their functional descriptions, and construct the task semantic representation;
[0011] S2. The coder agent performs dependency analysis and constructs an intra-class dependency representation:
[0012] S3. Test design: The intelligent agent generates test cases for each member method.
[0013] S4. The coding agent generates initial class-level code based on the intra-class dependency representation and class-level code requirement description.
[0014] S5. Perform test consistency verification based on the generated class-level code. When the test result shows that the initial class-level code passes the test cases, confirm that the code implementation is correct and mark it as passed. When the test result shows that the class-level code fails the test cases, enter the collaborative iteration process.
[0015] S6. Collaborative Iteration: Based on test feedback, the coding agent analyzes the root causes of the current class-level code and decides whether to generate a code correction plan. Based on test feedback, the testing agent analyzes the coverage, boundary conditions, and expected output correctness of the current test cases and decides whether to generate a test case correction plan. Based on test feedback, the coding agent analyzes the logical integrity, boundary scenario handling capabilities, output accuracy, and exception tolerance mechanisms of the current class code and decides whether to generate a code correction plan. Based on the code correction plan output by the coding agent and the test case correction plan output by the testing agent, it is determined whether the two parties have reached an agreement. If the iteration count has not reached the preset upper limit, the process returns to step S5 to re-verify the test consistency. If the iteration count reaches the preset upper limit, the code still fails the test cases, and the coding agent and the testing agent still have not reached an agreement, the process proceeds to step S7.
[0016] S7. Expert agent performs test case correctness verification: The expert agent verifies the correctness of the test cases. If the expert agent determines that the test cases are correct, proceed to step S8; if the expert agent determines that the test cases are incorrect, the expert agent updates the test cases and verifies them against the latest class-level code; if there are member methods that fail the test verification, proceed to step S8; if all member methods pass the test verification, proceed to step S14 to output the complete class-level code.
[0017] S8. Generating UML Activity Diagrams for Logical Design Agent: For the methods that failed the test verification in step S7, the logical design agent generates corresponding UML activity diagrams based on their functional descriptions, intra-class dependency representations, and test failure feedback information.
[0018] S9. Code correction based on UML activity diagram: The coded agent outputs updated member method code based on the UML activity diagram;
[0019] S10. Perform test verification of UML-driven code: Use the test cases in step S7 to test and verify the updated code output in step S9, and obtain the test results; if the test results show that the updated member method code passes the test cases, it is marked as passed, and steps S8 to S10 are repeated for the remaining member methods to be corrected; if the test results show that the updated code fails the test cases, proceed to step S11.
[0020] S11. The logic design agent iteratively corrects the UML activity diagram based on the test failure information: Based on the test failure feedback information from step S10, the logic design agent performs cause analysis on the UML activity diagram and generates a UML correction plan to obtain an updated UML activity diagram. At the same time, the UML iteration count is incremented by one. When the UML iteration count has not reached the preset upper limit, the process returns to step S9, corrects the code based on the updated UML activity diagram, and tests again. When the UML iteration count reaches the preset upper limit and the corresponding method still fails the test case, the process proceeds to step S12.
[0021] S12. Expert agent intervenes to verify and correct the UML activity diagram: The expert agent verifies the correctness of the current UML activity diagram and outputs the corrected UML activity diagram based on the test failure feedback information.
[0022] S13. Code generation and test verification of the expert agent's modified UML activity diagram: The coding agent modifies the member method code according to the UML activity diagram modified by the expert agent in step S12, and uses the corresponding test cases to test and verify the code to obtain the final test results.
[0023] S14. Output the final class-level code result: Regardless of whether the final test result described in step S13 passes the test cases, the final member method code is integrated with the other member method code in the class to generate the final class-level code result and output it.
[0024] On the other hand, the present invention provides a dual-drive class-level code generation system based on multi-agent collaboration, including: a dependency analysis module, a test-driven module, and a design-driven module;
[0025] The dependency analysis module includes a task description input unit, a dependency analysis unit, and a dependency artifact generation unit.
[0026] The task description input unit is used to receive task description information for generating class-level code;
[0027] The dependency analysis unit is used to perform semantic parsing on task description information. By analyzing class structure, method signatures and calling relationships between methods, it identifies the dependency relationships between methods and the dependency relationships between methods and fields, thereby constructing a dependency relationship model in the program structure.
[0028] The dependency artifact generation unit is used to generate dependency artifacts based on the dependency analysis results. The dependency artifacts are used to represent the calling relationship between methods in a class and the access relationship between methods and properties, and provide dependency constraint information for the subsequent code generation stage to guide the method implementation order and logical structure in the subsequent code generation process.
[0029] The test-driven module includes a class code generation unit, a test case generation unit, and a test execution and feedback unit.
[0030] The class code generation unit is used to generate an initial implementation of class code based on dependent artifacts and task description information, including class structure definition, method framework, and logical implementation of methods in the class;
[0031] The test case generation unit is used to generate corresponding test cases based on the class requirement description. The test cases include method input data, expected output results, and corresponding test assertions.
[0032] The test execution and feedback unit is used to execute test cases on the generated class code and determine whether each method passes the test based on the test execution results. When the test results show that all methods pass the test, a success result is output. When there are methods that fail the test, the test failure information is fed back to the class code generation unit and the test case generation unit to adjust and update the class code or test cases and re-execute the test. If there are still methods that fail the test after reaching the specified threshold, an expert review test case mechanism is implemented, thereby forming a test-driven iterative code generation process.
[0033] The design-driven module includes an activity graph generation unit, an activity graph-based code repair unit, and a code generation unit;
[0034] The activity diagram generation unit is used to generate UML activity diagrams for failed methods based on class requirement descriptions, etc. The activity diagrams are used to describe the logical relationships such as control flow, condition judgment, and loop structure during program execution.
[0035] The activity diagram-based code repair unit is used to analyze and repair the existing code structure according to the generated activity diagram. By mapping the control flow structure in the activity diagram to the program code, the logical structure in the code is supplemented or adjusted to ensure that the code logic is consistent with the program behavior described by the activity diagram.
[0036] The code generation unit is used to generate the final code implementation based on the repaired program structure, and to output the code in a structured manner to form complete class code that meets the functional requirements.
[0037] The beneficial effects of this invention are:
[0038] 1. To address the problems in existing technologies where the code generation process lacks structural constraints and the modeling of intra-class member relationships is insufficient, resulting in poor overall consistency of class-level code and logical conflicts in the calling relationships between methods, this invention sets up a dependency analysis module to structurally model the dependency relationships between methods in a class and between methods and member attributes, and uses this as a constraint condition in the subsequent generation process. This mechanism ensures the consistency and rationality of intra-class structural relationships, and improves the structural integrity and semantic consistency of the generated code.
[0039] 2. To address the problem that existing technologies lack a systematic verification mechanism for code generation results, often relying on single-output generation and making it difficult to detect and correct errors in a timely manner, this invention constructs a test-driven closed-loop mechanism. This mechanism transforms the code generation process from a "single output" to a closed-loop optimization process of "generation-verification-correction" by having a test agent generate test cases, an encoding agent generate code, and multiple rounds of consistency iteration. Simultaneously, an expert agent is set up as the final arbiter to avoid the non-convergence problem caused by infinite iteration, thereby significantly improving code accuracy and generation stability while ensuring system termination.
[0040] 3. To address the issues of control flow errors, missing boundary conditions, and incomplete loop logic that easily arise in the existing direct generation mode of complex method logic under large models, this invention introduces a design-driven module. In addition to the test-driven approach, a UML activity diagram modeling stage is added. The logic design agent explicitly and structurally expresses the internal control logic of the method, and uses the UML activity diagram as the basis for code generation. This transforms implicit semantic reasoning into an explicit structural modeling process, improving the generation accuracy and interpretability in complex control flow scenarios.
[0041] 4. This invention constructs a three-stage collaborative mechanism of "dependency analysis—test-driven—design-driven," and introduces a multi-agent collaborative structure involving coding agents, testing agents, logic design agents, and expert agents. This enables the code generation process to possess hierarchical decision-making and role-based error correction capabilities. Compared to existing single-model end-to-end generation methods, this significantly enhances the system's robustness and generalization ability in class-level complex tasks. Therefore, this invention does not simply call and superimpose multiple large language models, but rather constructs a multi-agent collaborative generation framework with clear division of labor and feedback paths, forming a dual closed-loop optimization structure. This distinguishes it from existing technologies in terms of technical means and achieves a simultaneous improvement in generation accuracy and structural consistency in terms of technical effect. Attached Figure Description
[0042] Figure 1 A flowchart illustrating a dual-drive class-level code generation method based on multi-agent collaboration, provided in an embodiment of the present invention;
[0043] Figure 2This is a structural diagram of a multi-agent collaborative dual-drive class-level code generation system provided in an embodiment of the present invention;
[0044] Figure 3 A flowchart for the reliability evaluation of test cases provided in this embodiment of the invention;
[0045] Figure 4 A schematic diagram of the unit structure of the dependency analysis stage in a multi-agent collaborative dual-drive class-level code generation system provided in an embodiment of the present invention;
[0046] Figure 5 A schematic diagram of the unit structure of the test-driven coding stage in a multi-agent collaborative dual-drive class-level code generation system provided in an embodiment of the present invention;
[0047] Figure 6 This is a schematic diagram of the unit structure of the UML-driven coding stage in a multi-agent collaborative dual-drive class-level code generation system provided in an embodiment of the present invention. Detailed Implementation
[0048] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0049] like Figure 1As shown, this invention employs a dual-driven class-level code generation method based on multi-agent collaboration. First, a class-level code generation task description is obtained and parsed. Second, the coding agent performs semantic analysis on the task representation, identifying call dependencies between methods in the class and dependencies between methods and member attributes, constructing an intra-class dependency representation as a structural constraint for subsequent code generation. Then, the test-driven generation phase begins. The test agent generates test cases for each member method, including method input data, expected output results, and corresponding test assertions; the coding agent generates initial class-level code based on the class-level code generation task description and intra-class dependencies. Next, the generated code is tested and verified. If the test fails, the coding agent and the test agent perform collaborative self-checking based on the test failure information, correcting the code or test cases and retesting within a preset iteration limit. If no consensus is reached even after reaching the iteration limit, the expert agent performs a one-time verification of the test cases, determines the modification of the test cases based on the verification results, and performs testing and verification again. If any member methods fail the tests after the test-driven phase, the process moves to the design-driven reinforcement phase. The logic design agent generates or modifies a UML activity diagram (describing the control logic structure of the method) for the corresponding method based on the test failure information. The coding agent rewrites or optimizes the code of the corresponding method according to the UML activity diagram and performs testing again. If the tests still fail within a preset iteration limit, an expert agent performs a one-time correction to the UML activity diagram, generates the final code based on the expert-corrected UML activity diagram, and performs final testing. Finally, the final class-level code result is generated and output; regardless of whether all final tests pass, the currently generated class-level code is output as the final result.
[0050] like Figure 2 As shown in the diagram, this embodiment presents a schematic of a multi-agent collaborative dual-drive class-level code generation system architecture, mainly divided into three parts: a dependency analysis module, a test-driven module, and a design-driven module. These modules communicate with each other and operate in a collaborative manner.
[0051] I. Dependency Analysis Module: Specific functions are as follows:
[0052] 1. Task Analysis: Receive the class-level functional requirements description as input, perform inter-method call dependency analysis, and method and member attribute access dependency analysis;
[0053] 2. Dependency Structure Modeling and Standardized Output: The analysis results are uniformly transformed into a standardized structural data model. For each target class, its internal dependencies are formalized using a graph representation: each method and field is abstracted as a vertex in the graph, and the dependencies between members are represented as directed edges. An adjacency list data structure is used to store the outgoing edges of each vertex, thereby characterizing the intra-class dependencies. The formal definition is as follows: Given a set of task descriptions... Where n represents the number of classes, each Indicates the first A natural language description of each class. The output set of in-class dependencies is defined as follows: Each of them From the set of vertices Sum of edges Composition. The vertex set satisfies, where For inclusion A collection of fields with 1 field. For inclusion A set of methods. At this stage, the code is designed to implement a mapping function from task description to intra-class dependencies, formally expressed as: .
[0054] II. Test-Driven Module: The test-driven module is used to build a test-code collaborative iteration mechanism, simulating the process of "coders and testers collaborating" in real-world development. It improves code reliability through multiple rounds of iterative verification. Specific functions are as follows:
[0055] 1. Test Case Generation: The test agent generates test cases for each method based on class-level task requirements. Each test case includes the method's input data, expected output results, and corresponding test assertions. Five test cases are generated by default for each method, including normal path testing, boundary condition testing, and exception handling testing.
[0056] 2. Initial code generation: The coding agent generates initial class-level code based on the class-level task requirements;
[0057] In this step, guided by the requirements description and intra-class dependencies, the coding module generates complete class implementation code that meets the functional requirements, including the specific implementation logic of each method. Furthermore, for each class, the coding module is required to prioritize generating the method with the fewest dependencies in the dependency relationships. The formal definition is as follows: Given a set of class task descriptions… We define the target code generation set as ,in Indicates the number of classes, each Representing the The code generation implementation for each class. Specifically, For the first The number of methods contained in a class Indicates the first The first in the class The specific implementation code of each method. The code and automated test execution mechanism: The code generated by the coding agent and the test cases generated by the testing agent are executed automatically, and the test results are output. In this step, the coding module performs functional tests on the generated code to verify its correctness. This process simulates the standard unit testing process used by developers in actual software development. For the evaluation of class-level code generation tasks, a class is considered to have passed the test only if all its constituent methods pass their corresponding test cases; otherwise, if any method fails its assigned test case, the class is considered to have failed the test. The formal definition is as follows: ,in Indicates the first The set of test case execution results for all methods in a class, specifically represented by the following formal definition: in It is a binary indicator used to record the first... The first in the class The method of the first The execution results of each test case. For each... Its value selection rules are defined as follows: ,in This indicates that during test execution, the code... In the input The actual output below;
[0058] 3. Dual-Agent Collaborative Iterative Mechanism: Simulating a collaborative model in real software development, the coding agent is responsible for correcting the code, and the testing agent is responsible for correcting the test cases; both parties reflect on and modify based on failure information. For classes that fail tests, the coding and testing modules enter a consensus review process: For all methods within the class that fail test cases, the coding module assumes "the current test cases are correct" during the review phase and focuses on fixing the code to pass the tests (but if the code itself is determined to be error-free, it can remain unchanged); the test design module assumes "the current code is correct" during the review phase and adjusts the expected results of the test cases based on the actual output of the code (but if the test cases themselves are determined to be error-free, it can remain unchanged). The coding module implements a mapping function. Its expression is: Mapping functions implemented in the test design module Its expression is Test quality assessment algorithm: When the test still fails after three rounds of iteration, a test reliability assessment algorithm is introduced. (where n is the iteration number, i = 1, 2, ...; TRI) [0,1]). When TRI 0.8: High reliability test; when TRI 0.2: High-confidence error test; when 0.2 TRI 0.8: Uncertain test result; expert intervention is required.
[0059] 4. Expert review mechanism: When TRI judges the test to be in the middle range or with low confidence, an expert agent intervenes to correct the test logic or assertions, and then the test process is restarted.
[0060] III. Design-Driven Module: The design-driven module is activated when the test-driven mechanism fails to resolve the issue. It performs deep optimization through structured logic modeling to improve the correctness of the code logic. Specific functions are as follows:
[0061] 1. UML Activity Diagram Generation: The logic designer agent generates or modifies method-level UML activity diagrams based on the original task requirements, dependencies, and test failure information. In this step, the design module comprehensively analyzes the task description, dependencies, and error information of each failed method to deduce functional details, identify boundary conditions, handle exceptions, and determine other key implementation elements, ultimately completing the detailed design of the method. It is worth noting that the design module utilizes the sequence, selection, and cycle structures of UML activity diagrams to logically organize the method's implementation flow. This process simulates actual software development practice: for methods with complex implementation logic, developers typically use UML activity diagrams to model their implementation logic, which deepens the understanding of requirements and strengthens guidance for the coding process. The formal definition is as follows: Let... Represents a set of UML activity diagrams, where each Including the Activity graph corresponding to all failed methods in each class. Define mapping functions. This function is specifically designed to complete the UML activity diagram design for methods that fail in the test. Its expression is: Where m represents the total number of failed test cases. Code refactoring based on UML: The coding agent rewrites the method implementation according to the activity diagram, corrects logical flaws, and optimizes the branch structure. Test verification is re-executed. Within a preset number of iterations: if the test passes, proceed to the next method; if it still fails, trigger the expert correction mechanism.
[0062] 2. Expert-Level One-Time Structural Revision: An expert agent performs structural revisions to the UML activity diagrams. In this step, the expert module, driven by a large language model, fine-tunes the UML activity diagrams of methods that failed tests. This process is based on a comprehensive understanding of the class and method task descriptions, combined with error messages generated by test failures. The expert module can flexibly add, modify, or delete control flow elements (including sequence, selection, and loop structures) to optimize the modeling effect of the method implementation logic. The formal definition is as follows: Given a set of UML activity diagrams… The optimized version can be represented as: Once the expert module optimizes the UML activity diagram, it is considered the final confirmed version and will not be modified further. Subsequently, the coding module regenerates the code and executes test cases under the guidance of these UML activity diagrams. If any method in a class still fails the test cases, this stage will terminate, and the last version of the code for that class will be considered the final result.
[0063] like Figure 4 As shown, the dependency analysis phase includes a task description input unit, a dependency analysis unit, and a dependency artifact generation unit. The task description input unit receives task description information for the class-level code to be generated. This task description information includes class structure information and method definition information, providing basic data for subsequent dependency analysis. The dependency analysis unit performs semantic parsing on the task description information. By analyzing the class structure, method signatures, and call relationships between methods, it identifies dependencies between methods and between methods and fields, thereby constructing a dependency model in the program structure. The dependency artifact generation unit generates dependency artifacts based on the dependency analysis results. These dependency artifacts represent the call relationships between methods in a class and the access relationships between methods and attributes, providing dependency constraint information for the subsequent code generation phase to guide the method implementation order and logical structure during the subsequent code generation process.
[0064] like Figure 5 As shown, the test-driven coding phase includes a class code generation unit, a test case generation unit, and a test execution and feedback unit;
[0065] The class code generation unit generates an initial implementation of class code based on dependent artifacts and task description information, including class structure definition, method framework, and logical implementation of methods within the class. The test case generation unit generates corresponding test cases based on class requirements, including method input data, expected output results, and corresponding test assertions. The test execution and feedback unit executes test cases on the generated class code and determines whether each method passes the test based on the execution results. When the test results show that all methods pass, a success result is output. If any method fails the test, the test failure information is fed back to the class code generation unit and the test case generation unit for adjustment and updating of the class code or test cases, and the test is re-executed. If, after reaching a specified threshold, there are still methods that fail the test, an expert review mechanism for test cases is implemented, thus forming a test-driven iterative code generation process.
[0066] like Figure 6 As shown, the UML-driven coding phase includes an activity diagram generation unit, an activity diagram-based code repair unit, and a code generation unit. The activity diagram generation unit generates UML activity diagrams for failed methods based on class requirements descriptions, etc. These activity diagrams describe the control flow, conditional judgments, and loop structures during program execution. The activity diagram-based code repair unit analyzes and repairs the existing code structure based on the generated activity diagrams. By mapping the control flow structure in the activity diagrams to the program code, it supplements or adjusts the logical structure in the code to ensure that the code logic is consistent with the program behavior described in the activity diagrams. The code generation unit generates the final code implementation based on the repaired program structure and outputs the code in a structured manner to form complete class code that meets functional requirements.
[0067] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A dual-drive class code generation method based on multi-agent collaboration, characterized by, include: S1. Obtain the class-level code to generate the task description, perform semantic parsing on the task description, extract the class name, member attributes, member methods and their functional descriptions, and construct the task semantic representation; S2. The coder agent performs dependency analysis and constructs intra-class dependency representations; S3. Test design agent generates test cases for each member method; S4. The coding agent generates initial class-level code based on the intra-class dependency representation and class-level code requirement description. S5. Perform consistency verification tests based on the generated class-level code. When the test results show that the initial class-level code passes the test cases, confirm that the code implementation is correct and mark it as passed. When the test results show that the class-level code fails the test cases, the collaborative iteration process begins. S6. Collaborative Iteration: The coding agent analyzes the causes of the current class-level code based on the test feedback information and decides whether to generate a code correction plan. Based on test feedback, the test agent analyzes the coverage, boundary conditions, and expected output correctness of the current test cases and decides whether to generate a test case correction plan. Based on test feedback, the coding agent analyzes the logical integrity, boundary scenario handling capability, output accuracy, and exception tolerance mechanism of the current class code and decides whether to generate a code correction plan. Based on the code correction scheme output by the coding agent and the test case correction scheme output by the test agent, it is determined whether the two parties have reached an agreement. If the iteration count has not reached the preset upper limit, return to step S5 to re-perform the test consistency verification; if the iteration count has reached the preset upper limit, the code still fails the test case and the coding agent and the test agent still have not reached an agreement, proceed to step S7. S7. Expert agent performs test case correctness verification: The expert agent verifies the correctness of the test cases. If the expert agent determines that the test cases are correct, proceed to step S8. If the expert agent determines that a test case is incorrect, the expert agent will update the test case and verify it against the latest class-level code. If any member methods fail the test verification, proceed to step S8; if all member methods pass the test verification, proceed to step S14 to output the complete class-level code. S8. Generating UML Activity Diagrams for Logical Design Agent: For the methods that failed the test verification in step S7, the logical design agent generates corresponding UML activity diagrams based on their functional descriptions, intra-class dependency representations, and test failure feedback information. S9. Code correction based on UML activity diagram: The coded agent outputs updated member method code based on the UML activity diagram; S10. Test and verify the UML-driven code: Use the test cases in step S7 to test and verify the updated code output in step S9, and obtain the test results. If the test results show that the updated member method code passes the test cases, it is marked as passed, and steps S8 to S10 are repeated for the remaining member methods to be corrected; if the test results show that the updated code fails the test cases, proceed to step S11. S11. The logic design agent iteratively corrects the UML activity diagram based on the test failure information: Based on the test failure feedback information from step S10, the logic design agent performs cause analysis on the UML activity diagram and generates a UML correction plan to obtain an updated UML activity diagram. At the same time, the UML iteration count is incremented by one. When the UML iteration count has not reached the preset upper limit, the process returns to step S9, corrects the code based on the updated UML activity diagram, and tests again. When the UML iteration count reaches the preset upper limit and the corresponding method still fails the test case, the process proceeds to step S12. S12. Expert agent intervenes to verify and correct the UML activity diagram: The expert agent verifies the correctness of the current UML activity diagram and outputs the corrected UML activity diagram based on the test failure feedback information. S13. Code generation and test verification of the expert agent's modified UML activity diagram: The coding agent modifies the member method code according to the UML activity diagram modified by the expert agent in step S12, and uses the corresponding test cases to test and verify the code to obtain the final test results. S14. Output the final class-level code result: Regardless of whether the final test result described in step S13 passes the test cases, the final member method code is integrated with the other member method code in the class to generate the final class-level code result and output it.
2. The dual-drive class-level code generation method based on multi-agent collaboration according to claim 1, characterized in that, The coder agent performs dependency analysis and constructs intra-class dependencies, including: The coder agent analyzes the semantic representation of the task, identifies the calling dependencies between member methods and the access dependencies between member methods and member attributes, and generates a structured intra-class dependency representation; wherein, the intra-class dependency representation is used to characterize the structural associations between class members.
3. The dual-drive class-level code generation method based on multi-agent collaboration according to claim 1, characterized in that, The test design agent generates test cases for each member method, including: The test design agent generates test cases for each member method based on the functional description of the member method. The test cases include input data, expected output results, and corresponding test assertions. The test assertions are used to verify whether the actual output results meet the expected results. The test assertions include, but are not limited to, AssertTrue, AssertFalse, AssertEqual, and AssertNotEqual.
4. The dual-drive class-level code generation method based on multi-agent collaboration according to claim 1, characterized in that, The coding agent generates initial class-level code based on the intra-class dependency representation and class-level code requirement description. Given a set of task descriptions ,in, For the number of target classes, The natural language function description of the i-th class is defined as follows: its in-class dependency set is defined as The dependency unit corresponding to each class , by vertex set With the set of directed edges Composition, edge set Used to characterize the dependencies of each member method in the i-th class on other member methods and member variables within the class; Based on a given set of task descriptions Define the target code generation set as ,in, Representing the The code generation implementation for each class , Indicates the first The first in the class The specific implementation code of each member method Indicates the first The number of member methods in each class, and the mapping function implemented by the encoded agent. The formulaic expression is as follows: .
5. The dual-drive class-level code generation method based on multi-agent collaboration according to claim 1, characterized in that, Perform consistency verification tests based on the generated class-level code, including: The code generated by the coding agent and the test cases generated by the test agent are executed automatically, and the test results are output. For the evaluation of class-level code generation tasks, a class is considered to have passed the test only if all its constituent methods pass their corresponding test cases; otherwise, if any method fails its assigned test case, the class is considered to have failed the test. The formal definition is as follows: ,in, Indicates the first The set of test case execution results for all methods in a class, specifically represented by the following formal definition: in It is a binary indicator used to record the first... The first in the class The method of the first The execution results of each test case; for each Its value selection rules are defined as follows: ,in, This indicates that during test execution, the code... In the input The actual output is as follows. Indicates the number of classes. Representing the The code generation implementation for each class This represents the test function. Indicates the first A collection of test cases for each class. Indicates the first The first in the class The code implementation of each method, Indicates the first The first in the class The input data for the k-th test case of the method. This indicates the expected output.
6. The dual-drive class-level code generation method based on multi-agent collaboration according to claim 1, characterized in that, The logic design of the intelligent agent generates UML activity diagrams, including: make Represents a set of UML activity diagrams, where Including the Activity graphs for all failed test methods in each class; define mapping functions. This function is specifically designed to complete the UML activity diagram design for methods in the test failure class, and its expression is: ,in, This represents the number of classes, where m represents the total number of classes that failed the test. Indicates the first A collection of UML activity diagrams for each class. Indicates the first The first in the class UML activity diagrams for each method, Indicates the first The number of member methods in a class Represents a set of task descriptions. Represents the collection of dependencies within a class. This represents the set of error messages for all classes. Indicates the first A collection of error messages for each class. Indicates the first Natural language description of each class, Indicates the first Intra-class dependencies of each class.
7. A dual-drive class-level code generation system based on multi-agent collaboration, used to implement the method as described in claims 1-6, characterized in that, include: Dependency analysis module, test-driven module, and design-driven module; The dependency analysis module includes a task description input unit, a dependency analysis unit, and a dependency artifact generation unit. The task description input unit is used to receive task description information for generating class-level code; The dependency analysis unit is used to perform semantic parsing on task description information. By analyzing class structure, method signatures and calling relationships between methods, it identifies the dependency relationships between methods and the dependency relationships between methods and fields, thereby constructing a dependency relationship model in the program structure. The dependency artifact generation unit is used to generate dependency artifacts based on the dependency analysis results. The dependency artifacts are used to represent the calling relationship between methods in a class and the access relationship between methods and properties, and provide dependency constraint information for the subsequent code generation stage to guide the method implementation order and logical structure in the subsequent code generation process. The test-driven module includes a class code generation unit, a test case generation unit, and a test execution and feedback unit. The class code generation unit is used to generate an initial implementation of class code based on dependent artifacts and task description information, including class structure definition, method framework, and logical implementation of methods in the class; The test case generation unit is used to generate corresponding test cases based on the class requirement description. The test cases include method input data, expected output results, and corresponding test assertions. The test execution and feedback unit is used to execute test cases on the generated class code and determine whether each method passes the test based on the test execution results; when the test results show that all methods pass the test, a success result is output. When a method fails the test, the test failure information is fed back to the class code generation unit and the test case generation unit. The class code or test cases are adjusted and updated, and the test is re-executed. If a method still fails the test after the specified threshold is reached, an expert review test case mechanism is implemented, thus forming a test-driven iterative code generation process. The design-driven module includes an activity graph generation unit, an activity graph-based code repair unit, and a code generation unit; The activity diagram generation unit is used to generate UML activity diagrams for failed methods based on class requirement descriptions, etc. The activity diagrams are used to describe the logical relationships such as control flow, condition judgment, and loop structure during program execution. The activity diagram-based code repair unit is used to analyze and repair the existing code structure according to the generated activity diagram. By mapping the control flow structure in the activity diagram to the program code, the logical structure in the code is supplemented or adjusted to ensure that the code logic is consistent with the program behavior described by the activity diagram. The code generation unit is used to generate the final code implementation based on the repaired program structure, and to output the code in a structured manner to form complete class code that meets the functional requirements.