Code modification method and device based on semantic update fragment generation

By using semantic update fragment generation and reinforcement learning strategies, the problems of high latency and insufficient reliability in large language model code modification are solved, achieving low-cost and efficient code modification and ensuring the accuracy and compliance of the code with engineering standards.

CN121722426BActive Publication Date: 2026-04-28BEIJING SILICON HEART TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING SILICON HEART TECH CO LTD
Filing Date
2026-02-26
Publication Date
2026-04-28

AI Technical Summary

Technical Problem

Existing code modification techniques based on large language models suffer from high inference latency and insufficient reliability in generating long texts. In particular, in large-scale software projects, the surge in tokens caused by full file rewriting and the "intermediate loss" phenomenon seriously affect the integrity and correctness of the code.

Method used

A semantic update fragment generation method is adopted. By obtaining the original code, modified description information and target code from multiple triples, semantic update fragments and first reconstructed code are generated. Semantic placeholders are used to replace the unmodified code fragments, and the target code modification model is used for verification and training. A reinforcement learning strategy is introduced to implement a reward and punishment mechanism to ensure the accuracy and purity of the code modification.

Benefits of technology

It significantly reduces resource consumption and latency, improves the success rate of modification and location, and the generated code modifications conform to engineering standards, ensuring the security and purity of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121722426B_ABST
    Figure CN121722426B_ABST
Patent Text Reader

Abstract

The application provides a code modification method and device based on semantic update fragment generation. The code modification method comprises the following steps: generating a semantic update fragment and a first reconstruction code according to original code and modification description information; performing code modification verification on the semantic update fragment according to the code difference between the original code and the first reconstruction code; when the code modification verification is passed, generating a second reconstruction code by modifying the original code according to the semantic update fragment, and determining whether the code modification of the second code modification model is correct according to the code comparison result between the second reconstruction code and the first reconstruction code; taking the original code, the semantic update fragment and the first reconstruction code corresponding to the second code modification model with code modification errors as model training data to perform model training to obtain a target code modification model; and modifying the to-be-modified code by using the target code modification model to obtain a complete code. The application can reduce inference delay and improve code modification efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of large language model technology, specifically to a code modification method and apparatus based on semantic update fragment generation. Background Technology

[0002] In the field of intelligent software development, code-assisted generation and modification techniques based on Large Language Models (LLMs) have become a research hotspot. Currently, general-purpose large models, such as GPT-4 and Claude 3.5, generally adopt a full rewrite mechanism when performing code modification tasks, that is, regenerating the complete file content, including the unchanged parts, according to user instructions.

[0003] However, this mechanism faces significant technical bottlenecks in practical applications. On the one hand, the codebase of modern software projects continues to expand, with individual source files often reaching thousands of lines. Rewriting entire files results in a linear positive correlation between the number of tokens output by the model and the file length. Since the number of tokens directly determines inference latency and computational cost, this mechanism leads to extremely high interaction latency and significant computational overhead. On the other hand, during the generation of long text sequences, large models are prone to the "Lost-in-the-Middle" phenomenon, meaning their ability to remember and retain information from the middle of long texts is significantly weakened. This leads to the original code, far from the modification instructions, being incorrectly truncated, omitted, or altered, compromising the integrity and correctness of the code and causing the edited program code to malfunction. Summary of the Invention

[0004] In view of this, this application proposes a code modification method and apparatus based on semantic update fragment generation to solve the problems of high inference latency and insufficient reliability of long text generation in existing code modification technologies.

[0005] The first aspect of this application proposes a code modification method based on semantic update fragment generation, the method comprising:

[0006] Obtain multiple triples for different programming languages; each triple includes the original code, modification description information, and target code for the corresponding programming language; the target code is standard code obtained by modifying the original code based on the modification description information; the modification description information includes the modification content, modification location, and modification reason;

[0007] For any triple, a semantic update fragment and a first reconstructed code are generated based on the original code and modification description information in the triple; the first reconstructed code is the code obtained by the first code modification model after modifying the original code according to the modification description information; the semantic update fragment includes a code modification fragment, a modification anchor code fragment, and a semantic placeholder; the semantic placeholder is used to represent the unmodified code fragment in the original code; the modification anchor code fragment is the context feature of the code modification position in the original code, and the code modification position is the position of the code modification fragment in the original code;

[0008] The semantic update fragment is modified and verified based on the code differences between the original code and the first reconstructed code.

[0009] Under the condition that the code modification verification is passed, the original code and the semantic update fragment are respectively input into multiple second code modification models of different types, so that each second code modification model modifies the original code according to the semantic update fragment to generate second reconstructed code, and determines whether the code modification of the second code modification model is correct based on the code comparison result between the second reconstructed code and the first reconstructed code;

[0010] The original code, the semantic update fragment, and the first reconstructed code corresponding to the second code modification model with the code correction error are used as model training data to train the target code modification model.

[0011] The target code is modified by modifying the model to obtain the complete modified code.

[0012] In this embodiment of the application, the target code modification model is obtained by using the original code corresponding to the second code modification model with the code modification error, the semantic update fragment, and the first reconstructed code as model training data, including:

[0013] The base model is invoked to generate third reconstructed code based on the original code and the semantic update fragment;

[0014] Determine whether the third refactored code contains runtime errors and modification errors. Runtime errors include syntax errors or improper use of semantic placeholders that prevent parsing. Modification errors include modifications to unspecified code regions in the original code.

[0015] If the aforementioned operational error exists, then the deduction of points for the first error penalty item of the basic model is determined;

[0016] If the aforementioned modification error exists, then the deduction of points for the second error penalty item of the base model is determined;

[0017] When the third refactoring code is consistent with the first refactoring code, the perfect match reward score of the base model is determined;

[0018] When the third refactored code does not have line-level errors and there are no modification errors, the guaranteed reward score of the base model is determined;

