A code repair method and computing device
Patent Information
- Application Number
- CN202610769988.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-29
- Publication Date
- 2026-10-09
AI Technical Summary
[0006]本申请实施例提供的代码修复方法中,计算设备可以在目标代码运行故障时,生成描述故障的故障报告。由于故障报告中包括推理日志、内存快照信息和错误堆栈信息,大语言模型在解析故障报告时,根据推理日志可以感知到故障发生时目标代码的业务意图,根据错误堆栈信息可以感知到故障的发生位置,根据内存快照信息可以感知到目标代码运行时传输的变量情况。通过结合错误堆栈信息和内存快照信息,使得大语言模型准确感知到目标代码运行过程中的动态特征,可以准确还原出发生故障的场景,从而保证大语言模型准确地分析出产生故障的原因。再通过结合业务意图,保证大语言模型的修复方向不偏离业务意图,生成可以准确修复故障的修复代码。最后,计算设备将修复代码与目标代码合并,以修复故障,整个过程无需人工协助,实现代码故障的闭环自愈。
Smart Images

Figure CN122884515A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computing device technology, and in particular to a code repair method and computing device. Background Technology
[0002] With the development of large language models, agent-based automation systems are evolving from single-task execution to complex multi-step reasoning and tool invocation. These agents typically rely on the collaboration of external interfaces, databases, and microservices to complete complex cross-domain tasks, thereby achieving task automation and intelligence.
[0003] However, current intelligent agents are developed using predefined tool registration mechanisms, and code specifications are fixed before deployment, making them unable to cope with unexpected failures during actual operation. Furthermore, current artificial intelligence (AI) code assistance tools have limitations, failing to accurately identify the causes of operational failures, and thus unable to accurately repair them. Summary of the Invention
[0004] This application provides a code repair method and computing device, which can accurately repair code faults.
[0005] In a first aspect, embodiments of this application provide a code repair method, comprising: generating a fault report when the target code fails to run; the fault report includes at least: an inference log, memory snapshot information, and error stack information; the inference log is used to indicate the business intent of the target code when the fault occurs; the memory snapshot information includes variable information involved when the target code fails; the error stack information is used to indicate the location where the fault occurs; parsing the fault report and generating repair code based on the parsing result of the fault report; the repair code is used to repair the running fault of the target code; and merging the repair code and the target code to repair the fault.
[0006] In the code repair method provided in this application embodiment, the computing device can generate a fault report describing the fault when the target code fails during runtime. Since the fault report includes inference logs, memory snapshot information, and error stack information, the large language model, when parsing the fault report, can perceive the business intent of the target code at the time of the fault based on the inference logs, the location of the fault based on the error stack information, and the variable transmission status during the target code's runtime based on the memory snapshot information. By combining the error stack information and memory snapshot information, the large language model can accurately perceive the dynamic characteristics during the target code's execution process, accurately reconstruct the fault scenario, and thus ensure that the large language model accurately analyzes the cause of the fault. Furthermore, by combining the business intent, the repair direction of the large language model is ensured to remain within the business intent, generating repair code that accurately repairs the fault. Finally, the computing device merges the repair code with the target code to repair the fault. The entire process requires no manual assistance, achieving a closed-loop self-healing mechanism for code faults.
[0007] In one possible implementation, the fault report also includes: the fault type; parsing the fault report through a large language model to generate repair code, including: querying the knowledge base for example code corresponding to the fault type; the knowledge base stores example code for handling different fault types; inputting the example code, inference log, memory snapshot information, and error stack information into the large language model to generate repair code. It can be understood that the example code can serve as a few-shot example for the large language model, allowing it to generate more accurate repair code based on memory snapshot information and error stack information by referencing previous solutions for the same fault type.
[0008] Another possible implementation method includes inputting error stack information into a classification model to obtain the fault type. Through the above steps, a specific scheme for obtaining the fault type is provided, ensuring the feasibility of this scheme.
[0009] Another possible implementation involves storing the repair code and fault type in a knowledge base. Updating the knowledge base ensures the efficiency and accuracy of subsequent fault repairs for similar issues.
[0010] Another possible implementation includes: generating test cases based on memory snapshot information; merging the fix code and target code, including: testing the fix code with test cases, and merging the fix code and target code after the tests pass. By configuring test cases to reproduce failure scenarios and verify the effectiveness of the fix code, the accuracy of the code fix is further guaranteed.
[0011] In another possible implementation, test cases include input parameters and expected output results. Testing the fix code using test cases includes: deploying and running the fix code in a sandbox environment; inputting the input parameters into the fix code and obtaining the actual output results; if the actual output results match the expected output results, the test on the fix code is deemed passed; otherwise, the test on the fix code is deemed failed. Through the above steps, a specific scheme for testing the fix code is provided, ensuring the feasibility of this scheme.
[0012] In another possible implementation, if the code repair test fails, the method further includes: obtaining fault information of the repair code; the fault information includes: memory snapshot information of the repair code, error stack information of the repair code, and test cases of the repair code; inputting the fault information into a large language model to obtain the adjusted repair code. Through the above steps, the computing device can autonomously perform closed-loop code repair without human intervention, thereby saving operation and maintenance costs.
[0013] Another possible implementation involves merging the repair code and the target code. This includes writing the repair code into the target code's runtime memory space while the target code is running, thus merging the repair code and the target code. This method enables dynamic fault repair without restarting the target code, improving fault repair efficiency.
[0014] In another possible implementation, the method is applied to an agent; the agent is used to implement business functions through target code.
[0015] Secondly, embodiments of this application provide a code repair apparatus, which includes one or more functional modules for implementing the code repair method described in the first aspect above.
[0016] Thirdly, embodiments of this application provide a computing device including a processor and a memory; the processor is coupled to the memory; the memory is used to store computer instructions, which are loaded and executed by the processor to enable the computing device to implement the method described in the first aspect.
[0017] Fourthly, embodiments of this application provide a computer-readable storage medium comprising: computer software instructions; when the computer software instructions are executed in a computing device, they cause the computing device to implement the method described in the first aspect.
[0018] Fifthly, embodiments of this application provide a computer program product that, when run on a computing device, causes the computing device to execute the steps of the method related to the first aspect described above, so as to implement the method of the first aspect described above.
[0019] The beneficial effects of the second to fifth aspects mentioned above can be referred to the corresponding description of the first aspect, and will not be repeated here. Attached Figure Description
[0020] Figure 1 This is a schematic diagram of the composition of a computing device provided in an embodiment of this application; Figure 2 A schematic diagram illustrating the composition of a code repair platform provided in an embodiment of this application; Figure 3 A flowchart illustrating a code repair method provided in an embodiment of this application; Figure 4 A flowchart illustrating another code repair method provided in this application embodiment; Figure 5 A flowchart illustrating yet another code repair method provided in an embodiment of this application; Figure 6 A schematic diagram illustrating the complete workflow of a code repair platform provided in this application embodiment; Figure 7 This is a schematic diagram illustrating the composition of a code repair device provided in an embodiment of this application. Detailed Implementation
[0021] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0022] It should be noted that in the embodiments of this application, the words "exemplarily" or "for example" are used to indicate examples, illustrations, or explanations. Any embodiment or design scheme described as "exemplarily" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of the words "exemplarily" or "for example" is intended to present the relevant concepts in a specific manner.
[0023] To facilitate a clear description of the technical solutions of the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish the same or similar items with essentially the same function and effect. Those skilled in the art can understand that the terms "first" and "second" are not intended to limit the quantity or execution order.
[0024] The following is a brief explanation of the technical terms used in the embodiments of this application: 1. Large Language Model (LLM): A type of artificial intelligence model that uses deep learning algorithms to learn language patterns and structures through training on a large amount of text data, thereby enabling it to understand and generate natural language text.
[0025] 2. Intelligent Agent: This refers to an intelligent system built on a large model that can autonomously understand tasks, plan steps, invoke tools, and complete complex goals. Through interaction with the large model, it combines natural language understanding, reasoning abilities, and external tools to achieve problem-solving capabilities similar to humans.
[0026] 3. Application Programming Interface (API): A predefined "communication rule" between different software systems. It specifies how data is requested, transmitted, and responded to, allowing two independent programs to interact without needing to know each other's internal logic.
[0027] This application provides a code repair method that can accurately repair code faults.
[0028] In some implementations of the code repair method provided in this application, the computing device can generate a fault report describing the fault when the target code fails during execution. Since the fault report includes inference logs, memory snapshot information, and error stack information, the large language model, when parsing the fault report, can perceive the business intent of the target code at the time of the fault based on the inference logs, the location of the fault based on the error stack information, and the variable transmission status during the target code's runtime based on the memory snapshot information. By combining the error stack information and memory snapshot information, the large language model can accurately perceive the dynamic characteristics during the target code's execution, accurately reconstruct the fault scenario, and thus ensure that the large language model accurately analyzes the cause of the fault. Furthermore, by combining the business intent, the repair direction of the large language model is ensured to remain within the business intent, generating repair code that accurately repairs the fault. Finally, the computing device merges the repair code with the target code to repair the fault. The entire process requires no manual assistance, achieving a closed-loop self-healing mechanism for code faults.
[0029] The target code can be the logic code that enables the intelligent agent to implement business functions. The code repair scheme in this application embodiment allows the intelligent agent to possess self-repair and evolution capabilities.
[0030] The embodiments provided in this application will now be described in detail with reference to the accompanying drawings.
[0031] The technical solutions provided in this application can be applied to computing devices. For example, the computing device can be a server or a terminal device. This application does not specifically limit the type of computing device.
[0032] The server can be a single physical or logical server, or it can consist of two or more physical or logical servers that share different responsibilities and work together to achieve the various functions of the server.
[0033] When the computing device is specifically a server, in terms of form, the server can be a blade server, a high-density server, a rack server, or a full-rack server; in terms of function, the server can be a general-purpose server, a graphics processing unit (GPU) server, an AI server, etc.
[0034] When the computing device is specifically a terminal device, the terminal device may include a personal digital assistant (PDA), an ultra-mobile personal computer (UMPC), a tablet computer, a laptop computer, a netbook, a desktop computer, an all-in-one computer, etc.
[0035] The following section uses a computing device as a server as an example to introduce its system architecture. Figure 1 This is a schematic diagram illustrating the composition of a computing device provided in an embodiment of this application. Figure 1 As shown, the computing device may include a processor 101 and a memory 102; the memory 102 stores instructions executable by the processor 101; when the processor 101 is configured to execute instructions, the computing device executes the code repair method in the embodiments of this application.
[0036] In some implementations, a code repair platform (software program) is deployed in the computing device. The computing device executes the code repair method described in this application by running the code repair platform.
[0037] Figure 2 This is a schematic diagram illustrating the composition of a code repair platform provided in an embodiment of this application. Figure 2 As shown, the code repair platform 200 includes a monitoring and failure capture (MFC) module 201, a large model reasoning module 202, a testing module 203, and an adaptive knowledge base 204.
[0038] The monitoring and fault capture module 201 is used to monitor the running status of the target code during runtime. When the target code encounters a running fault, it records inference logs, error stack information, and memory snapshot information to generate a fault report describing the fault.
[0039] Among them, the inference log is used to describe the functional logic of the target code, the memory snapshot information includes variable information involved when the target code fails, and the error stack information is used to indicate the location where the failure occurred.
[0040] The large model inference module 202 is used to receive fault reports and call a large language model for parsing to analyze the cause of the fault in the target code and generate repair code. The repair code is used to fix the runtime faults in the target code. The large model inference module 202 can automatically call the large language model through a meta agent (MA).
[0041] Test module 203, also known as the Security Sandbox and Dynamic Integration (SSDI) module, provides an isolated sandbox environment for deploying the fix code generated by the large model inference module 202 into the sandbox environment for testing. Specifically, it uses test cases to test whether the fix code can output the expected results.
[0042] In addition, the test module 203 is also used to record fault information during testing and feed the fault information back to the large model inference module 202, so that the large model inference module 202 can readjust and repair the code based on the large language model.
[0043] Knowledge base 204, also known as the self-adaptation knowledge repository (SAKR), is used to store repair code. This code serves as a reference for the large model inference module 202 when it faces similar faults in the future, thereby improving the accuracy and efficiency of fault repair.
[0044] It should be noted that the system architecture and application scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of system architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0045] Figure 3 This is a flowchart illustrating a code repair method provided in an embodiment of this application. For example, the code repair method provided in this embodiment can be applied to... Figure 1In the computing device shown, in other words, it can be used... Figure 1 The computing device executes the method; specifically, the method can be executed by the processor of the computing device.
[0046] like Figure 3 As shown, the code repair method provided in this application embodiment may include the following steps: S301. When the target code encounters a runtime failure, generate a failure report.
[0047] The fault report should include at least the following: inference log, memory snapshot information, and error stack information. The inference log indicates the business intent of the target code at the time the fault occurred (e.g., describing what the current code is supposed to do). The memory snapshot information includes variable information involved when the target code failed. The error stack information indicates the location of the fault (e.g., indicating a specific line of code that encountered an error, along with the reason for the error).
[0048] In this embodiment, the computing device monitors the execution status of the target code and records its dynamic runtime context. When the target code malfunctions (an exception occurs), the computing device generates a fault report describing the fault based on the dynamic context at the time of the fault. The computing device can use detection tools to detect the target code and obtain relevant information. This embodiment does not limit the specific detection tools used.
[0049] For example, a computing device can run Figure 2 The MFC module 201 is used to generate fault reports.
[0050] In some implementations, the recorded dynamic context includes memory snapshot information derived from the memory space where the target code runs, recording variable information involved in the runtime of the target code (such as variable field names, variable types, data structures, etc.). When a fault occurs, the computing device freezes the memory snapshot information at the time of the fault and generates a fault report to accurately guide the subsequent repair process of the large language model.
[0051] In some implementations, the recorded dynamic context includes an inference log. This inference log records the target code, or the agent's thought process during runtime—what it needs to do and what results it hopes to achieve. The inference log allows analysis of the business intent expressed in the target code, accurately guiding the subsequent repair process of the large language model.
[0052] In some implementations, the fault report also includes the fault type. The fault type can be obtained from error stack information. The computing device can input the error stack information into a pre-trained classification model to obtain the fault type. The classification model can be a locally deployed lightweight model used to classify error stack information into several common fault types, such as authentication failure (API_AUTH_FAILUR), data structure mismatch (DATA_SCHEMA_MISMATCH), and logical error (LOGIC_ERROR).
[0053] It should be noted that error stack information is not universal and contains a lot of redundant information. This application converts non-standard error stack information into standardized fault types, facilitating subsequent guidance of the large language model's reasoning process and ensuring the accuracy of code repair.
[0054] In some implementations, when the target code involves calling external API interfaces / tools, the computing device can also intercept (e.g., through a configured gateway) network interaction messages and record them in a fault report. These external interaction messages record the input parameters and return results of the target code calling the external API interface / tool. These messages can further assist the large language model in analyzing the location of the fault. For example, if the external API interface fails to return valid data due to network issues, this can also cause the target code to malfunction, even if the target code itself is not problematic and requires no repair.
[0055] S302. Parse the fault report based on the large language model, and generate repair code based on the parsing results of the fault report.
[0056] Among them, the repair code is used to fix runtime faults in the target code.
[0057] In this embodiment of the application, after obtaining a fault report, the computing device can input the target code and the fault report into a large language model, so that the large language model can parse the fault report to accurately identify the cause of the fault and obtain the parsing result, thereby generating accurate repair code.
[0058] Specifically, the computing device can construct prompts based on preset prompt templates. These prompts, along with the fault report, are then input into a large language model. The prompts guide the model to parse the fault report and generate accurate repair code.
[0059] Specifically, the process by which the large language model accurately identifies the cause of a fault and generates repair code is as follows: As mentioned earlier, the fault report includes error stack information, which indicates the line of code in the target code that malfunctioned and the cause of the error (surface-level error cause). Based on the error stack information, the large language model can locate the malfunctioning code block during parsing for analysis. During analysis, the large language model can collaboratively verify the memory snapshot information in the fault report to determine if the code block has runtime faults caused by variable name (field name) mismatches, thus accurately identifying the root cause of the fault. Then, based on the determined root cause, the large language model generates repair code that does not deviate from the business intent guided by the business intent in the fault report.
[0060] For example, memory snapshot information might indicate that an external tool returns a variable named "Name," while the variable in the code block might be written as "name." This inconsistency in variable names could lead to runtime errors. Based solely on the error stack trace, the large language model can only identify errors in lines of code containing "name," but it cannot pinpoint the root cause as "the actual transmitted variable name is 'Name,' while the variable name written in the code is 'name,' resulting in a variable name inconsistency." By combining error stack trace information and memory snapshot information to determine the root cause, the large language model, when generating fix code, will accurately rewrite "name" as "Name," rather than blindly trying other naming conventions.
[0061] In addition, if the business intent is to obtain the user's name, the large language model will ensure that the repair code can achieve the function of obtaining the user's name when generating the repair code, rather than deleting the erroneous lines of code indiscriminately, which would result in a situation where the fault in the target code can be resolved, but the business intent cannot be met.
[0062] For example, the prompt for generating repair code could be: "The attachment is a fault report when the code failed during execution. Analyze the cause of the fault based on the content of the fault report, generate repair code that can resolve the fault, and ensure that the generated repair code does not deviate from the business intent. The fault report includes error stack information indicating the location of the fault, memory snapshot information indicating the variables involved in memory at the time of the fault, and inference logs indicating the business intent." It should be noted that, taking an agent as an example, the agent's target code may involve code that calls external tools and API interfaces. The agent's target code is predefined, and the calling specifications and interface parameter specifications for external tools are defined during the development phase. This design flaw directly reduces the agent's tolerance to minor changes in external tools / API interfaces.
[0063] For example, when changes occur in the external tools / API interfaces that the agent interfaces with, such as adjustments to field names or minor tweaks to parameter order, runtime errors can occur. Current AI-assisted coding tools work by capturing error stack traces and generating static repair code that conforms to logic and coding standards, based on general coding conventions and human feedback. In other words, current AI-assisted coding tools can only fix static code problems (such as logical errors or non-standard coding), but due to a lack of awareness of the dynamic context of runtime code, they cannot accurately identify errors caused by changes in field names in external tools / API interfaces. This prevents the agent from autonomously repairing itself using AI-assisted coding tools, necessitating human intervention. This reduces the stability of autonomous code execution and increases maintenance costs.
[0064] Therefore, this application embodiment obtains memory snapshot information at the time of the fault to generate a fault report. Since the memory snapshot information includes variable information transmitted during the runtime of the target code, the large language model can perceive the dynamic context of the target code runtime when parsing the fault report, so as to accurately identify fault problems caused by changes in fields of variable information returned by the API interface.
[0065] In one possible implementation, the fault report also includes: the fault type; such as... Figure 4 As shown, the above S302 can be specifically implemented as follows: S3021. Based on the fault type, query the sample code corresponding to the fault type from the knowledge base.
[0066] The knowledge base, namely the aforementioned Adaptive Knowledge Base (SAKR), stores sample code (templates) for handling different types of faults. The knowledge base is pre-configured and can be deployed locally on a computing device or in the cloud.
[0067] S3022. Input the example code, inference log, memory snapshot information, and error stack information into the large language model to generate repair code.
[0068] Regarding S3021-S3022 above, the computing device can use the fault type in the fault report as a search condition to query example code for the fault type from the knowledge base. The example code can serve as an example for few-shot learning of the large language model. By referring to previous solutions for the same fault type, the large language model can generate more accurate repair code based on memory snapshot information and error stack information.
[0069] Optionally, the computing device can also query example code corresponding to similar functions from the knowledge base based on the function of the target code. By referring to example code with similar functions, the large language model can generate more accurate repair code.
[0070] In some implementations, the fault report includes information in four dimensions: error stack information, memory snapshot information, inference log, and fault type. The computing device inputs the fault report into a large language model. The large language model can retrieve example code from the knowledge base for reference based on the fault type, ensure that the repair process does not deviate from the main business intent based on the inference log, compensate for the large language model's lack of awareness of the dynamic running information of the target code based on the memory snapshot information, and locate the specific fault location based on the error stack information. By combining the above four dimensions of information, the large language model can accurately generate repair code to solve the fault problem.
[0071] For example, the prompts used to generate repair code can be adjusted to guide the large language model in parsing the fault report and providing sample code. For instance, the prompts for generating repair code could be: "The input is a fault report indicating a code failure and sample code for fixing similar faults. Analyze the cause of the fault based on the fault report, generate repair code that can resolve the fault, ensuring that the generated repair code does not deviate from the business intent and conforms to the specifications of the sample code. The fault report includes error stack information indicating the location of the fault, memory snapshot information indicating the variables involved in memory at the time of the fault, and an inference log indicating the business intent." The embodiments of this application accurately reconstruct the fault scene based on structured runtime fault reports, comprehensively capture multi-dimensional information at the moment of the fault, effectively improve the understanding of the large language model of the cause of the fault and the business implementation intent, and ensure the accuracy of code repair.
[0072] S303. Merge the repair code and the target code to fix the fault.
[0073] After the computing device obtains the repair code, it can merge the repair code and the target code to update and fix the runtime faults in the target code.
[0074] Specifically, S303 can be implemented as follows: when the target code is in the running state, write the repair code into the running memory space of the target code to merge the repair code and the target code.
[0075] In other words, the embodiments of this application can adopt a dynamic integration approach to merge the repair code with the target code without restarting the target code. Specifically, the computing device writes the repair code into the memory space where the target code runs to replace the faulty code, thereby achieving dynamic repair of the target code without restarting it. Furthermore, a new identifier (or version number) is configured for the repair code and stored in the tool registry, facilitating subsequent restarts of the intelligent agent from the faulty location and the retrieval of the latest repair code from the tool registry to ensure correct service implementation.
[0076] In one possible implementation, the computing device also performs functional testing on the repair code. In the code repair method provided in this application embodiment, the computing device further performs the following: generating test cases based on memory snapshot information. The repair code is then tested using the test cases; if the tests pass, the repair code is merged.
[0077] In some implementations, by adjusting the prompts, the large language model can also generate test cases, including input parameters and expected output results, based on the memory snapshot information in the fault report. These test cases aim to reproduce the failure scenario and verify the effectiveness of the fix code. For example, the prompts used to generate test cases could be: "Generate multiple sets of test cases, including input parameters and expected output results, based on the memory snapshot information in the fault report, to facilitate the running tests of the fix code."
[0078] It should be understood that computing devices do not need to re-call external API interfaces to obtain test cases (which may involve the consumption of network resources or the number of calls), but instead generate test cases through the variable information that caused the failure in the memory snapshot information, thereby accurately recreating the failure scene and reducing testing costs.
[0079] Specifically, such as Figure 5 As shown, the above-mentioned code fix using test cases can be implemented as follows: S501. Deploy and run the fix code in a sandbox environment.
[0080] The computing device can be configured with an isolated sandbox environment to deploy the runtime environment for the fix code (such as dependent third-party libraries), ensuring that the fix code can run in the sandbox environment. Specifically, the computing device can use virtual machine technology or container technology to configure the isolated sandbox environment, and this application embodiment does not specifically limit this.
[0081] In addition, by configuring sandbox permissions, computing devices can limit access to critical system resources such as the file system and network resources of the computing device by the fix code, and grant the minimum permissions required to execute test cases, so as to minimize the impact of the testing process on the actual production environment.
[0082] S502. Input the input parameters into the repair code and obtain the actual output result.
[0083] S503. If the actual output results match the expected output results, the code repair test is deemed to have passed; otherwise, the code repair test is deemed to have failed.
[0084] The computing device inputs the input parameters from the test cases into the repair code. If the repair code encounters an error or the actual output does not match the expected output, the repair code test fails. If the actual output matches the expected output, the repair code test passes.
[0085] In some implementations, if the fix code fails the tests, the computing device also performs the following steps: Retrieving fault information from the fix code. This fault information includes: a memory snapshot of the fix code, error stack traces of the fix code, and test cases for the fix code. The fault information is then input into a large language model to obtain the adjusted fix code.
[0086] In other words, if the test fails, the computing device feeds back the fault information to the large language model, guiding the model to adjust the generated repair code to obtain modulated repair code. Then, the computing device can test the adjusted repair code again, repeating the above process until the repair code passes the test.
[0087] In addition, the computing device can be configured with a maximum number of iterations and a time threshold. If the number of repeated adjustments exceeds the maximum number of iterations, or the cumulative time of repeated testing exceeds the time threshold, it indicates that the fault is of a high severity and difficult to resolve through autonomous repair. In this case, the computing device can push the fault to the administrator for manual assistance in repair.
[0088] Optionally, the computing device can also analyze the generated fix code using static code analysis. Static code analysis refers to determining whether the code contains security vulnerabilities, redundant code, performance issues, etc., by analyzing the code's text / syntax structure without running the code, and then fixing them. Specifically, the computing device can use AI-assisted code analysis tools to perform static code analysis on the fix code, and if the analysis passes, the fix code is deemed effective.
[0089] In some implementations, the computing device merges the fix code with the target code after the fix code passes sandbox testing and static code analysis.
[0090] In some implementations, after S303 above, the computing device further performs the following steps: storing the repair code and fault type association in a knowledge base to update the knowledge base and ensure the efficiency and accuracy of fault repair for similar problems in the future.
[0091] Furthermore, if multiple debug versions of the fix exist—for example, for the aforementioned issue, the fix only effectively resolved the problem after going through versions 1, 2, and 3—the computing device can also store the adjustment path information (including different versions of the code from multiple adjustment processes) in a knowledge base. This allows the large language model to infer problem-solving strategies from the adjustment paths, thereby improving the accuracy of the fix code generated by the large language model.
[0092] In some implementations, the code repair method provided in this application is applied to an intelligent agent; the intelligent agent is used to implement business functions through target code. If the target code malfunctions during the operation of the intelligent agent providing external services, the intelligent agent can automatically perform closed-loop repair of the target code using the code repair method of this application. The entire process requires no manual intervention, thus improving the intelligence level of the intelligent agent.
[0093] The following section introduces the large language model involved in this application.
[0094] In some implementations, step S302 involves generating repair code using a large language model. The large language model can be trained as follows: The first step is to collect historical fault reports of fault codes from past maintenance scenarios, along with corresponding configuration fix codes (either manually written in past maintenance scenarios or automatically written and adopted by code fix tools) to form training samples. Each training sample contains a historical fault report of the fault code and its corresponding fix code.
[0095] The second step is to design prompt word templates. These templates guide the model in learning to analyze the causes of failures based on historical failure reports and generate corrective code. For example, a prompt word template might look like this: "Attachment X is a failure report when the code failed. Analyze the cause of the failure based on the report's content, generate corrective code that resolves the failure, and ensure that the generated corrective code does not deviate from the business intent. The failure report includes error stack information indicating the location of the failure, memory snapshot information indicating the variables involved in memory at the time of the failure, and an inference log indicating the business intent." The third step, during training, involves filling the access address of the historical fault report into the input part of the prompt word, and using the repair code corresponding to the historical fault report as the target output. The model updates its weights by comparing the difference between its own output and the target output.
[0096] Specifically, the computing device loads a pre-trained large language model as the base model. For each sample in the training sample set, historical fault reports from the sample are input into the base model, and the model outputs candidate repair codes via autoregression. The computing device updates the model's weights using a backpropagation algorithm by calculating the cross-entropy loss between the candidate repair codes and the pre-configured repair codes. After multiple rounds of iterative training, the model learns the ability to analyze the causes of faults based on fault reports and generate repair codes.
[0097] For example, the base model employs a pre-trained large language model with a multi-layer Transformer decoder structure, with a parameter count ranging from 7 billion to 130 billion. The model undergoes self-supervised pre-training on a massive general-purpose corpus, possessing general language understanding and generation capabilities. After step-by-step training, the final generated large language model, based on the base model, updates all weights through supervised fine-tuning. The model learns to analyze the cause of a fault based on the fault report of a fault code and generates repair code. The structure of the fine-tuned model remains consistent with the base model, both being multi-layer Transformer decoder architectures.
[0098] For example, in the analysis process of a large language model, after the input content (in this embodiment, the fault report of the fault code) is converted into a vector representation through the model embedding layer, the multi-layer decoder calculates the association weight between each semantic unit through a multi-head attention mechanism, focuses on effective information and suppresses redundant content, and combines a feedforward network to complete the feature transformation layer by layer, and finally decodes and generates the corresponding target output result (in this embodiment, the repair code).
[0099] The fourth step, in a real-world application scenario, involves the computing device generating a fault report based on the fault. This fault report is then filled into a preset prompt word template to obtain a complete prompt word. This complete prompt word is input into a fine-tuned large language model, which can then generate repair code to resolve the current fault. The following example illustrates this code repair process.
[0100] Background: Financial data analysis scenario.
[0101] Task: The agent calls an external API to query the latest stock price of Company A and decide whether to buy.
[0102] The target code involved is as follows: def get_stock_decision(ticker): # The agent calls the tool to obtain data response = stock_api_tool.get_quote(ticker) # Fault occurred in the line: directly parse the expected data structure current_price = response['data']['market_quote']['price'] # Judgment condition: If the current stock price is greater than 100, sell; otherwise, buy.
[0103] if current_price>100: return "SELL" return "BUY" The above target code conforms to the syntax specification and previously ran normally. However, it is now throwing a KeyError: 'market_quote', which indicates an error in the market_quote field of the above code.
[0104] I. Fault analysis process of related technologies.
[0105] Related technologies can only analyze static code, such as AI code-aided tools that analyze fault information and make probabilistic guesses. Speculation 1: Perhaps the key name is misspelled? Should it be changed to market_data? Speculation 2: Perhaps response['data'] is empty? I suggest adding an if statement to check this.
[0106] Due to the limitations of AI code assistance tools, they do not know the current structure of the parameters returned by external API interfaces. Therefore, the above guesses are all untargeted guesses and cannot effectively solve the problem.
[0107] II. Fault analysis process of the embodiments of this application.
[0108] The computing device acquires the following four-dimensional data: 1. Memory snapshot information.
[0109] Get the input parameter: "Company A".
[0110] Data structure for retrieving the variable response: { "status": "success", "data": { "quote_v2": { / / <API provider changed the field name from 'market_quote' to 'quote_v2' "ticker": "NVDA", "price": 890.50 } } 2. Error stack information.
[0111] Faulty code line: current_price = response['data']['market_quote']['price'] 3. Inference log.
[0112] The computing device reads the inference log and determines that the business intent of the current code is: query stock price (price).
[0113] 4. Fault type.
[0114] Data structure mismatch.
[0115] The computing device can generate a fault report in JSON format to facilitate subsequent parsing by a large language model. As an example, the structure of the fault report is as follows: { Metadata: xxx (indicates metadata such as the name of the program where the fault occurred, the time of occurrence, etc.) Inference log: xxx Memory snapshot information: xxx Error stack information: xxx Fault type: xxx } The computing device inputs the fault report into the large language model, and the large language model finds through the error stack information that the code identifies ['market_quote']['price'], while only ['quote_v2']['price'] exists in the memory snapshot information. This indicates that the API provider has modified the data structure of the response variable to change the field name from 'market_quote' to 'quote_v2'.
[0116] Furthermore, the large language model determines the business intent as querying stock prices based on the inference logs in the fault report. Therefore, when generating repair code, the large language model does not simply ignore the error but adjusts the code content to adapt to the modified data structure. Moreover, when generating repair code, the large language model can also query similar code from the knowledge base for reference based on the fault type, ultimately generating repair code as follows: def get_stock_decision(ticker): # The agent calls the tool to obtain data response = stock_api_tool.get_quote(ticker) # Code fixes for the fault: Prioritize trying new data structures while maintaining compatibility with old data structures. data_block = response.get('data', {}) if 'quote_v2' in data_block: current_price = data_block['quote_v2']['price'] else: current_price = data_block.get('market_quote', {}).get('price') # Check if the extracted price is empty to ensure fault tolerance. if current_price is None: raise ValueError("Price not found in API response") if current_price>100: return "SELL" return "BUY" After obtaining the fix code, the computing device can generate test cases based on the memory snapshot information and use the test cases to test the fix code in a sandbox environment.
[0117] For example, given the specific data input to the response variable by the computing device, determine whether the following part of the code outputs the expected result: data_block = response.get('data', {}) if 'quote_v2' in data_block: current_price = data_block['quote_v2']['price'] else: current_price = data_block.get('market_quote', {}).get('price') If this part of the code can output the stock price correctly (e.g., 300), the test passes. If this part of the code malfunctions or outputs unexpected data (e.g., querying for a price but outputting the time), the test fails. The computing device then calls the large language model to readjust the repair code.
[0118] Figure 6 This is a schematic diagram illustrating the complete workflow of a code repair platform provided in an embodiment of this application. Figure 6 As shown, firstly, the monitoring and fault capture module can monitor the execution process of the target code and generate a fault report when the target code encounters a fault. Secondly, the large model inference module calls a large language model, parses the fault report based on the language model, and generates repair code based on the parsing results. The testing module can test the repair code; if the test passes, the repair code and the target code are merged; if the test fails, feedback is sent to the large model inference module to regenerate the repair code. The knowledge base is used to store the generated repair code as reference knowledge in subsequent code repair processes.
[0119] In the code repair method provided in this application embodiment, the computing device can generate a fault report describing the fault when the target code fails during runtime. Since the fault report includes inference logs, memory snapshot information, and error stack information, the large language model, when parsing the fault report, can perceive the business intent of the target code at the time of the fault based on the inference logs, the location of the fault based on the error stack information, and the variable transmission status during the target code's runtime based on the memory snapshot information. By combining the error stack information and memory snapshot information, the large language model can accurately perceive the dynamic characteristics during the target code's execution process, accurately reconstruct the fault scenario, and thus ensure that the large language model accurately analyzes the cause of the fault. Furthermore, by combining the business intent, the repair direction of the large language model is ensured to remain within the business intent, generating repair code that accurately repairs the fault. Finally, the computing device merges the repair code with the target code to repair the fault. The entire process requires no manual assistance, achieving a closed-loop self-healing mechanism for code faults.
[0120] In summary, the beneficial effects of the embodiments of this application are mainly reflected in the following aspects: Enhanced reliability: Improved autonomous adaptation rate, reduced system downtime, and enhanced robustness of intelligent agents in dynamic environments, making them adaptable to core business scenarios.
[0121] Reduced operation and maintenance costs: Reduced reliance on human developers and maintenance needs, saving the steps of manual intervention in analysis, rewriting and deployment of code, and significantly reducing long-term system operation and maintenance costs and response time.
[0122] Efficiency improvement: System repair time is shortened, task execution can be quickly restored, and business continuity is effectively guaranteed.
[0123] Capability Iteration and Upgrade: Structure successful or failed experiences into knowledge in a knowledge base to continuously improve the efficiency and accuracy of generating fix code.
[0124] As can be seen, the above mainly describes the solutions provided by the embodiments of this application from a methodological perspective. To achieve the above functions, the embodiments of this application provide corresponding hardware structures and / or software modules for executing each function. Those skilled in the art should readily recognize that, in conjunction with the modules and algorithm steps of the various examples described in the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0125] In an exemplary embodiment, this application also provides a code repair apparatus. This code repair apparatus may be the aforementioned computing device or a processor within the computing device. The system function comparison apparatus may include one or more functional modules for implementing the code repair method of the above method embodiments.
[0126] For example, Figure 7 This is a schematic diagram illustrating the composition of a code repair device provided in an embodiment of this application. Figure 7 As shown, the code repair device 700 includes a generation module 701 and a merging module 702. The generation module 701 and the merging module 702 are interconnected.
[0127] The generation module 701 is used to generate a fault report describing the fault when the target code fails during execution; the fault report includes at least: memory snapshot information and error stack information; the memory snapshot information includes variable information involved when the target code fails; the error stack information is used to indicate the location where the fault occurs; The generation module 701 is further configured to parse the fault report using a large language model and generate repair code; the repair code is used to repair the runtime fault of the target code; The merging module 702 is used to merge the repair code and the target code to repair the fault.
[0128] Other optional implementation methods are described in the foregoing method embodiments.
[0129] This application also provides a computer-readable storage medium. All or part of the processes in the above method embodiments can be executed by computer instructions instructing related hardware; for example, the related hardware can be a processor of a computing device. The program instructions can be stored in the above-described computer-readable storage medium, and when executed, they can implement the processes of the above method embodiments. The computer-readable storage medium can be memory. The above-described computer-readable storage medium can also be an external storage device, such as a hard disk, smart media card (SMC), secure digital (SD) card, flash card, etc. Further, the above-described computer-readable storage medium can include both memory and external storage devices. The above-described computer-readable storage medium is used to store the above-described computer program instructions and other programs and data required for the above-described software package translation.
[0130] This application also provides a computer program product comprising a computer program that, when run on a computing device, causes the computing device to execute any of the code repair methods provided in the above embodiments.
[0131] The code repair method of this application embodiment can also be applied to the following extended scenarios.
[0132] First, the horizontal expansion of technology application scenarios.
[0133] The core mechanism of this application's embodiments is not only applicable to backend service-oriented intelligent agents, but can be further extended to the fields of automated process robots, big data processing, and edge computing. In automated process robot scenarios, traditional page control lookup based on static rules often fails due to interface redesigns. This solution can dynamically repair control positioning logic by capturing the runtime DOM tree structure or screen visual snapshots and combining them with a large visual model. In big data scenarios, to address the "Schema drift" problem caused by changes in upstream data sources, this system can capture real-time data samples and automatically adjust the Structured Query Language (SQL) scripts or conversion rules to ensure the continuity of the data link. Furthermore, for IoT edge devices with limited network access or difficult on-site maintenance, this application's embodiments can collect fault snapshots using lightweight probes, upload them to the cloud, generate patches, and then perform hot-swap updates, effectively solving the problem of remote debugging.
[0134] Second, vertical deepening of functional dimensions.
[0135] This application's embodiments can be expanded from a single "fault repair" function to include "performance optimization" and "proactive security defense." Regarding performance optimization, the system can monitor abnormal runtime states with high time consumption or high token consumption. By analyzing the runtime stack, it can identify redundant calls or infinite loops and automatically refactor the code to improve execution efficiency and reduce costs. In terms of security defense, when the system detects risks of SQL injection, prompt injection, or sensitive data leakage, it can utilize the runtime context to generate and dynamically inject security verification logic or filtering rules in real time. This enables millisecond-level automatic defense and hot-fixing against "zero-day vulnerabilities," thereby constructing an intelligent agent system with self-immunity capabilities.
[0136] Third, the evolution of system architecture and interaction patterns.
[0137] In terms of delivery form, the embodiments of this application can move from production environment operation and maintenance systems to auxiliary tools in the development phase. For example, it can be integrated into CI / CD pipelines or development tool plugins to capture a snapshot of the situation and generate a remediation proposal the instant automated test fails, thereby greatly shortening the development iteration cycle. In terms of architectural evolution, the embodiments of this application can be developed into a "multi-agent collaborative diagnosis and treatment" model, decoupling functions such as global monitoring, deep diagnosis, and sandbox verification into different dedicated intelligent agents. Through the collaboration and game between multiple intelligent agents, the diagnostic accuracy and remediation security under ultra-complex system failures can be further improved.
[0138] Although this application has been described herein in conjunction with various embodiments, those skilled in the art, by reviewing the accompanying drawings, disclosure, and appended claims, will understand and implement other variations of the disclosed embodiments in carrying out the claimed application. In the claims, the word "comprising" does not exclude other components or steps, and "a" or "an" does not exclude multiple instances. A single processor or other unit can implement several functions listed in the claims. While different dependent claims may recite certain measures, this does not mean that these measures cannot be combined to produce good results.
[0139] Although this application has been described in conjunction with specific features and embodiments, it is obvious that various modifications and combinations can be made thereto without departing from the spirit and scope of this application. Accordingly, this specification and drawings are merely exemplary illustrations of this application as defined by the appended claims, and are considered to cover any and all modifications, variations, combinations, or equivalents within the scope of this application. Clearly, those skilled in the art can make various alterations and modifications to this application without departing from the spirit and scope of this application. Thus, if such modifications and modifications of this application fall within the scope of the claims of this application and their equivalents, this application is also intended to include such modifications and modifications.
[0140] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A code repair method, characterized in that, The method includes: When the target code encounters a runtime failure, a failure report is generated. The failure report includes at least: an inference log, memory snapshot information, and error stack information. The inference log indicates the business intent of the target code at the time the failure occurred. The memory snapshot information includes variable information involved when the target code encountered a failure. The error stack information indicates the location where the failure occurred. The fault report is parsed, and repair code is generated based on the parsing results; the repair code is used to fix the runtime faults of the target code. The repair code and the target code are combined to fix the fault.
2. The method according to claim 1, characterized in that, The fault report also includes: the fault type of the fault; The process of parsing the fault report using a large language model to generate repair code includes: Based on the fault type, query the knowledge base for the corresponding example code; the knowledge base stores example code for dealing with different fault types; The example code, the inference log, the memory snapshot information, and the error stack information are input into the large language model to generate the repair code.
3. The method according to claim 2, characterized in that, The method further includes: The error stack information is input into the classification model to obtain the fault type.
4. The method according to claim 2 or 3, characterized in that, The method further includes: The repair code and the fault type are associated and stored in the knowledge base.
5. The method according to any one of claims 1-4, characterized in that, The method further includes: Based on the memory snapshot information, test cases are generated; The process of merging the repair code and the target code includes: The repair code is tested using the test cases described above. After the test passes, the repair code and the target code are merged.
6. The method according to claim 5, characterized in that, The test cases include input parameters and expected output results; testing the fix code using the test cases includes: Deploy and run the fix in a sandbox environment; Input the input parameters into the repair code to obtain the actual output result; If the actual output matches the expected output, the test on the fix code is deemed to have passed. Otherwise, the test for the fix code is deemed to have failed.
7. The method according to claim 6, characterized in that, If the fix code fails the test, the method further includes: Obtain the fault information of the repair code; the fault information includes: memory snapshot information of the repair code, error stack information of the repair code, and test cases of the repair code; The fault information is input into the large language model to obtain the adjusted repair code.
8. The method according to any one of claims 1-7, characterized in that, The merging of the repair code and the target code includes: When the target code is in a running state, the repair code is written into the memory space where the target code is running, so as to merge the repair code and the target code.
9. The method according to any one of claims 1-8, characterized in that, The method is applied to an intelligent agent; the intelligent agent is used to implement business functions through the target code.
10. A computing device, characterized in that, The computing device includes a processor and a memory; the processor is coupled to the memory. The memory is used to store computer instructions; The computer instructions are loaded and executed by the processor to enable the computing device to implement the code repair method as described in any one of claims 1-9.