Code automatic repair and hot update method and system
By combining global error listeners, Source Map and AST analysis with the OpenAI model to generate repair code blocks, we achieved automated code repair and hot updates for front-end applications, solving the time-consuming issue of code repair in production environments and improving system stability and user experience.
Patent Information
- Application Number
- CN202510893255.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-30
- Publication Date
- 2025-09-30
AI Technical Summary
Existing technologies cannot achieve automatic code repair and hot updates in production environments in front-end applications, resulting in time-consuming error repair and high maintenance costs, which affects user experience.
Runtime error information is captured through the global error listener, and the error source code location is reversed by combining the Source Map. AST backtracking analysis is used to generate detailed error nodes and context information. The OpenAI model is called to generate repair code blocks, and the repair patch is pushed to the client through WebSocket for hot update.
It achieves full process automation from error capture to repair, significantly improves code maintenance efficiency and accuracy, reduces labor costs, ensures application stability and user experience continuity, and is particularly suitable for application scenarios with high availability and real-time response.
Smart Images

Figure CN120723285A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of information security and network authentication, and in particular to a method and system for automatic code repair and hot update. Background Art
[0002] With the advancement of front-end technologies, front-end frameworks such as React have become widely used in web application development. Modern front-end applications are typically deployed to production environments after undergoing optimization processes such as bundling, compression, and code splitting. While these processes improve application performance, they also make it difficult to quickly locate and fix errors in online environments. Developers often need to manually analyze error stack traces, locate source code locations, fix code, and rebuild and deploy. This process is time-consuming and impacts the user experience.
[0003] Current hot-replacement technologies, such as Webpack's Hot Module Replacement (HMR), Vite's fast hot reload, and React's Fast Refresh, primarily focus on optimizing development environment efficiency. These technologies significantly improve the developer debugging experience by enabling real-time module replacement during development. However, these technologies lack the ability to dynamically fix errors in production environments. Once front-end errors occur in production, the system often fails to automatically detect and apply fixes, relying instead on manual intervention by developers, increasing maintenance costs and response times.
[0004] Therefore, automatic repair and hot update of running code have become urgent issues to be solved.
[0005] Patent CN117555720B discloses a method for code repair, comprising: obtaining m error stacks, wherein the m error stacks include a first error stack, where m is a positive integer; the m error stacks refer to stacks generated when an error event occurs during the running of a first program, wherein the m error stacks respectively include an error type of the error event, an error description of the error event, and unresolved error location information, wherein the unresolved error location information is used to indicate the code location where the error event occurs in the program file of the unresolved first program; obtaining the number of types corresponding to the multiple error types corresponding to the m error stacks; obtaining type weights corresponding to the multiple error types, wherein the type weights are used to indicate the degree of association between the error type and the stop running event of the first program; performing weighted summation of the number of types corresponding to the multiple error types based on the type weights corresponding to the multiple error types, and obtaining a program crash score as a trend analysis result, wherein the trend analysis result is used to indicate the possibility that the error events corresponding to the m error stacks cause the first program to stop running; and when the trend analysis result meets the program repair condition, performing a program crash score analysis on the m error stacks. The unresolved error location information corresponding to each error stack is parsed to obtain error location information, which includes a code file path and an exception code location. The code file path refers to the file path of the first source code where the error event occurs, and the exception code location refers to the location of the code fragment where the error event occurs in the first source code. The error type, error description, code file path, and exception code location of the error event are used as exception information. The exception information corresponding to the first error stack is traced to the source code, and the first source code corresponding to the first error stack is determined in the code file corresponding to the first program. The repair prompt text is determined based on the exception information and the first source code corresponding to the first error stack. The repair prompt text is used to guide the code repair model to repair the first source code. The code repair model is a pre-trained model that includes code repair functions. The repair prompt text is semantically analyzed by the code repair model, and the first source code corresponding to the first error stack is repaired based on the semantic analysis results to obtain a second source code. The code file corresponding to the first program is updated based on the second source code. This method only involves error stack analysis and code repair, and cannot achieve real-time code updates during operation, which limits the repair accuracy and applicability of complex codes.
[0006] Based on this, the present application provides a method and system for automatic code repair and hot update to improve the existing technology. Summary of the Invention
[0007] The purpose of this application is to provide a method and system for automatic code repair and hot update, which can rebuild and repair the code in the container running on the server, realize seamless hot update, effectively enhance the stability and maintainability of the system, and significantly reduce the time and labor cost of repairing online errors.
[0008] The purpose of this application is achieved by the following technical solutions:
[0009] In a first aspect, the present application provides a method for automatic code repair and hot update, which repairs code vulnerabilities of a target application and pushes the repair patch to a client. The method includes:
[0010] In the target application, use the global error listener to capture runtime error information;
[0011] Use Source Map to reverse parse error information to locate the source code of the error;
[0012] Performing AST backtracking analysis based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information;
[0013] Based on the error message and AST analysis results, call the OpenAI model to obtain the repair code block;
[0014] Generate a repair patch based on the repair code block and push it to the client.
[0015] The beneficial effects of this technical solution are as follows: the method captures runtime errors of the target application through a global error listener, combines Source Map reverse parsing to accurately locate the source code of the error, and uses AST backtracking analysis to generate detailed error nodes and context information. It then calls the OpenAI model to generate a repair code block and pushes the repair patch to the client for hot update. This method automates the entire process from error capture to repair, significantly improving the efficiency and accuracy of code maintenance. The hot update mechanism ensures that the repair patch can be quickly deployed without interrupting the operation of the application, ensuring the continuity and smoothness of the user experience. This method is particularly suitable for application scenarios that require high availability and real-time response, such as online service platforms or dynamic Web applications. It can effectively enhance the stability and maintainability of the system while reducing labor costs during maintenance.
[0016] In some optional implementations, capturing runtime error information using a global error listener in the target application includes:
[0017] Use the window.onerror function to capture JavaScript runtime errors;
[0018] Use the unhandledrejection event to capture Promise exceptions;
[0019] Use React boundary components to catch React rendering errors;
[0020] JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into the error message and uploaded to the server.
[0021] The beneficial effects of this technical solution are: the method captures JavaScript runtime errors through the window.onerror function, Promise exceptions through the unhandledrejection event, and React rendering errors through the React boundary component, integrating various error messages and uploading them to the server. This multi-dimensional error capture mechanism can comprehensively collect runtime exceptions, ensure the integrity and diversity of error data, and provide a rich data foundation for subsequent error location and repair. This method can effectively cover various error scenarios that may occur in complex applications, support comprehensive monitoring of front-end applications, significantly improve the reliability of error handling and the stability of system operation, and is particularly suitable for large-scale, modular front-end development projects.
[0022] In some optional implementations, the AST backtracking analysis is performed based on the source code location to obtain an AST analysis result, where the AST analysis result includes an AST error node and context information, including:
[0023] Generate a first abstract syntax tree based on the error source code location and locate the AST error node;
[0024] Perform context dependency analysis on AST error nodes to obtain context information.
[0025] The beneficial effects of this technical solution are: generating a first abstract syntax tree based on the location of the error source code, locating the AST error node, and obtaining detailed context information through context dependency analysis. This method provides high-precision error location capabilities through in-depth analysis of the code structure, laying a solid foundation for subsequent intelligent repair. AST backtracking analysis can deeply understand the code logic and dependencies, ensuring the comprehensiveness and accuracy of the error analysis results. This method is particularly suitable for handling errors in complex code bases, and can support accurate analysis of dynamically generated code or modular code, thereby improving the success rate of code repair and the overall robustness of the system.
[0026] In some optional implementations, calling the OpenAI model to obtain a repair code block based on the error information and AST analysis results includes:
[0027] S1: Based on the error message and AST analysis results, construct AI prompt words and input them into the OpenAI model;
[0028] S2: Use the OpenAI model to obtain repair code blocks;
[0029] S3: Use the code verifier to verify the correctness of the repaired code block;
[0030] S3a: If the repair code block is correct, do nothing;
[0031] S3b: If the repair code block is incorrect, optimize the AI prompt word and execute S1.
[0032] The beneficial effect of this technical solution is that the method constructs AI prompt words by combining error information and AST analysis results, calls the OpenAI model to generate repair code blocks, and uses the code verifier to verify the correctness of the repair code. If the code is incorrect, it is regenerated by optimizing the prompt words until it meets the requirements. This method uses artificial intelligence technology to automate code repair, significantly reducing the workload of developers for manual repairs, while ensuring the high quality and reliability of the repair code through verification and iteration mechanisms. This intelligent repair method can quickly adapt to a variety of error scenarios, providing an efficient solution for the maintenance of complex applications, and is particularly suitable for fast-iteration development environments.
[0033] In some optional implementations, generating a repair patch based on the repair code block and pushing the patch to the client includes:
[0034] Modify the erroneous source code based on the repair code block and generate a second abstract syntax tree;
[0035] Performing comparative analysis on the first abstract syntax tree and the second abstract syntax tree to generate a repair patch;
[0036] Push the fix patch to the client.
[0037] The beneficial effect of this technical solution is that the method modifies the erroneous source code based on the repair code block, generates a second abstract syntax tree, and generates a repair patch by comparing the first and second abstract syntax trees, which is then pushed to the client. This method ensures the accuracy and minimizes modifications of the patch through AST comparison, effectively avoiding unnecessary code changes and improving the reliability and stability of the repair process. The process of automatically generating patches simplifies the development process, shortens the time from error discovery to repair deployment, and ensures that the repair patch can be seamlessly integrated into the target application, significantly improving the efficiency of code maintenance and being suitable for real-time application scenarios with dynamic updates.
[0038] In some optional implementations, pushing the repair patch to the client includes:
[0039] Use WebSocket to push the fix to the client;
[0040] Utilize Service Worker cache fix patch;
[0041] Deploy a monitoring module in the Service Worker to automatically download fixes and update them in the target application.
[0042] The beneficial effects of this technical solution are as follows: it utilizes WebSocket technology to push repair patches to the client in real time, combines it with Service Worker for patch caching, and deploys a monitoring module to automatically download and update patches. This push mechanism enables rapid patch distribution, significantly improves update efficiency, and reduces the impact of network latency on application operation. Service Worker's caching function ensures stable patch storage on the client, while the automatic update mechanism further reduces maintenance costs and ensures the continuous operation and stability of the application. This method is particularly suitable for distributed systems or highly concurrent applications, effectively supporting real-time updates and high system availability.
[0043] In a second aspect, the present application provides a device for automatic code repair and hot update, the device comprising a processor configured to implement the following steps:
[0044] The monitoring module is used to capture runtime error information using the global error listener in the target application;
[0045] The positioning module is used to reversely parse error information using Source Map to locate the source code of the error;
[0046] An analysis module is used to perform AST backtracing analysis based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information;
[0047] The repair module is used to call the OpenAI model to obtain the repair code block based on the error information and AST analysis results;
[0048] The update module is used to generate a repair patch based on the repair code block and push it to the client.
[0049] In some optional implementations, the processor is configured to capture runtime error information using a global error listener in the target application in the following manner:
[0050] Use the window.onerror function to capture JavaScript runtime errors;
[0051] Use the unhandledrejection event to capture Promise exceptions;
[0052] Use React boundary components to catch React rendering errors;
[0053] JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into the error message and uploaded to the server.
[0054] In some optional embodiments, the processor is configured to perform AST backtracking analysis based on the source code location in the following manner to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information:
[0055] Generate a first abstract syntax tree based on the error source code location and locate the AST error node;
[0056] Perform context dependency analysis on AST error nodes to obtain context information.
[0057] In some optional embodiments, the processor is configured to call the OpenAI model to obtain a repair code block based on the error information and the AST analysis result in the following manner:
[0058] S1: Based on the error message and AST analysis results, construct AI prompt words and input them into the OpenAI model;
[0059] S2: Use the OpenAI model to obtain repair code blocks;
[0060] S3: Use the code verifier to verify the correctness of the repaired code block;
[0061] S3a: If the repair code block is correct, do nothing;
[0062] S3b: If the repair code block is incorrect, optimize the AI prompt word and execute S1.
[0063] In some optional implementations, the processor is configured to generate a repair patch based on the repair code block and push the patch to the client in the following manner:
[0064] Modify the erroneous source code based on the repair code block and generate a second abstract syntax tree;
[0065] Performing comparative analysis on the first abstract syntax tree and the second abstract syntax tree to generate a repair patch;
[0066] Push the fix patch to the client.
[0067] In some optional implementations, the processor is configured to push the repair patch to the client in the following manner, including:
[0068] Use WebSocket to push the fix to the client;
[0069] Utilize Service Worker cache fix patch;
[0070] Deploy a monitoring module in the Service Worker to automatically download fixes and update them in the target application.
[0071] In a third aspect, the present application provides an electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any of the above methods when executing the computer program.
[0072] In a fourth aspect, the present application provides a code automatic repair and hot update system, the system comprising:
[0073] The above electronic equipment.
[0074] In a fifth aspect, the present application provides a chip, wherein the chip stores a computer program, and when the computer program is executed by a processor, the steps of any of the above methods are implemented. BRIEF DESCRIPTION OF THE DRAWINGS
[0075] The present application is further described below with reference to the accompanying drawings and implementation methods.
[0076] Figure 1 A schematic diagram of a process for automatic code repair and hot update provided in an embodiment of the present application is shown.
[0077] Figure 2 A flow chart of a method for obtaining a repair code block provided in an embodiment of the present application is shown.
[0078] Figure 3 A structural block diagram of a code automatic repair and hot update device provided in an embodiment of the present application is shown.
[0079] Figure 4 A structural framework diagram of an electronic device provided in an embodiment of the present application is shown.
[0080] Figure 5 A schematic diagram of the structure of a code automatic repair and hot update system provided in an embodiment of the present application is shown.
[0081] Figure 6 A schematic diagram of the structure of a program product provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0082] Below, the embodiments of the present application are further described in conjunction with the accompanying drawings and specific implementation methods. It should be noted that, under the premise of no conflict, the various embodiments or technical features described below can be arbitrarily combined to form a new implementation method.
[0083] In the embodiments of the present application, "at least one" refers to one or more, and "more" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can represent: a, b, c, a and b, a and c, b and c, a and b and c, where a, b and c can be single or multiple. It is worth noting that "at least one" can also be interpreted as "one or more items".
[0084] It should also be noted that in the embodiments of this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations, or descriptions. Any implementation or design scheme described as "exemplary" or "for example" in the embodiments of this application should not be interpreted as being preferred or advantageous over other implementations or designs. Rather, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner.
[0085] Method Example
[0086] See also Figure 1 , Figure 1 A schematic diagram of a process for automatic code repair and hot update provided in an embodiment of the present application is shown.
[0087] The embodiment of the present application provides a method for automatically repairing and hot-updating code, repairing code vulnerabilities of a target application and pushing the repair patch to a client. The method includes:
[0088] Step S101: In the target application, a global error listener is used to capture runtime error information;
[0089] Step S102: using Source Map to reversely parse the error information to locate the error source code location;
[0090] Step S103: performing AST backtracking analysis based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information;
[0091] Step S104: Based on the error information and AST analysis results, call the OpenAI model to obtain the repair code block;
[0092] Step S105: Generate a repair patch based on the repair code block and push it to the client.
[0093] Therefore, this method captures runtime errors of the target application through a global error listener, combines Source Map reverse parsing to accurately locate the erroneous source code location, and uses AST backtracking analysis to generate detailed error nodes and context information. It then calls the OpenAI model to generate repair code blocks and pushes the repair patch to the client for hot update.
[0094] This approach automates the entire process, from error detection to repair, significantly improving the efficiency and accuracy of code maintenance. A hot update mechanism ensures that repair patches can be rapidly deployed without interrupting application operations, ensuring a continuous and smooth user experience. This approach is particularly suitable for applications requiring high availability and real-time responsiveness, such as online service platforms or dynamic web applications. It effectively enhances system stability and maintainability while reducing maintenance labor costs.
[0095] In one specific embodiment, a global error listener is first deployed within the target application's runtime environment to capture runtime error information. For example, in a JavaScript-based web application, a monitoring mechanism is set up to monitor various exceptions that occur during application execution, such as syntax errors, runtime exceptions, or logic errors. Once these error messages are captured, the system proceeds to the next step, utilizing Source Map technology to perform reverse parsing of the error information. Source Map maps the relationship between compressed code and the original source code to precisely locate the specific source code location where the error occurred, such as the location of a function or line of code. Based on the located source code location, the system further performs AST (Abstract Syntax Tree) backtracking analysis to generate an abstract syntax tree (AST) for the target code segment. By analyzing the error node and its context, the system obtains detailed AST analysis results, including the error's grammatical structure and related dependency information. The system then combines the captured error information with the AST analysis results to construct an appropriate input prompt and invokes the OpenAI model to generate a repair code block. Based on the input error description and context, the OpenAI model generates a code snippet that addresses the specific error, such as code that fixes an undefined variable or a logic error. After generating the repair code block, the system modifies the original source code based on this code and generates a repair patch. The fix patch encapsulates the fix with minimal modification, ensuring compatibility with existing code. Finally, the fix is pushed to the client via real-time communication mechanisms such as WebSocket. After receiving the patch, the client implements a hot update using technologies such as Service Worker, completing the fix without interrupting the application. This approach significantly improves code maintenance efficiency by automating the process from error detection to fix deployment. It is particularly suitable for dynamic web applications or high-concurrency scenarios, ensuring system stability and a continuous user experience.
[0096] In the embodiments of this application, a Source Map is a mapping file used to establish a correspondence between compressed or converted code (such as a JavaScript file processed by a packaging tool) and the original source code. It is stored in JSON format and records the mapping information between the line and column numbers in the compressed code and the specific locations in the original source code.
[0097] The process of using Source Map to reverse parse error information to locate the source code of the error is as follows: First, capture the error information generated when the target application is running, such as the stack trace data obtained through the global error listener. Then, the system loads the Source Map file corresponding to the target application, which records the mapping relationship between the compressed or converted code and the original source code. By parsing the line number and column number in the error information, combined with the mapping data of Source Map, the system can accurately trace back to the specific location in the original source code, such as a function or line of code. This method achieves fast and accurate error location by restoring the correspondence between the compressed code and the source code, providing a reliable foundation for subsequent code analysis and repair, and is particularly suitable for error troubleshooting of complex front-end applications.
[0098] In some optional implementations, capturing runtime error information using a global error listener in the target application includes:
[0099] Use the window.onerror function to capture JavaScript runtime errors;
[0100] Use the unhandledrejection event to capture Promise exceptions;
[0101] Use React boundary components to catch React rendering errors;
[0102] JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into the error message and uploaded to the server.
[0103] Therefore, this method uses the window.onerror function to capture JavaScript runtime errors, the unhandledrejection event to capture Promise exceptions, and the React boundary component to capture React rendering errors, consolidating various error messages and uploading them to the server. This multi-dimensional error capture mechanism comprehensively collects runtime exceptions, ensuring the integrity and diversity of error data, and providing a rich data foundation for subsequent error location and repair.
[0104] This method can effectively cover various error scenarios that may occur in complex applications, support comprehensive monitoring of front-end applications, significantly improve the reliability of error handling and the stability of system operation, and is particularly suitable for large-scale, modular front-end development projects.
[0105] In the embodiment of the present application, window.onerror is a global error handling function provided by JavaScript, which is bound to the browser window object (window). It is used to catch uncaught exceptions that occur when JavaScript is running, such as syntax errors, undefined variable references, or runtime logic errors.
[0106] The unhandledrejection event is a browser event that occurs when a Promise (an object used to handle asynchronous operations in JavaScript) is rejected and not handled by a .catch() or try-catch method. It is managed by the browser's event loop and is typically listened to via window.addEventListener('unhandledrejection', handler). The event object contains the rejected Promise instance and the reason for the error (such as an error message or exception object).
[0107] A React error boundary is a special React component that catches JavaScript errors that occur during the rendering process of its child component tree, such as state update errors, render function exceptions, or problems in component lifecycle methods. Error boundary components catch and handle errors by implementing the static getDerivedStateFromError or componentDidCatch methods, preventing them from spreading and crashing the entire application.
[0108] In some optional implementations, first, the window.onerror function is used to capture global errors in the JavaScript runtime, such as syntax errors or exceptions caused by undefined variables, to obtain error information and stack traces. Then, by listening to the unhandledrejection event, unhandled Promise exceptions are captured, such as situations where asynchronous operations fail but are not handled by catch, and relevant error details are recorded. In addition, for React applications, the React ErrorBoundary component is used to capture errors in the rendering process, such as component rendering failures or state update exceptions, to ensure that such errors do not cause the entire application to crash. The captured JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into a unified error message format, including error type, description, stack information, etc. Subsequently, these error messages are uploaded to the server via a network request (such as HTTP POST) for subsequent analysis and repair.
[0109] In some optional implementations, the AST backtracking analysis is performed based on the source code location to obtain an AST analysis result, where the AST analysis result includes an AST error node and context information, including:
[0110] Generate a first abstract syntax tree based on the error source code location and locate the AST error node;
[0111] Perform context dependency analysis on AST error nodes to obtain context information.
[0112] Based on the error source code location, a first abstract syntax tree is generated, the AST error node is located, and detailed context information is obtained through context dependency analysis. This method provides high-precision error location capabilities through in-depth analysis of the code structure, laying a solid foundation for subsequent intelligent repair.
[0113] AST backtracking analysis provides a deep understanding of code logic and dependencies, ensuring comprehensive and accurate error analysis results. This method is particularly suitable for handling errors in complex code bases and supports accurate analysis of dynamically generated or modularized code, thereby improving the success rate of code repairs and the overall robustness of the system.
[0114] In some optional embodiments, the system uses the error source code location (such as file path, line number and column number) obtained by reverse parsing the Source Map to extract the relevant code snippet. Then, a JavaScript parser (such as Esprima or Babel Parser) is used to parse the code snippet into a first abstract syntax tree (AST), which is a tree data structure representing the code structure, containing nodes such as variable declarations, function calls, conditional statements, etc. Based on the error location information, the system traverses the AST and locates the specific AST node (error node) corresponding to the error line number and column number, such as an erroneous function call or variable reference node. This process is achieved by comparing the source code location with the range information of the AST node (such as the start and end attributes) to ensure an accurate match.
[0115] Subsequently, the system performs context dependency analysis on the located AST error node to obtain relevant context information. Specifically, the system analyzes the node's dependencies and semantic context by traversing the parent node, sibling nodes, and subtrees of the AST error node. For example, if the error node is a function call, the system will check its parameters, caller, and related variable declarations to extract context information such as variable scope, function definition, or external dependent modules. In addition, the system may analyze control flow (such as conditional branches or loop structures) to understand the environment in which the error occurred.
[0116] See also Figure 2 , Figure 2 A schematic diagram of a process for obtaining a repair code block provided in an embodiment of the present application is shown.
[0117] In some optional implementations, calling the OpenAI model to obtain a repair code block based on the error information and AST analysis results includes:
[0118] S1: Based on the error message and AST analysis results, construct AI prompt words and input them into the OpenAI model;
[0119] S2: Use the OpenAI model to obtain repair code blocks;
[0120] S3: Use the code verifier to verify the correctness of the repaired code block;
[0121] S3a: If the repair code block is correct, do nothing;
[0122] S3b: If the repair code block is incorrect, optimize the AI prompt word and execute S1.
[0123] Therefore, this method constructs AI prompts by combining error information and AST analysis results, calls the OpenAI model to generate a repair code block, and uses a code verifier to verify the correctness of the repair code. If the code is incorrect, it is regenerated by optimizing the prompts until it meets the requirements.
[0124] This approach leverages artificial intelligence to automate code repair, significantly reducing the manual workload for developers. Verification and iteration ensure the high quality and reliability of the repaired code. This intelligent repair approach can quickly adapt to a variety of error scenarios, providing an efficient solution for maintaining complex applications and is particularly suitable for development environments with rapid iterations.
[0125] In some optional embodiments, in the error repair process of the target application, an AI prompt word is first constructed based on the captured error information (such as error messages, stack traces) and AST analysis results (including error nodes and their contextual information, such as variable dependencies and function call relationships). The system integrates the error information and AST analysis results into a structured text description, such as describing the error type, error code snippet, context variables and related code logic, to form a clear input prompt. Subsequently, the prompt word is input into the OpenAI model. The OpenAI model analyzes the cause of the error based on the semantic content of the prompt word and generates a corresponding repair code block, such as a code snippet that repairs undefined variables, logical errors or syntax problems. After generating the repair code block, the system uses a code verifier to check the code block. The verifier checks the syntactic correctness, logical consistency and compatibility with the context of the repair code through static analysis or simulated execution. If the verifier confirms that the repair code block is correct, the system directly accepts the code block and prepares for the subsequent patch generation step. If the verifier finds problems with the repair code block, such as syntax errors or unresolved original errors, the system will optimize the AI prompt words, such as adding more detailed context information, error descriptions, or adjusting the prompt structure. The optimized prompt words are then re-input into the OpenAI model and the generation and verification process is repeated until the correct repair code block is obtained. The code is as follows:
[0126]
[0127] In some optional implementations, generating a repair patch based on the repair code block and pushing the patch to the client includes:
[0128] Modify the erroneous source code based on the repair code block and generate a second abstract syntax tree;
[0129] Performing comparative analysis on the first abstract syntax tree and the second abstract syntax tree to generate a repair patch;
[0130] Push the fix patch to the client.
[0131] This method modifies the erroneous source code based on the repair code block, generates a second abstract syntax tree, and then generates a repair patch by comparing the first and second abstract syntax trees. This patch is then pushed to the client. This method ensures the accuracy and minimizes modifications of the patch through AST comparison, effectively avoiding unnecessary code changes and improving the reliability and stability of the repair process.
[0132] The automated patch generation process simplifies the development process, shortens the time from error discovery to fix deployment, and ensures that the fix patch can be seamlessly integrated into the target application, significantly improving the efficiency of code maintenance and making it suitable for real-time application scenarios with dynamic updates.
[0133] In some optional implementations, pushing the repair patch to the client includes:
[0134] Use WebSocket to push the fix to the client;
[0135] Utilize Service Worker cache fix patch;
[0136] Deploy a monitoring module in the Service Worker to automatically download fixes and update them in the target application.
[0137] Therefore, this method leverages WebSocket technology to push patches to clients in real time, integrates ServiceWorker for patch caching, and deploys a monitoring module to automatically download and update patches. This push mechanism enables rapid patch distribution, significantly improves update efficiency, and reduces the impact of network latency on application operations.
[0138] Service Worker's caching feature ensures stable patch storage on the client, while the automatic update mechanism further reduces maintenance costs and ensures continuous application operation and stability. This approach is particularly suitable for distributed systems or highly concurrent applications, effectively supporting real-time updates and high system availability.
[0139] In some optional embodiments, in systems implementing automated code patching and hot updates, a server-side build system and a client-side update agent work together to achieve seamless updates. First, after receiving a fix patch, the server initiates the build system to rebuild the affected code modules. The server parses the patch content, identifying the source code files and specific code snippets that require modification. It then uses a build tool (such as Webpack or Rollup) to incrementally compile the relevant modules to generate updated code packages. This build process ensures compatibility between the new code and existing modules and optimizes the output to reduce transmission size. After the build is complete, the server pushes the fix patch to the client in real time via the WebSocket protocol. A Service Worker deployed on the client acts as the update agent, receiving and processing the patch. After listening for the patch push via WebSocket, the Service Worker downloads and caches it locally, ensuring access to the patch content even during network outages. The Service Worker then triggers the update logic, dynamically loading the updated code module to replace the corresponding error code in the application, achieving a refresh-free hot update. This entire process eliminates the need to restart the application or refresh the page, maintaining user continuity.
[0140] Device embodiment
[0141] See also Figure 3 , Figure 3 A structural block diagram of a code automatic repair and hot update device provided in an embodiment of the present application is shown.
[0142] The embodiment of the present application also provides a code automatic repair and hot update device, the specific implementation of which is consistent with the implementation and technical effects recorded in the above method embodiment, and some contents will not be repeated here.
[0143] The present application provides a device for automatic code repair and hot update, the device comprising a processor configured to implement the following steps:
[0144] The monitoring module 101 is used to capture runtime error information in the target application using a global error monitor;
[0145] Positioning module 102, used to reversely parse error information using Source Map to locate the error source code location;
[0146] An analysis module 103 is configured to perform an AST backtrace analysis based on a source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information;
[0147] A repair module 104 is configured to call the OpenAI model to obtain a repair code block based on the error information and the AST analysis result;
[0148] The updating module 105 is configured to generate a repair patch based on the repair code block and push the patch to the client.
[0149] In some optional implementations, the processor is configured to capture runtime error information using a global error listener in the target application in the following manner:
[0150] Use the window.onerror function to capture JavaScript runtime errors;
[0151] Use the unhandledrejection event to capture Promise exceptions;
[0152] Use React boundary components to catch React rendering errors;
[0153] JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into the error message and uploaded to the server.
[0154] In some optional embodiments, the processor is configured to perform AST backtracking analysis based on the source code location in the following manner to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information:
[0155] Generate a first abstract syntax tree based on the error source code location and locate the AST error node;
[0156] Perform context dependency analysis on AST error nodes to obtain context information.
[0157] In some optional embodiments, the processor is configured to call the OpenAI model to obtain a repair code block based on the error information and the AST analysis result in the following manner:
[0158] S1: Based on the error message and AST analysis results, construct AI prompt words and input them into the OpenAI model;
[0159] S2: Use the OpenAI model to obtain repair code blocks;
[0160] S3: Use the code verifier to verify the correctness of the repaired code block;
[0161] S3a: If the repair code block is correct, do nothing;
[0162] S3b: If the repair code block is incorrect, optimize the AI prompt word and execute S1.
[0163] In some optional implementations, the processor is configured to generate a repair patch based on the repair code block and push the patch to the client in the following manner:
[0164] Modify the erroneous source code based on the repair code block and generate a second abstract syntax tree;
[0165] Performing comparative analysis on the first abstract syntax tree and the second abstract syntax tree to generate a repair patch;
[0166] Push the fix patch to the client.
[0167] In some optional implementations, the processor is configured to push the repair patch to the client in the following manner, including:
[0168] Use WebSocket to push the fix to the client;
[0169] Utilize Service Worker cache fix patch;
[0170] Deploy a monitoring module in the Service Worker to automatically download fixes and update them in the target application.
[0171] Device Example
[0172] An embodiment of the present application also provides an electronic device, which includes a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the steps of any of the above methods are implemented. The specific implementation method is consistent with the implementation method and the technical effect achieved in the above method embodiment, and some contents will not be repeated here.
[0173] See 4, Figure 4 A structural framework diagram of an electronic device provided in an embodiment of the present application is shown.
[0174] The electronic device includes at least one memory 210, at least one processor 220, and a bus 230 connecting different platform systems.
[0175] The memory 210 may include a readable medium in the form of a volatile memory, such as a random access memory (RAM) 211 and / or a cache memory 212 , and may further include a read-only memory (ROM) 213 .
[0176] The memory 210 also stores a computer program, which can be executed by the processor 220, so that the processor 220 implements the steps of any of the above methods.
[0177] The memory 210 may also include a utility 214 having at least one program module 215, such program module 215 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0178] Accordingly, the processor 220 may execute the aforementioned computer program and the utility 214 .
[0179] The processor 220 may be implemented as one or more application specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field programmable gate arrays (FPGAs), or other electronic components.
[0180] Bus 230 may represent one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor, or a local bus using any of a variety of bus architectures.
[0181] The electronic device may also communicate with one or more external devices 240, such as a keyboard, pointing device, Bluetooth device, etc., and may also communicate with one or more devices capable of interacting with the electronic device, and / or any device that enables the electronic device to communicate with one or more other computing devices (e.g., a router, modem, etc.). Such communication may be performed via input / output interface 250. Furthermore, the electronic device may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via network adapter 260. The network adapter 260 may communicate with other modules of the electronic device via bus 230. It should be understood that, although not shown in the figures, other hardware and / or software modules may be used in conjunction with the electronic device, including but not limited to microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage platforms.
[0182] System Example
[0183] See also Figure 5 , Figure 5 A schematic diagram of the structure of a code automatic repair and hot update system provided in an embodiment of the present application is shown.
[0184] The present application also provides a system for automatic code repair and hot update, the system comprising:
[0185] The above electronic equipment.
[0186] Media Examples
[0187] An embodiment of the present application also provides a chip, which stores a computer program. When the computer program is executed by a processor, the steps of any of the above methods are implemented. The specific implementation method is consistent with the implementation method and the technical effect achieved in the above method embodiment, and some contents will not be repeated here.
[0188] See also Figure 6 , Figure 6 A schematic diagram of the structure of a program product provided in an embodiment of the present application is shown.
[0189] The program product is used to implement any of the above methods. The program product can use a portable compact disc read-only memory (CD-ROM) and include program code, and can be run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited to this. In the embodiment of the present application, the readable storage medium can be any tangible medium that contains or stores a program, and the program can be used by or in combination with an instruction execution system, device or device. The program product can use any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0190] The chip may include a data signal propagated in baseband or as part of a carrier wave, which carries readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the foregoing. The readable storage medium may also be any readable medium that can send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, device, or component. The program code contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical cable, RF, or any suitable combination of the foregoing. The program code for performing the operations of the present invention may be written in any combination of one or more programming languages, including object-oriented programming languages such as Java and C++, as well as conventional procedural programming languages such as C, Python, or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user computing device, as a standalone software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. Where a remote computing device is involved, the remote computing device may be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computing device (e.g., through the Internet using an Internet service provider).
[0191] This application is explained from the perspectives of purpose of use, effectiveness, progress and novelty, and has complied with the functional enhancement and use requirements emphasized by the Patent Law. The above description and drawings of this application are only preferred embodiments of this application and are not intended to limit this application. Therefore, all structures, devices, features, etc. that are similar or identical to those of this application, that is, all equivalent replacements or modifications made in accordance with the scope of the patent application of this application, should fall within the scope of protection of the patent application of this application.
Claims
1. A method for automatic code repair and hot update, characterized in that: Repairing a code vulnerability in a target application and pushing a repair patch to a client, the method comprising: In the target application, use the global error listener to capture runtime error information; Use Source Map to reverse parse error information to locate the source code of the error; Performing AST backtracking analysis based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information; Based on the error message and AST analysis results, call the OpenAI model to obtain the repair code block; Generate a repair patch based on the repair code block and push it to the client.
2. The method for automatic code repair and hot update according to claim 1, characterized in that: In the target application, using a global error listener to capture runtime error information includes: Use the window.onerror function to capture JavaScript runtime errors; Use the unhandledrejection event to capture Promise exceptions; Use React boundary components to catch React rendering errors; JavaScript runtime errors, Promise exceptions, and React rendering errors are integrated into the error message and uploaded to the server.
3. The code automatic repair and hot update method according to claim 1, characterized in that: The AST backtracking analysis is performed based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information, including: Generate a first abstract syntax tree based on the error source code location and locate the AST error node; Perform context dependency analysis on AST error nodes to obtain context information.
4. The method for automatic code repair and hot update according to claim 3, characterized in that: Based on the error information and AST analysis results, the OpenAI model is called to obtain the repair code block, including: S1: Based on the error message and AST analysis results, construct AI prompt words and input them into the OpenAI model; S2: Use the OpenAI model to obtain repair code blocks; S3: Use the code verifier to verify the correctness of the repaired code block; S3a: If the repair code block is correct, do nothing; S3b: If the repair code block is incorrect, optimize the AI prompt word and execute S1.
5. The method for automatic code repair and hot update according to claim 3, characterized in that: Generating a repair patch based on the repair code block and pushing it to the client includes: Modify the erroneous source code based on the repair code block and generate a second abstract syntax tree; Performing comparative analysis on the first abstract syntax tree and the second abstract syntax tree to generate a repair patch; Push the fix patch to the client.
6. The method for automatic code repair and hot update according to claim 5, characterized in that: Pushing the repair patch to the client includes: Use WebSocket to push the fix to the client; Utilize Service Worker cache fix patch; Deploy a monitoring module in the Service Worker to automatically download fixes and update them in the target application.
7. A code automatic repair and hot update device, characterized in that: The code automatic repair and hot update device includes: The monitoring module is used to capture runtime error information using the global error listener in the target application; The positioning module is used to reversely parse error information using Source Map to locate the source code of the error; An analysis module is used to perform AST backtracing analysis based on the source code location to obtain an AST analysis result, wherein the AST analysis result includes an AST error node and context information; The repair module is used to call the OpenAI model to obtain the repair code block based on the error information and AST analysis results; The update module is used to generate a repair patch based on the repair code block and push it to the client.
8. An electronic device, characterized in that: The electronic device includes a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the steps of implementing the method according to any one of claims 1 to 6.
9. A code automatic repair and hot update system, characterized in that: The automatic code repair and hot update system includes: The electronic device according to claim 8.
10. A chip, characterized in that: The chip stores a computer program, and when the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Code repair method, device, equipment and medium
CN117555720B