[0019] The training score of the base model is calculated based on the deduction of the first error penalty item, the deduction of the second error penalty item, the perfect match reward score, and the guaranteed reward score.

[0020] If the training score is less than a preset score threshold, the base model is iteratively optimized until the training score of the base model is greater than or equal to the preset score threshold.

[0021] If the training score is greater than or equal to the preset score threshold, then the base model is determined as the target code modification model.

[0022] In this embodiment of the application, the method further includes:

[0023] The basic model is trained using a composite reward signal, which includes:

[0024]

[0025] in, Indicates a composite reward signal. Indicates a perfect match reward. This indicates the first error penalty item. This indicates the second error penalty item. This indicates a guaranteed minimum reward. This represents the first penalty coefficient corresponding to the first error penalty term. This represents the second penalty coefficient corresponding to the second error penalty term.

[0026] In this embodiment of the application, after generating a semantic update fragment and a first reconstructed code based on the original code in the triple and the modified description information, the method further includes:

[0027] The generated partial semantic update fragments are subjected to randomization enhancement processing; the randomization enhancement processing includes randomly replacing the semantic placeholders and / or fine-tuning the code indentation style, so as to improve the generalization ability of the target code modification model to different semantic placeholder expression forms.

[0028] In this embodiment of the application, the semantic update fragment is modified and verified based on the code differences between the original code and the first reconstructed code, including:

[0029] The original code is compared with the first reconstructed code to obtain the difference code;

[0030] Calculate the semantic similarity between the difference code and the semantic update fragment;

[0031] If the semantic similarity is greater than a preset similarity threshold, then the code modification verification of the semantic update fragment is deemed to have passed.

[0032] If the semantic similarity is less than or equal to the preset similarity threshold, then the code modification verification of the semantic update fragment is determined to be unsuccessful.

[0033] In this embodiment of the application, the modified code is obtained by modifying the code to be modified using the target code modification model, including:

[0034] Receive the code to be modified and the target modification description information input by the user;

[0035] A target semantic update fragment is generated based on the code to be modified and the target modification description information; the target semantic update fragment includes a target code modification fragment, a target modification anchor code fragment, and a target semantic placeholder; the target semantic placeholder is used to represent the unmodified code fragment in the code to be modified; the target modification anchor code fragment is the context feature code of the target code modification position in the code to be modified, and the target code modification position is the code position of the code modification fragment in the code to be modified;

[0036] The code to be modified and the target semantic update fragment are input into the target code modification model, so that the target code modification model can locate the target replacement position of the code modification fragment in the code to be modified according to the target modification anchor code fragment, and merge the code modification fragment into the target replacement position to obtain the modified complete code.

[0037] In this embodiment of the application, before obtaining multiple triples from different programming languages, the method further includes:

[0038] Obtain multiple raw triples for different programming languages. Each raw triple includes the original code, original modification description information, and target code for the corresponding coding language. The original modification description information is the original commit comment information in the code version control system. The original commit comment information includes code function description information and memo-type information, where the memo-type information is an information description without specific technical content.

[0039] For any one of the plurality of original triples, the modification description information is generated based on the target difference code between the original code in the original triple and the target code, and the original modification description information; wherein, the modification description information includes code function description information, and the code function description information includes modification content, modification location, and modification reason;

[0040] Generate a triplet for the corresponding programming language based on the original code, the modified description information, and the target code.

[0041] An embodiment of the second aspect of this application provides a code modification apparatus based on semantic update fragment generation, comprising:

[0042] The triplet acquisition module is used to acquire multiple triples from different programming languages. Each triple includes the original code, modification description information, and target code of the corresponding programming language. The target code is standard code obtained by modifying the original code based on the modification description information. The modification description information includes the modification content, modification location, and modification reason.

[0043] A semantic update fragment generation module is used to generate a semantic update fragment and a first reconstructed code for any triple, based on the original code and modification description information in the triple; the first reconstructed code is the code obtained by a first code modification model modifying the original code according to the modification description information; the semantic update fragment includes a code modification fragment, a modification anchor code fragment, and a semantic placeholder; the semantic placeholder is used to represent the unmodified code fragment in the original code; the modification anchor code fragment is the context feature of the code modification position in the original code, and the code modification position is the position of the code modification fragment in the original code;

[0044] The code modification verification module is used to verify the code modification of the semantic update segment based on the code differences between the original code and the first reconstructed code.

[0045] The code modification judgment module is used to input the original code and the semantic update fragment into multiple second code modification models of different types, respectively, under the condition that the code modification verification is passed, so that each second code modification model modifies the original code according to the semantic update fragment to generate second reconstructed code, and determines whether the code modification of the second code modification model is correct based on the code comparison result between the second reconstructed code and the first reconstructed code.

[0046] The model training module is used to train the target code modification model by using the original code corresponding to the second code modification model with the part of the code that was modified incorrectly, the semantic update fragment, and the first reconstructed code as model training data.

[0047] The code modification module is used to modify the code to be modified using the target code modification model to obtain the complete modified code.

[0048] An embodiment of the third aspect of this application provides a computer device including a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the computer instructions to perform the code modification method based on semantic update fragment generation described in the first aspect.

[0049] An embodiment of the fourth aspect of this application provides a computer-readable storage medium storing computer instructions for causing a computer to execute the code modification method based on semantic update fragment generation described in the first aspect above.

[0050] This application has the following computational effects:

[0051] 1. Extremely low resource consumption and latency: This application embodiment utilizes semantic placeholders to replace unmodified code fragments, which significantly reduces the data processing volume of the first code modification model, thereby improving the semantic update fragment generation efficiency of the first code modification model. The first code modification model only needs to output a very short modified fragment (saving 80%+ Tokens), while the target code modification model completes the heavy work of concatenating the entire file, significantly reducing end-to-end latency and inference costs.

