Context management methods and systems in code generation
By performing structured parsing and differential compression of C language source code and dialogue history, the problem of low code generation quality caused by context management mechanism is solved, achieving more efficient code generation and higher task success rate.
Patent Information
- Application Number
- CN202611601332.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-15
- Publication Date
- 2026-11-17
AI Technical Summary
When the context space is limited, the defects in the context management mechanism in the existing technology lead to low code generation quality, low task success rate, and compilation errors and logical errors.
By performing structured parsing of the context containing C language source code and dialogue history, structured and unstructured elements are identified and classified. Priority protection and differential compression techniques are used to retain structured elements referenced in the current active rounds and to differentially process elements not protected by priority, thereby generating an optimized context.
It improves the success rate of code generation, reduces the possibility of compilation and logical errors, improves the efficiency of context space utilization, and can accommodate more rounds of interactive information.
Smart Images

Figure CN123114445A_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the interdisciplinary field of artificial intelligence and software engineering, and in particular relates to a context management method and system for code generation. Background Technology
[0002] With the widespread application of Large Language Models (LLMs) in tasks such as code generation and programming assistance, utilizing Artificial Intelligence Agents (Agents) for autonomous multi-round code interaction and modification has become a development paradigm. In software development scenarios such as embedded systems, the Agent needs to continuously understand the project structure, edit code files, and invoke compilation and testing tools through multiple rounds of interaction to complete development tasks.
[0003] When the context space is limited, existing context management technologies use a uniform truncation or compression method for code elements and dialogue content in the context, resulting in missing declarations, definitions or configuration information required for code generation, and compilation errors in the code generated by the Agent.
[0004] When the context space is limited, existing context management technologies use the same compression method for code elements and dialogue content in the context, resulting in incomplete code dependency and configuration constraint information required for code generation, and logical errors in the code generated by the Agent. Summary of the Invention
[0005] This application provides a context management method and system for code generation, aiming to solve the technical problems of low code generation quality and low task success rate caused by defects in the context management mechanism in the prior art.
[0006] In a first aspect, embodiments of this application provide a context management method for code generation, the method comprising: Perform structured parsing of a context containing C language source code and dialogue history, and identify and classify the structured and unstructured elements in the context; When the compression triggering condition is met, the structural element is given priority protection based on the dependency reference relationship between the current active round and the historical structural element, so that the structural element referenced by the current active round is completely preserved. Differentiated compression is applied to structural and unstructured elements that are not protected by priority. Prioritized structural elements, differentially compressed structural elements, and differentially compressed unstructured elements are recombined to generate an optimized context for use by a large language model.
[0007] In one possible implementation of the first aspect, the structural elements include header file dependency chains, compile-time symbol definitions, register mappings, static configuration tables, and function call topologies; when compressing the function call topology, at least two layers of adjacent call relationships involving the direct caller and the direct callee involved in the current active round modification point are preserved; The unstructured elements include natural language descriptions, agent reasoning processes, tool execution results, and compilation error outputs.
[0008] In one possible implementation of the first aspect, the step of prioritizing the structural elements based on the dependency relationship between the current active round and historical structural elements, so that the structural elements referenced by the current active round are fully preserved, includes: When the currently active round explicitly or implicitly references header files or function definitions from historical rounds, compression or downgrading of the header files or function definitions is prohibited through a dependency transitive locking mechanism. The compile-time symbol definitions retain complete key-value pairs or complete declaration relationships, and are sorted according to the frequency of use of the compile-time symbol definitions in historical commands, so as to retain compile-time symbol definitions whose usage frequency reaches a preset frequency threshold and discard compile-time symbol definitions whose usage frequency does not reach the preset frequency threshold.
[0009] In one possible implementation of the first aspect, the differential compression of structured and unstructured elements not protected by priority includes: A summary of the source code in the context is generated while preserving C language declaration-level information, and differential compression is performed on the static configuration table in the structural element.
[0010] In one possible implementation of the first aspect, the C language declaration-level information includes function signatures, type definitions, and constant values after macro expansion; The abstracts are classified into L1 level abstracts, L2 level abstracts, and L3 level abstracts according to their granularity. The L1 level summary is a file-level overview, retaining the file name and file function description; The L2 level summary is module-level information, retaining the function list and the global variable list; The L3 level summary is statement-level information, retaining conditional statements, assignment statements, and function call statements.
[0011] In one possible implementation of the first aspect, performing differential compression on the static configuration table in the structural element includes: The baseline version of the static configuration table is stored in the form of a folded digest, which retains the table header, total number of rows, index start value, and index end value. The modified difference rows in the static configuration table are preserved in their complete form, along with comments on the original values.
[0012] In one possible implementation of the first aspect, the method further includes: Differential compression is triggered when the token usage of a single static configuration table in the context exceeds a preset ratio threshold.
[0013] In one possible implementation of the first aspect, the method further includes: Before performing the differential compression, the integrity baseline Sbaseline and the current instantaneous structural score Scurrent of the structural element are calculated. Calculate the difference between the integrity baseline Sbaseline and the current instantaneous structural score Scurrent, and calculate the ratio of the difference to the integrity baseline Sbaseline; If the ratio is greater than a preset loss threshold, the compression of the structured elements is abandoned, only the unstructured elements are released, and external vector retrieval or disk cache replacement is triggered to replace the historical round records.
[0014] In one possible implementation of the first aspect, the compression triggering condition includes the context's token usage rate reaching a preset usage rate threshold; The differentiated compression implements a three-level compression strategy based on the token usage rate: When the token usage rate reaches a preset first usage rate threshold but does not reach a preset second usage rate threshold, L1 light compression is performed, and the unstructured elements are truncated in chronological order starting from the earliest dialogue round. When the token usage rate reaches a preset second usage rate threshold but does not exceed a preset third usage rate threshold, L2 medium compression is performed to generate an L2 level summary for the historical rounds. When the token usage rate exceeds a preset third usage rate threshold, L3 deep compression is performed, structural elements from historical rounds are downgraded and retained, and L1 level digests are generated for unstructured elements. The complete interaction corresponding to the current active round and the complete interaction corresponding to the previous round are not affected by the differential compression; the complete interaction includes user questions, agent responses, and tool execution results.
[0015] Secondly, embodiments of this application provide a context management system for code generation, including: The context parsing module is used to perform structured parsing of the context containing C language source code and dialogue history, and to identify and classify the structured and unstructured elements in the context. The priority protection module is used to protect the structural elements based on the dependency relationship between the current active round and the historical structural elements when the compression triggering condition is met, so that the structural elements referenced by the current active round are completely preserved. The differential compression module is used to perform differential compression on structured and unstructured elements that are not protected by priority. The context reorganization module is used to reorganize priority-protected structural elements, differentially compressed structural elements, and differentially compressed unstructured elements to generate an optimized context for use by large language models.
[0016] Thirdly, embodiments of this application provide a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the context management method in code generation as described in any of the first aspects above.
[0017] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program that, when executed by a processor, implements the context management method in code generation as described in any one of the first aspects.
[0018] Fifthly, embodiments of this application provide a computer program product that, when run on a computer device, causes the computer device to execute the context management method in code generation as described in any of the first aspects above.
[0019] This application embodiment performs structured parsing of the context containing C language source code and dialogue history, identifying and classifying structured and unstructured elements. This allows for the differentiation of different types of elements when the context space is limited, avoiding the loss of declarations, definitions, or configuration information required for code generation due to uniform truncation or compression, thereby reducing the possibility of compilation errors in the agent-generated code. When the compression trigger condition is met, structured elements are prioritized based on the dependency reference relationship between the current active round and historical structured elements, ensuring that structured elements referenced by the current active round are fully preserved. This helps maintain the integrity of code dependency relationships and configuration constraint information, reducing logical errors in the agent-generated code. Differentiated compression is performed on structured and unstructured elements that are not prioritized, and the prioritized structured elements, differentiated compressed structured elements, and differentiated compressed unstructured elements are recombined to generate an optimized context for use by a large language model. This improves the utilization efficiency of the context space, allowing the limited context window to accommodate more rounds of interaction information, thereby increasing the success rate of code generation.
[0020] It is understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant descriptions in the first aspect mentioned above, and will not be repeated here. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of this application, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 A schematic flowchart illustrating the context management method in code generation provided in this application embodiment; Figure 2 A schematic flowchart illustrating a context management method in code generation provided in another embodiment of this application; Figure 3 This is a schematic diagram illustrating the operation of the priority protection module provided in an embodiment of this application. Figure 4 A schematic diagram illustrating the abstract generation process in differentiated compression provided in this application embodiment; Figure 5 A schematic diagram of the differential compression process of the configuration table provided in the embodiments of this application; Figure 6 This is a schematic diagram of the structure of the computer device provided in the embodiments of this application. Detailed Implementation
[0023] In the following description, specific details such as particular system architectures and techniques are set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this application with unnecessary detail.
[0024] It should be understood that, when used in this application specification and the appended claims, the term "comprising" indicates the presence of the described features, integrals, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or a collection thereof.
[0025] It should also be understood that the term “and / or” as used in this application specification and the appended claims means any combination of one or more of the associated listed items and all possible combinations, and includes such combinations.
[0026] As used in this application specification and the appended claims, the term "if" may be interpreted, depending on the context, as "when," "once," "in response to determination," or "in response to detection." Similarly, the phrase "if determined" or "if detected [the described condition or event]" may be interpreted, depending on the context, as meaning "once determined," "in response to determination," "once detected [the described condition or event]," or "in response to detection [the described condition or event]."
[0027] Furthermore, in the description of this application and the appended claims, the terms "first," "second," "third," etc., are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.
[0028] References to "one embodiment" or "some embodiments" as described in this specification mean that one or more embodiments of this application include a specific feature, structure, or characteristic described in connection with that embodiment. Therefore, the phrases "in one embodiment," "in some embodiments," "in other embodiments," "in still other embodiments," etc., appearing in different parts of this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.
[0029] Figure 1 A schematic flowchart of a method provided in an embodiment of this application is shown.
[0030] S101, perform structured parsing of the context containing C language source code and dialogue history, and identify and classify the structured and unstructured elements in the context.
[0031] Structural elements refer to components in C language source code and dialogue history that have a clear syntactic structure and procedural meaning. Their completeness and accuracy affect whether the code can be compiled and run correctly.
[0032] Unstructured elements refer to parts that primarily carry semantic information rather than strict syntactic structure in the context. Their format is relatively free, but they are useful for understanding task intent and the debugging process.
[0033] In this embodiment of the application, the original, mixed text information stream is transformed into structured, machine-understandable data by performing structured parsing of the context, thereby avoiding the simple truncation mode that treats all text equally in traditional methods, and providing a foundation for subsequent priority protection and differentiated compression.
[0034] In one optional embodiment, the structural elements include header file dependency chains, compile-time symbol definitions, register mappings, static configuration tables, and function call topologies; when compressing the function call topology, at least the two layers of adjacent call relationships involving the direct caller and the direct callee involved in the current active round modification point are preserved.
[0035] The header file dependency chain refers to the dependency chain that represents the reference relationship between source code files, constructed by resolving reference instructions.
[0036] Among them, compile-time symbol definitions refer to symbol definitions in the C language whose values or types are determined at compile time, including macro definitions and enumeration definitions.
[0037] Register mapping refers to the correspondence between addresses and registers used to access hardware registers in embedded C projects, which exists in the form of mutable pointers or address macros.
[0038] In this context, the static configuration table refers to a constant table in an embedded C project that exists in the form of a large static array or a structure array and is used to describe the relationships between input / output mappings, interrupt vectors, register configurations, etc.
[0039] Among them, function call topology refers to the relationship structure that represents the function call path and call level, constructed by analyzing the call relationships between functions.
[0040] The current active round refers to the interaction round consisting of the user's question being asked and its subsequent Agent response, tool call and tool execution result.
[0041] In this embodiment, by preserving the two-layer adjacency call relationship, the compressed model can still understand the position of the current modification point in the local control flow, thus avoiding the generation of logically incorrect call order.
[0042] In one optional embodiment, the unstructured elements include natural language descriptions, agent reasoning processes, tool execution results, and compilation error outputs.
[0043] Natural language description refers to natural language instructions or explanations exchanged between the user and the agent.
[0044] The Agent reasoning process refers to the reasoning text generated by the Agent during code generation or task execution.
[0045] The tool execution result refers to the execution result log generated by calling external tools, such as text search results and compilation output.
[0046] Among them, compiler error output refers to the error or warning information output by the compiler during the compilation process.
[0047] S102, when the compression triggering condition is met, the structural element is given priority protection based on the dependency reference relationship between the current active round and the historical structural element, so that the structural element referenced by the current active round is completely preserved.
[0048] Among them, the compression trigger condition refers to the condition used to determine whether to enter the compression evaluation process.
[0049] In this context, dependency refer to explicit or implicit references between code or interaction content in the current active round and historical structural elements.
[0050] Priority protection refers to a method of assigning higher retention priority to structural elements referenced in the current active round, so that they are fully preserved during the compression process.
[0051] In one optional embodiment, when prioritizing structured elements, the integrity of header file dependency chains is preserved first, and header file dependencies are not truncated even if the context reaches the truncation threshold; the integrity of structured elements takes precedence over that of non-structured elements; and structured elements referenced by the current active round are not truncated below the truncation threshold.
[0052] In this embodiment, by analyzing the dependency reference relationship between the currently running task and the structural elements in the historical context, the behavior of human programmers focusing on relevant functions and definitions during coding is simulated, thereby avoiding compilation errors caused by lost dependencies.
[0053] In an optional embodiment, S102 prioritizes the structural elements based on the dependency relationship between the current active round and historical structural elements, ensuring that the structural elements referenced by the current active round are fully preserved, including: Step a1: When the currently active round explicitly or implicitly references a header file or function definition from a historical round, the compression or downgrading of the header file or function definition is prohibited by the dependency transitive locking mechanism.
[0054] Among them, the dependency transitive locking mechanism refers to a protection strategy that automatically marks the referenced element and all its upstream dependent elements as incompressible and non-degradable when the system detects that the interaction content of the current active round explicitly or implicitly references a structural element that already exists in the historical round, thereby forcing it to be completely preserved in the context.
[0055] In this embodiment of the application, the dependency transitive locking mechanism ensures that all code blocks that are directly or indirectly dependent on by the current work remain in their original and complete state.
[0056] Step a2: The compile-time symbol definitions retain complete key-value pairs or complete declaration relationships, and are sorted according to the usage frequency of the compile-time symbol definitions in historical commands, so as to retain compile-time symbol definitions whose usage frequency reaches a preset frequency threshold and discard compile-time symbol definitions whose usage frequency does not reach the preset frequency threshold.
[0057] Among them, the preset frequency threshold refers to the threshold used to determine whether a compiler-time symbol definition belongs to high-frequency use and therefore needs to be retained.
[0058] In this embodiment of the application, by statistically analyzing the usage frequency of each symbol in historical commands, the complete key-value pairs or declaration relationships of high-frequency symbols are retained, while low-frequency symbols are discarded, thereby preserving the definitions of symbols with higher value within a limited space.
[0059] In one optional embodiment, when compressing structural elements that are not protected by priority, the retention rate is not less than 70%, and elements in inactive chains are deleted preferentially.
[0060] In an optional embodiment, the method further includes: before performing the differential compression, calculating the integrity baseline Sbaseline and the current instantaneous structure score Scurrent of the structured element; calculating the difference between the integrity baseline Sbaseline and the current instantaneous structure score Scurrent, and calculating the ratio of the difference to the integrity baseline Sbaseline; if the ratio is greater than a preset loss threshold, then abandoning the compression of the structured element, releasing only the unstructured element, and triggering external vector retrieval or disk cache replacement to replace historical round records.
[0061] Among them, the integrity baseline Sbaseline refers to the initial total score obtained by the system after a comprehensive weighted evaluation of all structural elements in the current context at the beginning of each round of interaction and before any compression action occurs.
[0062] The current immediate structural score Scurrent refers to the total score calculated immediately for the structural elements in the current context before compression is performed, according to the same weighted evaluation rules as the integrity baseline Sbaseline.
[0063] The preset loss threshold is a threshold used to determine whether the compression will cause excessive loss of structural information.
[0064] Among them, external vector retrieval or disk cache replacement refers to the processing method of supplementing or replacing historical round records through external vector retrieval or disk caching when the compression of structural elements is abandoned.
[0065] In this embodiment, the integrity baseline Sbaseline is calculated as follows: at the start of each round of Agent interaction, before any compression action occurs, all structured elements in the current context are weighted and summed. The individual weighting rules include: the more enumerations and macro definitions, the higher the weight; when the number of configuration table rows is greater than 100, the weight is doubled; the more nodes in the function call topology, the higher the weight; the higher the completeness of the header file dependency chain, the higher the weight. The integrity baseline Sbaseline is obtained by multiplying the individual weight of each structured element by its quantity or size and then summing the results. The current real-time structured score Scurrent is calculated immediately before compression execution according to the same weighting evaluation rules as Sbaseline.
[0066] In one optional embodiment, the compression triggering condition adopts a logical OR mechanism, that is, the compression evaluation process is initiated when either of the following conditions is met: Condition A, the usage rate of context tokens reaches a preset threshold; Condition B, the usage of a single configuration table token exceeds a preset proportion threshold of the total budget, which is triggered independently by the configuration table differential compression module.
[0067] The preset threshold refers to the threshold used to determine whether the usage rate of context tokens is too high.
[0068] The preset ratio threshold is a threshold used to determine whether the token usage of a single configuration table is too large.
[0069] In this embodiment of the application, by triggering a condition with two conditions, the situation is avoided where there is still spare global space, but a single large table has already occupied a lot of space for other valuable information.
[0070] S103 performs differentiated compression on structured and unstructured elements that are not protected by priority.
[0071] Differentiated compression refers to using different compression algorithms based on the element type, rather than simply deleting or truncating, in order to minimize token usage while preserving the recoverability and understandability of information to the greatest extent possible.
[0072] In this embodiment of the application, differentiated compression is used to solve the problems of coarse summary content and loss of key technical details in the prior art, thereby achieving effective utilization of context space.
[0073] In one optional embodiment, the differential compression of structured and unstructured elements not protected by priority includes: Step b1: Generate a summary of the source code in the context that preserves C language declaration-level information.
[0074] In C language, declaration-level information includes function signatures, type definitions, and constant values after macro expansion.
[0075] In one optional embodiment, the summary is divided into L1-level summary, L2-level summary, and L3-level summary according to granularity; the L1-level summary is a file-level overview, retaining the file name and file function description; the L2-level summary is module-level information, retaining the function list and global variable list; the L3-level summary is statement-level information, retaining conditional statements, assignment statements, and function call statements.
[0076] In one optional embodiment, the L1-level summary also retains file content statistics, such as the number of enumerations, the number of macro definitions, the number of global variables, and the number of functions.
[0077] Here, the abstract anchor refers to the structured location information attached to the abstract result, including file path, line number, and declaration type.
[0078] In this embodiment, C language domain constraints are injected when the Large Language Model (LLM) is used to generate the summary, forcibly requiring the preservation of C language declaration relationships, including: complete preservation of function signatures, with parameter names and types not omitted; preservation of type definitions, including complete preservation of type definitions, structures, and unions; and preservation of macro expansion results, retaining the expanded constant values. The summary result includes structured anchors, such as [Const_IO.c:45-48, enum: EN_KI_FDL3] or [driver.c:13, func]. When the Agent encounters an anchor during inference and determines that it needs to obtain the complete original definition of the anchor to continue generating correct code, it can call a tool to read the content corresponding to the anchor, temporarily inject it into the active area of the current context window, immediately following the user's latest question, and then resume the generation of the Large Language Model.
[0079] In one optional embodiment, all levels of the digest retain declaration-level precision, that is, the L1-level digest, L2-level digest, and L3-level digest are all generated according to C language domain constraints, retaining function signatures, type definitions, and constant values after macro expansion.
[0080] Step b2: Perform differential compression on the static configuration table in the structural element.
[0081] In one optional embodiment, step b2 specifically includes: Step b21: Store the baseline version of the static configuration table in the form of a folded summary, wherein the folded summary retains the table header, total number of rows, index start value, and index end value.
[0082] Among them, folded summary refers to a deep compression form specifically for large static configuration tables. It does not retain all row-by-row content of the table, but extracts and retains core metadata, such as the table's C language definition header, the total number of rows in the table, and key index start and end values.
[0083] Step b22: The modified difference rows in the static configuration table are preserved in their complete form, along with comments on the original values.
[0084] Here, the difference row refers to the configuration table row that has been modified by the Agent during the interaction.
[0085] In one optional embodiment, when performing differential compression on the static configuration table, configuration rows related to the current active round are also retained based on the configuration items involved in the current active round.
[0086] In this embodiment, the baseline version of the static configuration table is stored in a collapsed digest format, which retains the table header, total number of rows, index start value, and index end value. Modified rows of the static configuration table are retained in their complete form, along with comments indicating the original values. For example, in Const_SpfRec.c, after differential compression of the 1452-row configuration table, only the actual changed content needs to be retained; unchanged rows are collapsed into a baseline digest, and modified rows are retained in their entirety with comments indicating the original values.
[0087] In an optional embodiment, the method further includes: Differential compression is triggered when the token usage of a single static configuration table in the context exceeds a preset ratio threshold.
[0088] The preset ratio threshold refers to the token occupancy ratio threshold used to trigger differential compression of a single configuration table.
[0089] In this embodiment of the application, when the token usage of a single configuration table in the current context window exceeds 15% of the total budget, differential compression is triggered immediately without waiting for the global 80% threshold, enabling configuration table compression to have proactive processing capabilities.
[0090] In one optional embodiment, the compression triggering condition includes the context's token usage rate reaching a preset usage rate threshold; The differentiated compression employs a three-level compression strategy based on the token usage rate: when the token usage rate reaches a preset first usage rate threshold but does not reach a preset second usage rate threshold, L1 light compression is performed, truncating unstructured elements chronologically from the earliest dialogue round; when the token usage rate reaches a preset second usage rate threshold but does not exceed a preset third usage rate threshold, L2 medium compression is performed, generating an L2-level summary for historical rounds; when the token usage rate exceeds a preset third usage rate threshold, L3 deep compression is performed, downgrading and preserving structured elements from historical rounds, and generating an L1-level summary for unstructured elements; the complete interaction corresponding to the current active round and the complete interaction corresponding to the previous round are not affected by the differentiated compression; the complete interaction includes user questions, Agent responses, and tool execution results.
[0091] Among them, the first usage rate threshold, the second usage rate threshold, and the third usage rate threshold are token usage rate thresholds that increase sequentially.
[0092] Among them, L1 light compression, L2 medium compression, and L3 deep compression are three-level compression strategies with progressively increasing compression levels.
[0093] Among them, downgrade preservation refers to retaining only the core declaration information of the structural elements of the historical rounds during L3 deep compression, such as only retaining the function signature.
[0094] It should be noted that, Figure 2 The ≥70% structural reduction mentioned in L3 deep compression refers to the following: when the token usage rate exceeds the third usage rate threshold, structural elements in earlier rounds (excluding the current active round and the round before it) are downgraded and retained, reducing their token usage by more than 70% compared to before the downgrade. In other words, the retained token amount after compression does not exceed 30% of the original amount. This 70% is the token compression rate or downgrade compression magnitude of the structural element, not the loss rate of the structural integrity score. The loss of the structural integrity score is still controlled by the baseline verification before compression. That is, if (Sbaseline - Scurrent) / Sbaseline is greater than the preset loss threshold, the compression of structural elements is abandoned, and only non-structural elements are released. The complete interactions of the current active round and the round before it are not subject to this 70% reduction and are always fully retained.
[0095] In this embodiment, for compilation error output, the error type, file path, and line number are extracted using regular expressions. The extracted results are marked with high retention priority and are retained over ordinary dialogue logs in subsequent compression to avoid the misinterpretation of critical debugging clues. This extraction method is compatible with various embedded compiler output formats, or can be adapted to different compiler output formats through configurable templates.
[0096] S104 reorganizes the priority-protected structural elements, the differentially compressed structural elements, and the differentially compressed unstructured elements to generate an optimized context for use by the large language model.
[0097] Among them, optimized context refers to input context with high information density, clear structure, and prominent key points, without exceeding the model context length limit.
[0098] In this embodiment of the application, the reorganization process takes into account logical correlation and temporal order, reorganizes all processed context fragments, and finally provides them to the large language model as the basis for generating the next step of code or response.
[0099] In one optional embodiment, S104 specifically includes: Step c1: Combine the processing results of modules 2, 3, and 4 with the system prompt message. Module 2 is the structure preservation module, module 3 is the domain-aware summarization module, and module 4 is the configuration table differential compression module. The system prompt message remains unchanged during the combination process.
[0100] Among them, system prompts refer to the system prompts provided to the large language model.
[0101] In step c2, module 5 finally assembles and generates the optimized context, which is then output and sent to the large language model.
[0102] Module 5, the final assembly, refers to the processing module that splices and reassembles structured elements that have been prioritized, structured elements that have been differentially compressed, and unstructured elements that have been differentially compressed.
[0103] In one implementation scenario, the complete collaborative workflow of this application's method is demonstrated, covering the entire process of context structured resolution, dependency reference-based priority protection, differential compression, and context reorganization. Please refer to [link / reference]. Figure 2 The diagram illustrates the overall flow of the method. At the start of the process, the Agent receives a code modification task, whose context includes the user's initial instructions and relevant C source code files (e.g., a Const_SpfRec.c file containing a large static configuration table and a business logic file driver.c). First, the system performs a structured parsing of this context, identifying header file dependency chains, compile-time symbol definitions (such as macro definitions), and function call topologies (such as the definitions and call relationships of funcA and funcB) in driver.c, and classifying them as structured elements. Simultaneously, it identifies a 1452-line static array in Const_SpfRec.c as a static configuration table and classifies user instructions from the dialogue history as unstructured elements.
[0104] After multiple rounds of dialogue and code modifications, the Agent references funcA in funcB, establishing a dependency relationship between the current active round and the historical structural element funcA. At this point, the token utilization rate of the entire context increases to 82%, reaching the preset 80% utilization threshold and satisfying the compression trigger condition. Before performing differential compression, the system performs baseline verification. For example, assuming the integrity baseline Sbaseline before compression is 100 points, the system estimates that this compression operation will cause the score to drop to 90 points, i.e., the current immediate structural score Scurrent is 90. In this case, the loss ratio is (100-90) / 100=10%, which is less than the preset loss threshold (e.g., 20%), so the baseline verification passes, and the compression process continues. After the verification passes, the system initiates priority protection. Based on the identified dependency relationship, the system protects the definition of funcA referenced by funcB through a dependency transitive locking mechanism, ensuring its integrity and preventing compression. For structural elements not protected by priority (such as a function funcC that has never been referenced) and unstructured elements (such as dialogue history from earlier rounds), the system performs differential compression, for example, generating an L2-level summary for funcC. Finally, the system reassembles the priority-protected definition of funcA, the differentially compressed summary of funcC, and the dialogue history summary to generate an optimized context for use by the large language model.
[0105] Please continue reading. Figure 3The diagram illustrates the priority protection process described above. The diagram compares the contexts before and after compression to explain the protection strategy. The context before compression includes the header file `#include "driver.h"` referenced by the currently active round, as well as several compile-time symbol definitions, such as `#define ERR_NONE 0` (used 12 times historically), `#define ERR_TIMEOUT 1` (used 8 times historically), and `#define ERR_CRC 2` (used once historically). It also includes `funcA`, which is called by `funcB`. When compression is triggered, the system executes a combined protection strategy: First, since `funcA` is called by `funcB` in the currently active round, the system uses a dependency transitive locking mechanism to lock and fully preserve its call in the compressed context. Simultaneously, the actively referenced header file `#include "driver.h"` is also determined to require complete preservation of its dependency chain. Second, the compile-time symbol definitions are sorted according to their historical usage frequency. The `#define ERR_NONE 0` and `#define ERR_TIMEOUT 1` are determined to be high-frequency reserved due to their frequent use, while `#define ERR_CRC2` is discarded due to its low frequency of use. Elements not protected by any mechanism, such as the unreferenced `#include "legacy.h"` and the irrelevant function `funcC`, are compressed normally, for example, downgraded to an L2 digest. Through this multi-strategy collaboration, the protection of critical structural information is achieved.
[0106] In another implementation, this application employs a summary method that preserves C language declaration-level information for compressing historical context. In a multi-round debugging task, early contexts contain the complete contents of the file driver.c. As interaction progresses, the context reaches a compression threshold, and the system needs to perform differential compression on the contents of driver.c. See also... Figure 4This diagram illustrates the summary generation process. As shown, a C source code (driver.c) can generate summaries at three different granularities using this method. For example, the L1 level summary is a file-level overview, possibly containing only the input / output driver module of driver.c and file content statistics. The L2 level summary provides module-level information, retaining key C language declaration-level information, such as the type definition `enum ErrorCode {...}`, the macro-expanded constant value `#define MAX_RETRY 3`, and the function signature `ErrorCode set_io_config(u32, u8)`. The L3 level summary provides statement-level information, further retaining key execution logic, such as `if (reg&0x80) return ERR_TIMEOUT;`. Each summary item is appended with a structured anchor point, such as `[driver.c:13, func]`. In a subsequent interaction, the Agent may need to modify the code calling the `set_io_config` function, but the internal implementation logic cannot be determined solely from the function signature in the L2 level summary. At this point, the Agent can identify the anchor point [driver.c:13, func] in the digest and trigger an on-demand reverse lookup mechanism. Its internal processing might be as follows: The function signature alone is insufficient to determine whether modifications will introduce side effects; the complete implementation needs to be examined. Therefore, the Agent calls a tool to read the complete source code of the `set_io_config` function from external storage using the anchor point information and temporarily injects it into the active region of the current context for the large language model to reference in generating the correct modified code. This mechanism solves the problem of information loss and irreversibility in traditional digest methods, allowing the Agent to still obtain necessary original code details on demand, even when the context is compressed.
[0107] Furthermore, this application provides a specialized differential compression method for large static configuration tables commonly found in embedded C projects. In one implementation scenario, an agent is required to modify the pin function configuration in an embedded project. This requires modifying a large static array named `ConfigEntry_t spf_tbl`, defined in the file `Const_SpfRec.c`, totaling 1452 lines. When the agent loads this file, the system detects that the token usage of this static configuration table in the context is 18%, exceeding the preset independent compression triggering threshold (e.g., 15%). Therefore, the system immediately triggers differential compression for this table without waiting for the global token usage rate to reach the global threshold of 80%. Please refer to [link to relevant documentation]. Figure 5The diagram illustrates this differential compression process in detail. The system treats the complete 1452 rows of the configuration table as the baseline version. Suppose that the Agent, based on user instructions, modifies the content of row 1450 from {0x05AB, FAST, 0xFF, "DBG"} to {0x05AB, NORMAL, 0xF0, "RST"}. The differential compression module calculates this modification difference. After compression, most of the unmodified rows are replaced by a baseline folded digest, which retains the table header / / table header: static const ConfigEntry_t, total number of rows / / total number of rows: 1452, and index range / / index range: [0]...
[1451] . Only the modified row 1450 is completely preserved as the difference row, with the original value comment / / original value: {0x05AB, FAST, 0xFF, "DBG"}. In this way, the token usage of the configuration table is reduced from 18% to about 3%, saving context space while preserving the modification information.
[0108] In one optional implementation, the compression strategy of this application is dynamically correlated with the context token usage rate, forming a three-level compression strategy. In a long-term continuous interaction scenario, context content accumulates continuously. When the context token usage rate first reaches a preset first usage rate threshold, such as 83% (located at...), the compression strategy is further optimized. Figure 2 When the context token usage reaches the 80% to 85% range shown, the system performs L1 light compression. This operation truncates unstructured elements, such as historical reasoning text and old tool output logs, starting from the earliest dialogue turn in chronological order. As the interaction continues, context token usage further increases, reaching a preset second usage threshold, such as 90% (located in the range shown). Figure 2 (As shown in the 85% to 95% range), at which point the system performs moderate L2 compression. This operation generates an L2-level summary of the source code and dialogue history for historical rounds (excluding the current active round and its previous round). In a complex debugging task, the context is filled with a large amount of information, and the utilization rate exceeds the preset third utilization rate threshold, for example, reaching 96% (greater than). Figure 2 If the compression reaches 95% (as shown), the system performs L3 deep compression. At this point, the system performs downgraded preservation of structural elements from previous rounds (e.g., function definitions are compressed to retain only function signatures), while generating a coarser L1 summary for unstructured elements. Throughout all levels of compression, a protection rule remains in effect: the complete interaction corresponding to the current active round and the complete interaction corresponding to the previous round (including user questions, agent responses, and tool execution results) are always fully preserved, unaffected by any differential compression. This design ensures that the direct context of the agent's work is always complete and clear, achieving management of the context space.
[0109] Below is a complex application scenario combining multiple strategies. Suppose an agent is assigned a complex refactoring task: in a large embedded project, the main control logic needs to be modified, and simultaneously a large configuration table defined in a separate file needs to be adjusted. At the start of the task, the agent loads the main.c file containing the main control logic and the file containing the large configuration table Const_SpfRec.c. The system immediately detects that the configuration table in Const_SpfRec.c is using more than 15% of the context tokens, and the independent compression trigger mechanism is activated. The configuration table is differentially compressed, and its form in the context becomes a short, folded summary and a few modified difference lines. This saves context space for subsequent interactions.
[0110] Subsequently, the Agent interacted with the user in multiple rounds, continuously modifying the code in main.c. In one round, the Agent needed to call a key initialization function, init_device(), defined earlier in main.c. This call established a clear dependency reference. As the interaction continued, due to the history of multiple rounds of dialogue, tool output, and code modifications, the token utilization rate of the global context rose to 96%, exceeding the third utilization threshold and triggering L3 deep compression. At this point, the multiple protection mechanisms of this application began to work together: First, the priority protection module, based on the dependency transitive locking mechanism, identified that the init_device() function was depended upon by the currently active round, and therefore marked it as uncompressible, preserving its complete function body. Second, the differential compression module began processing other historical information. For the folded digest of Const_SpfRec.c, since it is itself an important structural element and its size is already small, the system chooses to preserve it completely. For other historical structural elements unrelated to the current task (such as other uncalled functions), they are downgraded to L3 digests (only the function signatures are preserved). For earlier unstructured elements (such as dialogues from several rounds ago), they are compressed into L1-level summaries (retaining only an overview). Of course, the complete interactions from the last two rounds are fully preserved. Finally, the context reorganization module combines these fragments of different states (the locked `init_device()`, the differentially compressed configuration table summary, summaries at various levels, and the most recent complete interactions) into an optimized context. This final context satisfies the model's length constraints while retaining all information important to the current task, enabling the agent to perform complex reorganization tasks.
[0111] To verify the technical effectiveness of this method, the applicant conducted experimental tests. The experiments used a lightweight language model as the base model for the Agent, and the test tasks included three categories: code review, code debugging, and code generation, totaling 100 tasks. After adopting this method, the task pass rate was 75%; the pass rate using the baseline scheme (equal truncation and general digest) was 60%. It can be seen that the pass rate of this method is improved by 15 percentage points, a relative improvement of approximately 25%. The above experimental data is only used to illustrate the technical effectiveness of this method and does not constitute a limitation on the scope of protection of this application.
[0112] The context management method provided in this application has broad application prospects. It can be integrated into various AI development tools based on large language models, such as integrated development environment plugins for AI-assisted programming, automated code debugging and repair systems, and migration tools for modernizing legacy code. Especially in the field of embedded software development, such as automotive electronics, industrial control, IoT devices, and the power industry, where project code structures are complex, hardware constraints are strict, and configuration parameters are numerous, this application can improve the success rate and efficiency of agents performing code generation, review, and maintenance tasks in these scenarios. By using this method as a context management engine, more reliable software development automation solutions can be built, thereby accelerating development cycles, reducing labor costs, and improving the quality of the final software product. This method can be integrated as a context management module into existing code agent frameworks. By replacing the original token truncation and digest strategies, it achieves context management for embedded C projects, improving the agent's task execution performance under limited context.
[0113] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.
[0114] Corresponding to the context management method in code generation described in the above embodiments, this application also provides a context management system in code generation. For ease of explanation, only the parts related to this application are shown.
[0115] The context management system used in this code generation includes: The context parsing module is used to perform structured parsing of the context containing C language source code and dialogue history, and to identify and classify the structured and unstructured elements in the context. The priority protection module is used to protect the structural elements based on the dependency relationship between the current active round and the historical structural elements when the compression triggering condition is met, so that the structural elements referenced by the current active round are completely preserved. The differential compression module is used to perform differential compression on structured and unstructured elements that are not protected by priority. The context reorganization module is used to reorganize priority-protected structural elements, differentially compressed structural elements, and differentially compressed unstructured elements to generate an optimized context for use by large language models.
[0116] It should be noted that the information interaction and execution process between the above modules or components are based on the same concept as the method embodiments of this application. For details on their specific functions and technical effects, please refer to the method embodiments section, which will not be repeated here.
[0117] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is merely an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the system can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiments can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit. Furthermore, the specific names of the functional units and modules are only for easy differentiation and are not intended to limit the scope of protection of this application. The specific working process of the units and modules in the above system can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0118] This application also provides a computer device, which includes: at least one processor, a memory, and a computer program stored in the memory and executable on the at least one processor. When the processor executes the computer program, it implements the steps in any of the above method embodiments.
[0119] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in the above-described method embodiments.
[0120] This application provides a computer program product that, when run on a computer device, enables the computer device to execute the steps described in the various method embodiments above.
[0121] Figure 6 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. Figure 6 As shown, the computer device of this embodiment includes: at least one processor 60 ( Figure 6(Only one is shown in the diagram), memory 61, and computer program 62 stored in said memory 61 and executable on said at least one processor 60, wherein the processor 60 executes said computer program 62 to implement the steps in the context management method embodiments of any of the above-described code generation.
[0122] The computer device may include, but is not limited to, a processor 60 and a memory 61. Those skilled in the art will understand that... Figure 6 The examples of computer devices are merely examples and do not constitute a limitation on computer devices. They may include more or fewer components than shown in the illustration, or combinations of certain components, or different components. For example, they may also include input / output devices, network access devices, etc.
[0123] The processor 60 may be a Central Processing Unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor.
[0124] In some embodiments, the memory 61 may be an internal storage unit of the computer device, such as a hard drive or memory. In other embodiments, the memory 61 may be an external storage device of the computer device, such as a plug-in hard drive, smart media card (SMC), secure digital (SD) card, flash card, etc. Furthermore, the memory 61 may include both internal and external storage units of the computer device. The memory 61 is used to store the operating system, applications, bootloader, data, and other programs, such as the program code of the computer program. The memory 61 can also be used to temporarily store data that has been output or will be output.
[0125] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include at least: any entity or system capable of carrying computer program code to a system / computer device, a recording medium, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium, such as a USB flash drive, a portable hard drive, a magnetic disk, or an optical disk.
[0126] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail or recorded in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0127] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0128] In the embodiments provided in this application, it should be understood that the disclosed systems / computer devices and methods can be implemented in other ways. For example, the system / computer device embodiments described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interfaces, devices, or units, and may be electrical, mechanical, or other forms.
[0129] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0130] The above-described embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.
Claims
1. A method for context management in code generation, characterized by, The method includes: Perform structured parsing of a context containing C language source code and dialogue history, and identify and classify the structured and unstructured elements in the context; When the compression triggering condition is met, the structural element is given priority protection based on the dependency reference relationship between the current active round and the historical structural element, so that the structural element referenced by the current active round is completely preserved. Differentiated compression is applied to structural and unstructured elements that are not protected by priority. Prioritized structural elements, differentially compressed structural elements, and differentially compressed unstructured elements are recombined to generate an optimized context for use by a large language model.
2. The method according to claim 1, characterized in that, The structural elements include header file dependency chains, compile-time symbol definitions, register mappings, static configuration tables, and function call topologies; when compressing the function call topology, at least two layers of adjacent call relationships involving the direct caller and the direct callee involved in the current active round modification point are preserved; The unstructured elements include natural language descriptions, agent reasoning processes, tool execution results, and compilation error outputs.
3. The method of claim 2, wherein, The step of prioritizing the structural elements based on the dependency relationship between the current active round and historical structural elements, so that the structural elements referenced by the current active round are fully preserved, includes: When the currently active round explicitly or implicitly references header files or function definitions from historical rounds, compression or downgrading of the header files or function definitions is prohibited through a dependency transitive locking mechanism. The compile-time symbol definitions retain complete key-value pairs or complete declaration relationships, and are sorted according to the frequency of use of the compile-time symbol definitions in historical commands, so as to retain compile-time symbol definitions whose usage frequency reaches a preset frequency threshold and discard compile-time symbol definitions whose usage frequency does not reach the preset frequency threshold.
4. The method of claim 2, wherein, The differential compression of structured and unstructured elements that are not protected by priority includes: A summary of the source code in the context is generated while preserving C language declaration-level information, and differential compression is performed on the static configuration table in the structural element.
5. The method according to claim 4, characterized in that, The C language declaration-level information includes function signatures, type definitions, and constant values after macro expansion; The abstracts are classified into L1 level abstracts, L2 level abstracts, and L3 level abstracts according to their granularity. The L1 level summary is a file-level overview, retaining the file name and file function description; The L2 level summary is module-level information, retaining the function list and the global variable list; The L3 level summary is statement-level information, retaining conditional statements, assignment statements, and function call statements.
6. The method according to claim 4, characterized in that, Performing differential compression on the static configuration table in the structural element includes: The baseline version of the static configuration table is stored in the form of a folded digest, which retains the table header, total number of rows, index start value, and index end value. The modified difference rows in the static configuration table are preserved in their complete form, along with comments on the original values.
7. The method of claim 6, wherein, The method further includes: Differential compression is triggered when the token usage of a single static configuration table in the context exceeds a preset ratio threshold.
8. The method of claim 1, wherein, The method further includes: Before performing the differential compression, the integrity baseline Sbaseline and the current instantaneous structural score Scurrent of the structural element are calculated. Calculate the difference between the integrity baseline Sbaseline and the current instantaneous structural score Scurrent, and calculate the ratio of the difference to the integrity baseline Sbaseline; If the ratio is greater than a preset loss threshold, the compression of the structured elements is abandoned, only the unstructured elements are released, and external vector retrieval or disk cache replacement is triggered to replace the historical round records.
9. The method of claim 8, wherein, The compression triggering condition includes the context's token usage rate reaching a preset usage rate threshold; The differentiated compression implements a three-level compression strategy based on the token usage rate: When the token usage rate reaches a preset first usage rate threshold but does not reach a preset second usage rate threshold, L1 light compression is performed, and the unstructured elements are truncated in chronological order starting from the earliest dialogue round. When the token usage rate reaches a preset second usage rate threshold but does not exceed a preset third usage rate threshold, L2 medium compression is performed to generate an L2 level summary for the historical rounds. When the token usage rate exceeds a preset third usage rate threshold, L3 deep compression is performed, structural elements from historical rounds are downgraded and retained, and L1 level digests are generated for unstructured elements. The complete interaction corresponding to the current active round and the complete interaction corresponding to the previous round are not affected by the differential compression. The complete interaction includes user questions, agent responses, and tool execution results.
10. A system for context management in code generation, characterized by include: The context parsing module is used to perform structured parsing of the context containing C language source code and dialogue history, and to identify and classify the structured and unstructured elements in the context. The priority protection module is used to protect the structural elements based on the dependency relationship between the current active round and the historical structural elements when the compression triggering condition is met, so that the structural elements referenced by the current active round are completely preserved. The differential compression module is used to perform differential compression on structured and unstructured elements that are not protected by priority. The context reorganization module is used to reorganize priority-protected structural elements, differentially compressed structural elements, and differentially compressed unstructured elements to generate an optimized context for use by large language models.