[0052] 2. High success rate of modification location: Semantic placeholders act as location anchors. The target code modification model is trained to find the correct modification location based on semantic placeholders, function names, and contextual logic, thus naturally immune to line number errors and whitespace noise, greatly improving the success rate of modification location.

[0053] 3. Code Security and Purity: Traditional SFT (Supervised Fine-Tuning) can only teach the model "how to write code," while the RL (Reinforcement Learning) strategy of this invention, through a reward and deduction mechanism using composite reward signals, can explicitly teach the target code modification model "what should not be changed." This makes the generated code modifications highly accurate and perfectly compliant with engineering standards. Attached Figure Description

[0054] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the scope of this application. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings:

[0055] Figure 1 A flowchart illustrating a code modification method based on semantic update fragment generation provided in an embodiment of this application is shown.

[0056] Figure 2 This illustration shows a schematic diagram of a code modification apparatus based on semantic update fragment generation according to an embodiment of this application;

[0057] Figure 3 This illustration shows a schematic diagram of the structure of a computer device according to an embodiment of this application;

[0058] Figure 4 A schematic diagram of a storage medium provided in one embodiment of this application is shown. Detailed Implementation

[0059] Exemplary embodiments of this application will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of this application are shown in the drawings, it should be understood that this application may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of this application and to fully convey the scope of this application to those skilled in the art.

[0060] It should be noted that, unless otherwise stated, the technical or scientific terms used in this application shall have the ordinary meaning as understood by one of ordinary skill in the art to which this application pertains.

[0061] The technical scenarios involved in the embodiments of this application are described below.

[0062] The existing technology has the following technical defects:

[0063] Full rewrite leads to high latency and uncontrollable costs: When modifying existing large models (such as GPT-4, Claude 3.5), it is usually necessary to regenerate a complete file containing the unmodified parts. As the size of the codebase grows (e.g., a file with thousands of lines of code), this full generation leads to a surge in the number of output tokens. Because the number of tokens is directly proportional to inference latency and computational cost, this results in extremely high interaction latency and expensive API call costs. Furthermore, the generation of long texts is prone to triggering the "Lost-in-the-Middle" phenomenon in the model, causing the original code to be incorrectly truncated.

[0064] The vulnerability and failure of traditional diff / patch methods: Traditional Git Diff or UnifiedDiff formats heavily rely on precise line numbers and strict context matching. Because the large model of the Transformer architecture has an inherent "illusion" flaw when handling precise arithmetic (line number calculation), it often generates incorrect line numbers; furthermore, even a single space difference between the generated context and the source code will cause standard patch tools to fail to match. Therefore, automated code application solutions based on traditional Diff have extremely low success rates in real-world engineering and cannot meet production-grade requirements.

[0065] Uncontrollable Side Effects of General Models: When executing modification instructions, general models often "cleverly" perform formatting, variable renaming, or style adjustments outside the scope of the instructions. Because these unexpected modifications (side effects) pollute the code commit history (Git History) and increase the noise of code reviews, the generated code is difficult to adopt directly in the production environment.

[0066] To address the aforementioned technical deficiencies, the technical effect to be achieved by the technical solution provided in this application is as follows:

[0067] Rapid and Low-Cost Incremental Applications: This invention trains a specialized 4B-parameter-level small model (NeuralApplier) that can quickly reconstruct a complete new file based on the "semantic update chunks (Edit Chunks)" generated by the large model and the source code. Technical Results: The front-end large model only needs to output extremely short modified chunks (saving 80%+ Tokens), while the arduous task of concatenating the entire file is completed by a local or low-cost small model, significantly reducing end-to-end latency and inference costs.

[0068] Robust semantic anchor point localization and fault tolerance: This invention abandons absolute line numbers and instead uses update fragments containing semantic placeholders (such as / / ... keep existing code...). Technical effect: The 4B small model, after specialized post-training, has learned to locate modification points based on the semantic structure of the code (such as function signatures and class definitions) rather than rigid character matching. Even with minor formatting differences or line number offsets in the source code, the model can still accurately identify the modification location and correctly apply the changes, significantly improving the success rate of patches.

[0069] Precise Modification Based on Side Effect Penalty: This invention introduces a specific reinforcement learning (RL) reward function to weight and penalize "patch errors" and "side effect errors." Technical Effect: This forces the model to learn the "minimum modification principle" in the post-training stage, meaning only the parts required by the instructions are modified, strictly leaving the rest of the code unchanged. This ensures the purity and atomicity of code changes, making it compliant with industry-grade code review standards.

[0070] According to an embodiment of this application, a code modification method based on semantic update fragment generation is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.

[0071] This embodiment provides a code modification method based on semantic update fragment generation. Figure 1 This is a flowchart of a code modification method based on semantic update fragment generation according to an embodiment of this application, such as... Figure 1 As shown, the process includes the following steps:

[0072] Step S101: Obtain multiple triples for different programming languages; each triple includes the original code, modified description information, and target code of the corresponding programming language.

[0073] Specifically, the target code is standard code obtained by modifying the original code based on the modification description information; the modification description information includes the modification content, the modification location, and the reason for the modification.

[0074] In some specific embodiments, the target code may be artificially generated, obtained by manually modifying the original code based on the modified description information.

[0075] In some specific embodiments, different programming languages ​​include, but are not limited to, Python, Java, Go, C++, and TypeScript.

[0076] In some specific embodiments, before step S101 described above, the method further includes steps a1-a3:

[0077] Step a1: Obtain multiple raw triples for different programming languages. Each raw triple includes the original code of the corresponding coding language, the original modification description information, and the target code.

[0078] Specifically, the original modification description information is the original commit comment information in the code version control system; the original commit comment information includes code function description information and memo-type information, wherein the memo-type information is information description without specific technical content.

[0079] More specifically, code change data across multiple languages ​​and projects is extracted from the historical commit records of code version control systems (such as Git and SVN). Each original triple contains: the original code (Code_Before), which can be understood as the code before modification; the original change description (Commit_Message), which can be understood as the commit comments filled in by the developer when the code was generated; and the target code (Code_After), which can be understood as the modified code. Among them, the original change description is unstructured raw natural language text, which is composed of a mixture of code function description information (including technical substance) and memo-like information (without technical substance).

[0080] Step a2: For any one of the plurality of original triples, generate the modification description information based on the target difference code between the original code in the original triple and the target code, and the original modification description information.

[0081] Specifically, the modification description information includes code function description information, which includes the modification content (i.e., the specific code change operation, such as adding a function or deleting a parameter), the modification location (i.e., the code coordinates where the change occurred, such as class name, method name, or line number range), and the modification reason (i.e., the technical motivation for the change, such as fixing defects or optimizing performance).

[0082] In some specific embodiments, modification description information can be generated as follows: The original code, target code, and original modification description information are input into a pre-defined intent understander (e.g., the DeepSeek-v3.2 model with strong reasoning capabilities), and the modification description information is output. This modification description information explains "what was modified" and "why the modification was made." This step solves the problem of the vague language of the original modification description information (Commit_Message), providing a high-quality prompt for subsequent training.

[0083] In some specific embodiments, modification description information can also be generated in the following way: First, a preset large language model is invoked to compare the original code and the target code to obtain the target difference code; then, the code function description information in the original modification description information is identified, and memo-like information is filtered out; finally, the objective change characteristics of the target difference code and the subjective intent of the code function description information are integrated to generate standardized modification description information. This method can transform unstructured, noisy original commit comments into structured, standard instructions that can directly drive the code modification model.

[0084] Step a3: Generate a triplet for the corresponding programming language based on the original code, the modified description information, and the target code.

[0085] Step S102: For any triple, generate a semantic update fragment and a first reconstructed code based on the original code and modified description information in the triple.

[0086] Specifically, the first reconstructed code is the code obtained by the first code modification model after modifying the original code according to the modification description information.

[0087] In some specific embodiments, the original code and modified description information can be input into a first code modification model (e.g., Claude-4.5-Opus, which has a strong instruction compliance capability, can be used as a "teacher model") to output a semantic update fragment (Edit_Chunk) and a first refactored code (Applied_Code).

[0088] In some specific embodiments, the semantic update fragment includes a code modification fragment, a modification anchor code fragment, and a semantic placeholder; the semantic placeholder is used to represent the unmodified code fragment in the original code; the modification anchor code fragment is a context feature of the code modification location in the original code, and the code modification location is the position of the code modification fragment in the original code, as shown below:

[0089] Suppose the user command is: "Change the tax rate in the calculate_total function from 0.05 to 0.08".

[0090] The frontend (i.e., the first code modification model) generates Edit_Chunks (i.e., semantic update fragments): ```python / / ... keep existing code...`

[0091] def calculate_total(price):

[0092] # Update tax rate

[0093] tax = 0.08

[0094] return price * (1 + tax)

[0095] / / ...keep existing code...

[0096] ```

[0097] In the example above, " / / ... keep existing code..." represents a semantic placeholder used to replace unmodified code snippets;

[0098] In the example above, "tax = 0.08" represents a modified code snippet;

[0099] In the example above, "def calculate_total(price):

[0100] # Update tax rate

[0101] "return price * (1 + tax)" indicates a modified anchor code snippet, which is a contextual feature of the modified code snippet used to locate the modification location.

[0102] This application embodiment can significantly reduce the data processing volume of the first code modification model by using semantic placeholders to replace unmodified code segments, thereby improving the semantic update segment generation efficiency of the first code modification model.

[0103] In some specific embodiments, after step S102, the method further includes:

[0104] The generated partial semantic update fragments are subjected to randomization enhancement processing; the randomization enhancement processing includes randomly replacing the semantic placeholders (e.g., replacing " / / ... keep existing code..." with " / * code remains* / "), and / or fine-tuning the code indentation style to improve the generalization robustness of the target code modification model and prevent it from overfitting to specific string patterns.

[0105] Step S103: Perform code modification verification on the semantic update fragment based on the code differences between the original code and the first reconstructed code.

[0106] In some specific embodiments, step S103 above includes steps S1031-S1034:

[0107] Step S1031: Compare the original code with the first reconstructed code to obtain the difference code.

[0108] Specifically, a structured comparison is performed between the original code and the first refactored code to identify and extract the differing code. The differing code represents the complete code change operations actually performed by the first code modification model, including the location and content information of added code segments, deleted code segments, and modified code segments.

[0109] Step S1032: Calculate the semantic similarity between the difference code and the semantic update fragment.

[0110] Specifically, semantic similarity is used to measure the degree of agreement between the proposed changes described in the semantic update fragment and the actual changes in the first refactored code.

[0111] In some specific embodiments, the similarity calculation methods include, but are not limited to: cosine similarity based on code embedding vectors and AST similarity based on tree edit distance.

[0112] In some specific embodiments, the similarity calculation dimensions include, but are not limited to: consistency of modification location (i.e., whether the modified anchor code snippet in the semantic update fragment corresponds to the changed location of the difference code), consistency of modification content (whether the modified code snippet in the semantic update fragment is equivalent to the changed content of the difference code), and rationality of semantic placeholders (whether the semantic placeholders correctly identify the unchanged context range).

[0113] Step S1033: If the semantic similarity is greater than the preset similarity threshold, then the code modification verification of the semantic update fragment is determined to be successful.

[0114] Step S1034: If the semantic similarity is less than or equal to the preset similarity threshold, then the code modification verification of the semantic update fragment is determined to be unsuccessful.

[0115] Specifically, the preset similarity threshold can be set according to the actual situation, and no specific limitation is made here.

[0116] Step S104: Under the condition that the code modification verification is passed, the original code and the semantic update fragment are respectively input into multiple second code modification models of different types, so that each second code modification model modifies the original code according to the semantic update fragment to generate second reconstructed code, and determines whether the code modification of the second code modification model is correct based on the code comparison result between the second reconstructed code and the first reconstructed code.

[0117] Specifically, the model architectures of the various second-code modifications differ, such as DeepSeek-v3.2 and GLM-4.7.

[0118] In this embodiment, multiple triples from different programming languages ​​correspond one-to-one with multiple code modification data (including semantic update fragments and first reconstructed code). Each code modification data is input into multiple second code modification models, so that each second code modification model modifies the original code according to the semantic update fragment in the code modification data to generate second reconstructed code, and compares the second reconstructed code with the first reconstructed code. If the two are consistent, it is determined that the code modification of the second code modification model is correct; if the two are inconsistent, it is determined that the code modification of the second code modification model has failed.

[0119] For any code modification data, if multiple second code modification models all indicate code modification failure for that code modification data, then the code modification data itself is determined to have a problem and cannot be used as training data for the target code modification model; conversely, if some of the multiple second code modification models indicate code modification failure or code modification success for that code modification data, then that code modification data can be used as training data for the target code modification model.

[0120] Modifying code to make all models correct has a near-zero training effect on the model because the model has already mastered it, and repeated training yields no incremental benefit. Modifying code to make all models incorrect also has a near-zero training effect on the model because the data itself has labeling problems, which can easily introduce noise and is not useful for model training. Modifying code to make some models incorrect or some models correct is at the model's capability boundary, which can enable the model to have the maximum learning gradient and has the greatest training effect on the model.

[0121] Step S105: Use the original code corresponding to the second code modification model with the part of the code that was modified incorrectly, the semantic update fragment, and the first reconstructed code as model training data to train the model and obtain the target code modification model.

[0122] In some specific embodiments, step S105 above includes steps S1051-S1059:

[0123] Step S1051: Invoke the base model to generate third reconstructed code based on the original code and the semantic update fragment.

[0124] Specifically, the base model includes, but is not limited to, Qwen3-4B-Instruct-2507 (4B parameter scale). The input to the base model is the original code (Code_Before) and the semantic update fragment (edit_chunk), and the output is the third reconstructed code (applied_code). The training algorithm includes, but is not limited to, the DAPO optimization algorithm.

[0125] Step S1052: Determine whether the third refactored code has runtime errors and modification errors.

[0126] Specifically, the runtime error includes the existence of syntax errors or improper use of semantic placeholders that prevents parsing; the modification error includes modifications to unspecified code regions in the original code.

[0127] Step S1053: If the runtime error exists, determine the deduction of the first error penalty item of the basic model.

[0128] Step S1054: If the modification error exists, determine the deduction of the second error penalty item of the basic model.

[0129] Step S1055: When the third reconstruction code is consistent with the first reconstruction code, determine the perfect match reward score of the base model.

[0130] Step S1056: When the third refactored code does not have a line-level error and there is no modification error, determine the guaranteed reward score of the basic model.

[0131] Step S1057: Calculate the training score of the base model based on the deduction of the first error penalty item, the deduction of the second error penalty item, the perfect match reward score, and the guaranteed reward score.

[0132] Step S1058: If the training score is less than a preset score threshold, the base model is iteratively optimized until the training score of the base model is greater than or equal to the preset score threshold.

[0133] Specifically, the preset score threshold can be set according to the actual situation, and no specific limitation is made here.

[0134] Step S1059: If the training score is greater than or equal to the preset score threshold, then the base model is determined as the target code modification model.

[0135] In some specific embodiments, in steps S1051-S1059 above, the basic model can be trained using a composite reward signal.

[0136] Specifically, the composite reward signal includes:

[0137]

[0138] in, Indicates a composite reward signal. Indicates a perfect match reward. This indicates the first error penalty item. This indicates the second error penalty item. This indicates a guaranteed minimum reward. This represents the first penalty coefficient corresponding to the first error penalty term. This represents the second penalty coefficient corresponding to the second error penalty term.

[0139] Step S106: Modify the code to be modified using the target code modification model to obtain the modified complete code.

[0140] In some specific embodiments, step S106 above includes steps S1061-S1063:

[0141] Step S1061: Receive the code to be modified and the target modification description information input by the user.

[0142] Step S1062: Generate a target semantic update fragment based on the code to be modified and the target modification description information.

[0143] Specifically, the target semantic update fragment includes a target code modification fragment, a target modification anchor code fragment, and a target semantic placeholder; the target semantic placeholder is used to represent the unmodified code fragment in the code to be modified; the target modification anchor code fragment is the context feature code of the target code modification position in the code to be modified, and the target code modification position is the code position of the code modification fragment in the code to be modified;

[0144] Step S1063: Input the code to be modified and the target semantic update fragment into the target code modification model, so that the target code modification model locates the target replacement position of the code modification fragment in the code to be modified according to the target modification anchor code fragment, and merges the code modification fragment into the target replacement position to obtain the modified complete code.

[0145] Regarding steps S1061-S1063 described above, the present application provides the following embodiments for illustration:

[0146] Suppose the user command is to "change the tax rate in the calculate_total function from 0.05 to 0.08".

[0147] Phase 1: Frontend (First Code Modification Model) generates Edit_Chunk (semantic update fragment) based on the code to be modified and the modification description information (obtained from the user instructions above), as shown below: ``` python / / ... keepexisting code...

[0148] def calculate_total(price):

[0149] # Update tax rate

[0150] tax = 0.08

[0151] return price * (1 + tax)

[0152] / / ...keep existing code...

[0153] ```

[0154] Phase 2: Backend (Target Code Modification Model):

[0155] Input: The code to be modified + the above Edit_Chunk.

[0156] Processing: The model recognizes / / ... keep... and, using the memory of the code to be modified, outputs the rest as is, replacing only the function body of calculate_total.

[0157] Output: The complete modified code.

[0158] The embodiments of this application have the following computational effects:

[0159] 1. Extremely low resource consumption and latency: This application embodiment utilizes semantic placeholders to replace unmodified code fragments, which significantly reduces the data processing volume of the first code modification model, thereby improving the semantic update fragment generation efficiency of the first code modification model. The first code modification model only needs to output a very short modified fragment (saving 80%+ Tokens), while the target code modification model completes the heavy work of concatenating the entire file, significantly reducing end-to-end latency and inference costs.

[0160] 2. High success rate of modification location: Semantic placeholders act as location anchors. The target code modification model is trained to find the correct modification location based on semantic placeholders, function names, and contextual logic, thus naturally immune to line number errors and whitespace noise, greatly improving the success rate of modification location.

[0161] 3. Code Security and Purity: Traditional SFT (Supervised Fine-Tuning) can only teach the model "how to write code," while the RL (Reinforcement Learning) strategy of this invention, through a reward and deduction mechanism using composite reward signals, can explicitly teach the target code modification model "what should not be changed." This makes the generated code modifications highly accurate and perfectly compliant with engineering standards.

[0162] Corresponding to the above implementation of the code modification method based on semantic update fragment generation, this application also provides a code modification apparatus based on semantic update fragment generation, used to execute the code modification method based on semantic update fragment generation described in any of the above embodiments. For example... Figure 2 As shown, the code modification device based on semantic update fragment generation includes:

[0163] The triplet acquisition module is used to acquire multiple triples from different programming languages. Each triple includes the original code, modification description information, and target code of the corresponding programming language. The target code is standard code obtained by modifying the original code based on the modification description information. The modification description information includes the modification content, modification location, and modification reason.

[0164] A semantic update fragment generation module is used to generate a semantic update fragment and a first reconstructed code for any triple, based on the original code and modification description information in the triple; the first reconstructed code is the code obtained by a first code modification model modifying the original code according to the modification description information; the semantic update fragment includes a code modification fragment, a modification anchor code fragment, and a semantic placeholder; the semantic placeholder is used to represent the unmodified code fragment in the original code; the modification anchor code fragment is the context feature of the code modification position in the original code, and the code modification position is the position of the code modification fragment in the original code;

[0165] The code modification verification module is used to verify the code modification of the semantic update segment based on the code differences between the original code and the first reconstructed code.

[0166] The code modification judgment module is used to input the original code and the semantic update fragment into multiple second code modification models of different types, respectively, under the condition that the code modification verification is passed, so that each second code modification model modifies the original code according to the semantic update fragment to generate second reconstructed code, and determines whether the code modification of the second code modification model is correct based on the code comparison result between the second reconstructed code and the first reconstructed code.

[0167] The model training module is used to train the target code modification model by using the original code corresponding to the second code modification model with the part of the code that was modified incorrectly, the semantic update fragment, and the first reconstructed code as model training data.

[0168] The code modification module is used to modify the code to be modified using the target code modification model to obtain the complete modified code.

[0169] Optionally, the model training module is further configured to call the base model to generate third reconstructed code based on the original code and the semantic update fragment; determine whether the third reconstructed code has runtime errors and modification errors, wherein runtime errors include syntax errors or improper use of semantic placeholders that prevent parsing; and modification errors include modifications to unspecified code regions in the original code. If runtime errors exist, the deduction of a first error penalty item for the base model is determined; if modification errors exist, the deduction of a second error penalty item for the base model is determined; when the third reconstructed code is consistent with the first reconstructed code, the base model is determined to be consistent. The model's perfect match reward score; when the third refactored code has no line-level errors and no modification errors, the guaranteed reward score of the base model is determined; the training score of the base model is calculated based on the deduction of the first error penalty item, the deduction of the second error penalty item, the perfect match reward score, and the guaranteed reward score; if the training score is less than a preset score threshold, the base model is iteratively optimized until the training score of the base model is greater than or equal to the preset score threshold; if the training score is greater than or equal to the preset score threshold, the base model is determined as the target code modification model.

[0170] Optionally, the apparatus further includes: a randomization enhancement processing module, used to perform randomization enhancement processing on the generated partial semantic update fragments; the randomization enhancement processing includes randomly replacing the semantic placeholders and / or fine-tuning the code indentation style to improve the generalization ability of the target code modification model to different semantic placeholder expression forms.

[0171] Optionally, the code modification verification module is further configured to compare the original code with the first reconstructed code to obtain the difference code; calculate the semantic similarity between the difference code and the semantic update segment; if the semantic similarity is greater than a preset similarity threshold, then determine that the code modification verification of the semantic update segment has passed; if the semantic similarity is less than or equal to the preset similarity threshold, then determine that the code modification verification of the semantic update segment has failed.

[0172] Optionally, the code modification module is further configured to receive the code to be modified and the target modification description information input by the user terminal; generate a target semantic update fragment based on the code to be modified and the target modification description information; the target semantic update fragment includes a target code modification fragment, a target modification anchor code fragment, and a target semantic placeholder; the target semantic placeholder is used to represent the unmodified code fragment in the code to be modified; the target modification anchor code fragment is the context feature code of the target code modification position in the code to be modified, and the target code modification position is the code position of the code modification fragment in the code to be modified; input the code to be modified and the target semantic update fragment into the target code modification model, so that the target code modification model locates the target replacement position of the code modification fragment in the code to be modified based on the target modification anchor code fragment, and merges the code modification fragment into the target replacement position to obtain the modified complete code.

[0173] Optionally, the apparatus further includes: a triple generation module, used to obtain multiple original triples for different programming languages, each original triple including the original code, original modification description information, and target code of the corresponding coding language; wherein, the original modification description information is the original commit comment information in the code version control system; the original commit comment information includes code function description information and memo nature information, the memo nature information being information description without specific technical content; for any original triple among the multiple original triples, generating modification description information based on the target difference code between the original code and the target code in the original triple and the original modification description information; wherein, the modification description information includes code function description information, the code function description information including modification content, modification location, and modification reason; generating triples for the corresponding programming language based on the original code, the modification description information, and the target code.

[0174] The code modification apparatus based on semantic update fragment generation provided in the above embodiments of this application and the code modification method based on semantic update fragment generation provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications they store.

[0175] This application also provides a computer device for executing the above-described code modification method based on semantic update fragment generation. Please refer to... Figure 3 This illustrates a schematic diagram of a computer device provided by some embodiments of this application. For example... Figure 3As shown, the computer device 3 includes: a processor 300, a memory 301, a bus 302, and a communication interface 303. The processor 300, the communication interface 303, and the memory 301 are connected via the bus 302. The memory 301 stores a computer program that can run on the processor 300. When the processor 300 runs the computer program, it executes the code modification method based on semantic update fragment generation provided in any of the foregoing embodiments of this application.

[0176] The memory 301 may include high-speed random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Communication between this system network element and at least one other network element is achieved through at least one communication interface 303 (which can be wired or wireless), such as the Internet, wide area network, local area network, or metropolitan area network.

[0177] Bus 302 can be an ISA bus, PCI bus, or EISA bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. Memory 301 is used to store programs. After receiving an execution instruction, the processor 300 executes the program. The code modification method based on semantic update fragment generation disclosed in any of the foregoing embodiments can be applied to the processor 300, or implemented by the processor 300.

[0178] The processor 300 may be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method can be completed by the integrated logic circuitry in the hardware of the processor 300 or by instructions in software form. The processor 300 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this application can be directly embodied in the execution of a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software modules may reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The storage medium is located in memory 301. The processor 300 reads the information in memory 301 and, in conjunction with its hardware, completes the steps of the above method.

[0179] The computer device provided in this application embodiment and the code modification method based on semantic update fragment generation provided in this application embodiment are based on the same inventive concept and have the same beneficial effects as the methods they adopt, run or implement.

[0180] This application also provides a computer-readable storage medium corresponding to the code modification method based on semantic update fragment generation provided in the foregoing embodiments. Please refer to... Figure 4 The computer-readable storage medium shown is an optical disc 30, on which a computer program (i.e., a program product) is stored. When the computer program is run by a processor, it executes the code modification method based on semantic update fragment generation provided in any of the foregoing embodiments.

[0181] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical and magnetic storage media, which will not be elaborated here.

[0182] The computer-readable storage medium provided in the above embodiments of this application and the code modification method based on semantic update fragment generation provided in the embodiments of this application are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the applications stored therein.

[0183] It should be noted that:

[0184] Numerous specific details are set forth in the specification provided herein. However, it will be understood that embodiments of this application may be practiced without these specific details. In some instances, well-known structures and techniques have not been shown in detail so as not to obscure the understanding of this specification.

[0185] Similarly, it should be understood that, for the sake of brevity and to aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of this application, various features of this application are sometimes grouped together in a single embodiment, figure, or description thereof. However, this disclosure should not be construed as reflecting a schematic diagram in which the claimed application requires more features than expressly recited in each claim. Rather, as reflected in the following claims, inventive aspects lie in fewer than all features of a single foregoing disclosed embodiment. Therefore, the claims following the detailed description are hereby expressly incorporated into that detailed description, wherein each claim itself is a separate embodiment of this application.

[0186] Furthermore, those skilled in the art will understand that although some embodiments described herein include certain features but not others included in other embodiments, combinations of features from different embodiments are intended to be within the scope of this application and form different embodiments. For example, in the following claims, any of the claimed embodiments can be used in any combination.

[0187] The above description is merely a preferred embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art 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 modification method based on semantic update fragment generation, characterized in that, The method comprises: obtaining a plurality of triples of different programming languages; each triple comprises original code, modification description information and target code of a corresponding programming language; the target code is standard code obtained by modifying the original code based on the modification description information; the modification description information comprises modification content, modification position and modification reason; for any triple, generating a semantic update segment and a first refactoring code according to the original code and the modification description information in the triple; the first refactoring code is a code obtained by modifying the original code according to the modification description information by a first code modification model; the semantic update segment comprises a code modification segment, a modification anchor code segment and a semantic placeholder; the semantic placeholder is used to represent a code segment in the original code that is not modified; the modification anchor code segment is a context feature of a code modification position in the original code, and the code modification position is the position of the code modification segment in the original code; performing code modification verification on the semantic update segment according to the code difference between the original code and the first refactoring code; under the condition that the code modification verification is passed, inputting the original code and the semantic update segment into a plurality of second code modification models of different types respectively, so that each second code modification model modifies the original code according to the semantic update segment to generate a second refactoring code, and determines whether the code modification of the second code modification model is correct according to the code comparison result between the second refactoring code and the first refactoring code; calling a basic model to generate a third refactoring code based on the original code and the semantic update segment; determining whether the third refactoring code has a running error and a modification error; the running error comprises a syntax error or a semantic placeholder used improperly resulting in being unable to parse; the modification error comprises modifying a code region not specified in the original code; if the running error exists, determining a deduction of a first error penalty term of the basic model; if the modification error exists, determining a deduction of a second error penalty term of the basic model; when the third refactoring code is consistent with the first refactoring code, determining a full match reward score of the basic model; when the third refactoring code does not have a line-level error and there is no modification error, determining a bottom line reward score of the basic model; calculating a training score of the basic model according to the deduction of the first error penalty term, the deduction of the second error penalty term, the full match reward score and the bottom line reward score; if the training score is less than a preset score threshold, iteratively optimizing the basic model until the training score of the basic model is greater than or equal to the preset score threshold; if the training score is greater than or equal to the preset score threshold, determining the basic model as a target code modification model; modifying the code to be modified by the target code modification model to obtain a modified complete code.

2. The method of claim 1, wherein, The method further comprises: The base model is trained by a composite reward signal, the composite reward signal comprising: wherein, represents a composite reward signal, represents a full match reward, represents a first error penalty term, represents a second error penalty term, represents a floor reward, represents a first penalty coefficient corresponding to the first error penalty term, represents a second penalty coefficient corresponding to the second error penalty term.

3. The method of claim 1, wherein, After generating the semantic update fragment and the first reconstructed code according to the original code in the triple and the modification description information, the method further comprises: Randomization enhancement processing is performed on the generated partial semantic update fragment; the randomization enhancement processing comprises randomly replacing the semantic placeholder and / or fine-tuning the code indentation style to improve the generalization ability of the target code modification model to different semantic placeholder expression forms.

4. The method of claim 1, wherein, According to the code difference between the original code and the first reconstructed code, the semantic update fragment is code modification verified, comprising: The original code is compared with the first reconstructed code to obtain a difference code; The semantic similarity between the difference code and the semantic update fragment is calculated; If the semantic similarity is greater than a preset similarity threshold, it is determined that the code modification verification of the semantic update fragment is passed; If the semantic similarity is less than or equal to the preset similarity threshold, it is determined that the code modification verification of the semantic update fragment is not passed.

5. The method of claim 1, wherein, The target code modification model is used to modify the code to be modified to obtain the modified complete code, comprising: Receiving the code to be modified and the target modification description information input by the user end; According to the code to be modified and the target modification description information, a target semantic update fragment is generated; the target semantic update fragment comprises a target code modification fragment, a target modification anchor code fragment and a target semantic placeholder; the target semantic placeholder is used to represent the code fragment in the code to be modified that is not modified; the target modification anchor code fragment is the context feature code of the target code modification position in the code to be modified, and the target code modification position is the code position of the code modification fragment in the code to be modified; The code to be modified and the target semantic update fragment are input into the target code modification model, so that the target code modification model locates the target replacement position of the code modification fragment in the code to be modified according to the target modification anchor code fragment, and fuses the code modification fragment to the target replacement position to obtain the modified complete code.

6. The method of claim 1, wherein, Before obtaining a plurality of triples of different programming languages, the method further comprises: A plurality of original triples of different programming languages are obtained, each original triple comprising original code, original modification description information and target code of a corresponding programming language; wherein the original modification description information is original submission annotation information in a code version control system; the original submission annotation information comprises code function description information and memorandum property information, and the memorandum property information is information description without specific technical content; For any original triple in the plurality of original triples, the modification description information is generated according to the target difference code between the original code and the target code in the original triple and the original modification description information; wherein the modification description information comprises code function description information, and the code function description information comprises modification content, modification position and modification reason; The triplets of the corresponding programming languages are generated according to the original code, the modification description information and the target code.

7. A code modification apparatus based on semantic update fragment generation, characterized in that, The device comprises: A triplet obtaining module is configured to obtain a plurality of triplets of different programming languages; each triplet comprises original code, modification description information and target code of a corresponding programming language; the target code is standard code obtained by modifying the original code based on the modification description information; the modification description information comprises modification content, modification position and modification reason; A semantic update fragment generating module is configured to, for any triplet, generate a semantic update fragment and a first refactoring code according to the original code and the modification description information in the triplet; the first refactoring code is code obtained by modifying the original code according to the modification description information by a first code modification model; the semantic update fragment comprises a code modification fragment, a modification anchor code fragment and a semantic placeholder; the semantic placeholder is used to represent a code fragment in the original code that is not modified; the modification anchor code fragment is a context feature of a code modification position in the original code, and the code modification position is the position of the code modification fragment in the original code; A code modification checking module is configured to perform code modification checking on the semantic update fragment according to the code difference between the original code and the first refactoring code; A code modification judging module is configured to, under the condition that the code modification checking is passed, input the original code and the semantic update fragment into a plurality of second code modification models of different types respectively, so that each second code modification model modifies the original code according to the semantic update fragment to generate a second refactoring code, and determines whether the code modification of the second code modification model is correct according to the code comparison result between the second refactoring code and the first refactoring code. The model training module is configured to: call the base model to generate third reconstruction code based on the original code and the semantic update segment; determine whether the third reconstruction code has a running error and a modification error, the running error including a syntax error or a semantic placeholder being improperly used to cause an inability to parse, and the modification error including a modification on a code region that is not specified in the original code; determine a deduction of a first error penalty item of the base model if the running error exists; determine a deduction of a second error penalty item of the base model if the modification error exists; determine a complete matching reward score of the base model when the third reconstruction code is consistent with the first reconstruction code; determine a bottom-line reward score of the base model when the third reconstruction code does not have a line-level error and the modification error does not exist; calculate a training score of the base model according to the deduction of the first error penalty item, the deduction of the second error penalty item, the complete matching reward score, and the bottom-line reward score; perform iterative optimization on the base model if the training score is less than a preset score threshold, until the training score of the base model is greater than or equal to the preset score threshold; and determine the base model as a target code modification model if the training score is greater than or equal to the preset score threshold. The code modification module is configured to modify the to-be-modified code by using the target code modification model to obtain a modified complete code.

8. A computer device, comprising: The code modification method based on the semantic update segment is executed by a processor of a computer. The computer readable storage medium stores computer instructions, and the computer instructions are used to make the computer execute the code modification method based on the semantic update segment.

9. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer instructions, and the computer instructions are used to make the computer execute the code modification method based on the semantic update segment.

Citation Information

Patent Citations

  • Code generation method and device, computer program product and electronic equipment

    CN120803424A

  • Code repairing method and device based on large language model, equipment and medium

    CN120909640A