Warehouse-level long code-oriented high-density completion input construction and completion method
By employing structured parsing, multi-channel relevance retrieval and filtering, importance assessment, and adaptive truncation, a high-density completion input is generated, solving the problem of excessively long input length in repository-level code completion and improving the stability and efficiency of the completion results.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HANGZHOU DIANZI UNIV
- Filing Date
- 2026-01-28
- Publication Date
- 2026-05-12
AI Technical Summary
In existing technologies for repository-level code completion, excessively long input lengths result in high memory usage, large computational overhead, and low information utilization efficiency, affecting real-time performance and user experience. Furthermore, direct truncation or heuristic trimming methods are prone to losing key information, leading to unstable completion results.
By employing structured parsing, multi-channel relevance retrieval and fusion filtering, function-level importance assessment and adaptive truncation, and fine-grained function block selection and pruning, high-density completion inputs are generated, ensuring that the model input length is reasonable while retaining key contextual information, reducing inference overhead and improving completion stability.
It effectively reduces the length and computational cost of the completion input, while improving the stability and usability of the completion results, ensuring the generation of high-quality completion results under context window constraints.
Smart Images

Figure CN122018918A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of computer technology, and relates to artificial intelligence and software engineering. Specifically, it relates to a method for constructing and completing high-density completion input for long code at the repository level. Background Technology
[0002] As large code models are increasingly used in integrated development environments (IDEs), repository-level code completion is gradually becoming an important technology for improving development efficiency. In repository-level completion scenarios, the model not only needs to reference local code snippets near the completion location but also background code information from the same repository to understand the semantic environment, type constraints, call relationships, and cross-file dependencies of the completion point, thereby generating semantically consistent and compileable completion results. Compared to traditional single-file completion, repository-level completion relies more heavily on the input context and requires broader background information, leading to a significant increase in the length of the completion input. However, existing large language models typically have an upper limit to the length of context they can handle in a single inference iteration. When the background code is large, directly inputting the complete background code into the model is no longer sufficient for practical deployment needs. On the one hand, the inference process of code completion models typically requires operations such as attention calculations on the input sequence. Increasing the input length significantly increases memory usage and computational overhead, leading to higher inference latency and impacting the real-time performance, concurrency capabilities, and user experience of interactive code completion. On the other hand, even if some models support longer context windows, excessively long inputs may still reduce the model's efficiency in utilizing key information, resulting in insufficient attention to crucial information in the middle of long sequences and unstable completion results. Furthermore, with the widespread adoption of commercial deployments and API calls, longer inputs also mean higher call costs and more expensive computational resources, making directly "stacking context" unsustainable in engineering. Therefore, reducing the length of the completion input, increasing the density of input information, and controlling inference costs without sacrificing completion quality have become critical issues that repository-level code completion systems urgently need to address.
[0003] To address the aforementioned issues, existing technologies typically truncate the background code, retaining only the context closest to the completion point to construct the completion input. However, this approach fails to adequately consider the prevalent cross-function and cross-file dependencies in code repositories, easily leading to the loss of definitions, calls, or constraints strongly related to the completion point, thus affecting the correctness and stability of the completion results. To mitigate the information loss problem caused by direct truncation, some solutions attempt to trim the background code using heuristic rules or static statistical features. For example, they identify redundant statements, repetitive structures, or low-value fragments and perform deletion or merging, which can reduce the input size to some extent. However, because they mainly rely on predefined rules or static features, it is difficult to dynamically perceive which information is truly useful for the current completion. In different projects, with different coding styles, and in different completion scenarios, it is easy to accidentally delete key context or retain irrelevant fragments, resulting in unstable completion effects. In addition, some studies have attempted to use the scoring signals of large language models themselves to evaluate the importance of candidate code segments and rank and select them accordingly. Although this can utilize the model's internal understanding of the completion task to some extent, it may still be affected by template code, naming style, or similarity in grammatical structure, misjudging noisy segments that are weakly related to or even irrelevant to the completion task as high-value context, thus affecting the stability of the completion results. At the same time, when the candidate scale is large, repeatedly performing model scoring evaluation will bring high computational overhead and inference latency, making it difficult to meet the efficiency and cost requirements of interactive code completion.
[0004] Therefore, existing technologies still lack a method to improve the information density of the completion input and the stability and usability of the completion results while reducing inference overhead and response latency. Summary of the Invention
[0005] To address the shortcomings of existing technologies, this invention proposes a high-density completion input construction and completion method for repository-level long code. Under the premise of explicitly considering context length constraints and inference cost constraints, the background long code context is filtered and compressed, so that the generated completion input not only meets the model input length limit, but also retains as much context information as possible that contributes to the current completion task. This reduces inference overhead and improves completion stability and usability, solving the problems of large background code context, limited model context window, high inference cost, and unstable completion results due to irrelevant noise interference in repository-level long code completion scenarios.
[0006] A high-density input completion method for repository-level long code includes the following steps:
[0007] Step 1: Structured Candidate Construction
[0008] Obtain the current code fragment and its background long code context corresponding to the position to be completed, perform structured parsing on the background long code context, and construct a set of candidate code units.
[0009] Step 2: Multi-channel relevance retrieval and fusion screening
[0010] Candidate code units containing the positions to be completed are used as query code units. The correlation between candidate code units and query code units is evaluated using different metrics. The evaluation results of multiple branches are then fused and filtered to obtain a candidate subset that is closer to the completion requirements.
[0011] Preferably, the relevance evaluation index is one of several based on term matching, semantic representation similarity, and code structure or symbolic dependency.
[0012] Step 3: Function-level importance evaluation, reordering, and adaptive truncation
[0013] The importance of the candidate subset is evaluated and re-ranked based on the large model score signal. Adaptive truncation is performed according to the importance gain decay relationship to remove low contribution candidates, avoiding noise and additional overhead introduced by indiscriminate filling budget, so as to obtain the retained candidate set.
[0014] Step 4: Fine-grained function block selection, trimming, and completion output.
[0015] The candidate code units in the candidate set retained after truncation in step 3 are mapped to a sequence of fine-grained semantic units. Based on the importance evaluation method in step 3, the candidate code units are divided into several functional blocks. The importance of the functional blocks is then evaluated, and they are selected or pruned under budget constraints. Finally, they are concatenated with the current code fragment corresponding to the position to be completed to form a high-density large language model completion input.
[0016] A completion method for constructing high-density completion input for repository-level long code is proposed. For the position to be completed in repository-level long code, a high-density completion input is first generated using the above method, and then input into the code large language model to obtain the completion result of the position to be completed.
[0017] The present invention has the following beneficial effects:
[0018] 1. By introducing a multi-source correlation fusion and hierarchical screening mechanism, the candidate semantic direction is calibrated while reducing the candidate size, thereby reducing the probability of weakly correlated noise being mistakenly selected.
[0019] 2. By using importance assessment reordering and adaptive truncation mechanisms, the candidate set can be dynamically converged based on actual contributions, reducing unnecessary model evaluation overhead and inference latency.
[0020] 3. By fine-grained selection or pruning under budget constraints, the information density and task relevance of the input completion context can be improved, and the stability and usability of the completion results can be maintained or improved under the condition of limited context window.
[0021] 4. Adopt a method consistent with the scoring criteria to divide functional blocks, thereby improving the logical consistency and stability of the fine-grained compression process in different completion tasks. Attached Figure Description
[0022] Figure 1 A flowchart of a completion method for constructing high-density completion input for repository-level long code.
[0023] Figure 2 This is a flowchart of multi-channel relevance retrieval and fusion processing.
[0024] Figure 3 The flowchart shows the process of function-level importance assessment, reordering, and adaptive truncation.
[0025] Figure 4 This is a flowchart of the fine-grained semantic unit compression process. Detailed Implementation
[0026] The present invention will be further explained below with reference to the accompanying drawings;
[0027] This embodiment provides a completion method for constructing high-density completion input for repository-level long code, such as... Figure 1 Specifically, it includes the following steps:
[0028] Step 1: Structured Candidate Construction
[0029] S11. First, obtain the current code snippet where the position to be completed is located and the corresponding background long code context.
[0030] The background long code context can originate from other locations within the same code file, other files in the same repository that have dependencies on the current file, or be pre-aggregated by a repository-level indexing mechanism.
[0031] S12. In order to construct a stable and semantically relatively complete candidate granularity, the background long code context is structured and parsed. Regular expression matching or syntax rule matching is used to identify the declaration start position of functions or classes, and the code between one declaration start position and the next declaration start position is divided into a candidate code unit.
[0032] As an optional implementation, an abstract grammatical structure of the background long code is constructed using a syntax parsing tool, and candidate code units are segmented based on the boundaries of declaration nodes.
[0033] S13. The candidate code units containing the position to be completed are used as query code units, and the remaining candidate code units are used as background candidate sets to form a unified processing object of query information and candidate set, providing input for subsequent multi-channel relevance retrieval and fusion filtering.
[0034] Step 2: Multi-channel relevance retrieval and fusion screening
[0035] S21. To reduce the candidate size and calibrate the semantic direction of the candidate set, this embodiment uses three parallel branches to perform multi-channel relevance retrieval on the candidate code unit set, and fuses the results of each branch, such as... Figure 2 As shown:
[0036] ①Term-based retrieval branch: Using term statistical retrieval methods such as BM25, relevant candidates are obtained based on the degree of matching between candidate code units and query code units at the explicit term level such as identifiers, keywords, and API names, so as to quickly filter obviously irrelevant candidate units.
[0037] ② Retrieval branch based on semantic representation similarity: An embedding model is used to encode candidate code units and query code units into vector representations, and cosine similarity is used to measure the degree of semantic closeness to supplement candidate units with low term overlap but semantic relevance.
[0038] ③ Retrieval branch based on code structure or symbolic dependency: Use syntax analysis tools such as tree-sitter to parse candidate code units and construct abstract syntax structures, further identify function call relationships or symbolic dependencies, and start from the query code unit to perform dependency expansion based on the call relationship or symbolic dependency, and mine related candidate units that may affect completion from the structural level.
[0039] S22. The candidate results from different retrieval branches are deduplicated, merged and sorted in the same process, and candidate completion and noise suppression are performed according to preset rules to generate a candidate subset that is significantly smaller in size than the original candidate set and whose semantic direction is closer to the completion requirements, so as to reduce the number of calls to the subsequent language model scoring evaluation and the overall inference overhead.
[0040] Step 3: Function-level importance evaluation, reordering, and adaptive truncation
[0041] S31. Use average mutual information based on conditional perplexity as the importance evaluation index AMI(c,q):
[0042] AMI(c,q) = PPL(q) - PPL(q|c)
[0043] Where c represents the candidate code unit and q represents the query code unit, PPL(q) represents the perplexity of the language model with respect to the query code unit q without the candidate code unit c, and PPL(q|c) represents the perplexity of the language model with respect to the query code unit q when the candidate code unit c is included as conditional information. The larger the value of AMI(c,q), the greater the contribution of the candidate code unit c to the completion task.
[0044] Each candidate code unit in the candidate subset obtained in step 2 is evaluated independently with the query information, thereby avoiding the increase in complexity caused by combining multiple candidates into a whole for evaluation. The candidate code units in the candidate subset are ranked based on the importance evaluation index AMI(c,q) to form a candidate ranking sequence.
[0045] S32. Candidate code units are included sequentially according to the candidate sorting sequence, and the importance gain relationship between adjacent candidate code units is calculated to characterize the degree of marginal contribution decay:
[0046] r_i = AMI(c_{i+1},q) / AMI(c_i,q)
[0047] Where r_i represents the importance gain ratio of the i-th candidate code unit c_i to its neighboring candidate code units c_{i+1}.
[0048] In this embodiment, when the importance gain ratio is lower than a preset threshold during several consecutive inclusion processes, it is considered that the marginal contribution of subsequent candidates to the completion task has significantly decreased, and the inclusion of subsequent candidate code units is stopped. This avoids forcibly filling candidates to a fixed size or indiscriminately using up the budget, which would introduce noise and cause additional evaluation overhead.
[0049] like Figure 3 As shown, for the candidate subset obtained by S2, the candidate code units are evaluated and reordered at the function level or class level, and adaptive truncation is performed according to the importance gain decay relationship of adjacent candidates to eliminate low-contribution candidates and reduce noise introduction. This allows the retained candidate set to dynamically converge to a more compact range based on the actual contribution, providing higher-quality candidate input for subsequent fine-grained function block selection and pruning, and reducing the overall inference cost and latency.
[0050] Step 4: Fine-grained function block selection, trimming, and completion output.
[0051] S41. Convert each candidate code unit retained in step 3 into a sequence of fine-grained semantic units. The fine-grained semantic units may be lines of code or syntactic fragments obtained by dividing the code into blocks based on abstract syntactic structures.
[0052] S42. Using contrastive perplexity as a fine-grained importance measure, functional block boundaries are detected by analyzing the score changes of adjacent fine-grained semantic units under language model conditions. When the score changes of adjacent fine-grained semantic units exceed a preset threshold, the boundary position is determined, thereby dividing candidate code units into multiple functional blocks. Subsequently, the contrastive perplexity score between each functional block and the query information is calculated as the importance score of that functional block. Under the target context length budget constraint, functional blocks with higher importance scores are preferentially retained, while low-scoring functional blocks are pruned or discarded to maximize the retention of information that contributes more to the completion task within a limited budget.
[0053] S43. Under the premise of meeting the total budget constraint, firstly, based on the importance evaluation index AMI(c,q) in step 3, allocate fine-grained budgets to candidate code units c, so that high-importance candidate units receive more budget to retain more key functional blocks, and low-importance candidate units are allocated less budget to reduce noise and redundancy. Then, within each candidate code unit, selection or pruning is performed based on the importance score of each functional block, thereby increasing the overall information density of the completion input without changing the total budget.
[0054] As an optional embodiment, attention-based scoring signals are used as a fine-grained importance measurement method. Attention association information between query information and candidate content is obtained from a large language model, and functional block boundaries are determined and functional block importance is evaluated accordingly.
[0055] S44. The function blocks retained under the budget constraint are spliced together in a preset order to generate a compressed completion context. Then, the compressed completion context and the current code fragment are used together to form a completion input, which is input to the large language model so that the large language model outputs the completion result of the position to be completed, thereby completing the long code completion task.
[0056] The above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Those skilled in the art can make equivalent substitutions or modifications to candidate block granularity, multi-channel retrieval combination methods, fusion strategies, adaptive truncation threshold strategies, and fine-grained semantic unit types without departing from the spirit and scope of the present invention, and all such substitutions or modifications should fall within the scope of protection of the present invention.
Claims
1. A method for constructing high-density completion input for repository-level long code, characterized in that: Get the current code snippet corresponding to the position to be completed and the corresponding background long code context; The background long code context is structured and parsed, and then segmented according to function boundaries or class boundaries to obtain multiple candidate code units; Using the current code fragment as query information, the correlation between candidate code units and query code units is evaluated through different indicators, and the evaluation results of different correlation indicators are deduplicated and merged to obtain a candidate subset; Based on the large language model scoring signal, the contribution of candidate code units to the completion of the current code segment is characterized and reordered. Candidate code units are included in the order of reordering and the marginal contribution gain is calculated. When the marginal contribution gain meets the preset decay termination condition, the inclusion is stopped to obtain the truncated candidate set. Under the context length budget constraint, the retained candidate set is mapped to fine-grained semantic units and selected or pruned to generate a compressed context; The compressed context is concatenated with the current code snippet to form a high-density completed input.
2. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: The background long code context originates from other locations within the same code file, other files in the same repository that have dependencies on the current file, or is pre-aggregated by a repository-level indexing mechanism.
3. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: Regular expression matching or syntax rule matching is used to identify the starting position of function or class declarations in the context of long code, and the code between one declaration start position and the next declaration start position is divided into a candidate code unit.
4. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: We use a syntax parsing tool to construct an abstract syntax structure of the background long code and perform candidate code unit segmentation based on the boundaries of declaration nodes.
5. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: The relevance metrics shown include term matching, semantic representation similarity, and code structure or symbolic dependencies.
6. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: The large language model scoring signal is AMI(c,q): AMI(c,q) = PPL(q) - PPL(q|c) Where c represents the candidate code unit, q represents the query code unit, PPL(q) represents the perplexity of the language model with respect to the query code unit q without introducing the candidate code unit c, and PPL(q|c) represents the perplexity of the language model with respect to the query code unit q when the candidate code unit c is introduced as conditional information.
7. The high-density completion input construction method for repository-level long code as described in claim 6, characterized in that: The marginal contribution gain is: r_i = AMI(c_{i+1},q) / AMI(c_i,q) Where r_i represents the marginal contribution gain of the i-th candidate code unit c_i, and AMI(c_i,q) and AMI(c_{i+1},q) represent the large language model score signals of the i-th candidate code unit c_i and its adjacent candidate code units c_{i+1}, respectively.
8. The high-density completion input construction method for repository-level long code as described in claim 1, characterized in that: The fine-grained semantic unit is a line of code or a syntactic fragment obtained by dividing the code into blocks based on an abstract syntactic structure; Using contrastive perplexity as a fine-grained importance measure, functional block boundaries are detected by analyzing the score changes of adjacent fine-grained semantic units under language model conditions. When the score changes of adjacent fine-grained semantic units exceed a preset threshold, the boundary position is determined, thereby dividing the candidate code unit into multiple functional blocks. Subsequently, the perplexity score between each functional block and the query information is calculated as the importance score of the functional block. Under the target context length budget constraint, functional blocks with higher importance scores are retained first, and functional blocks with low scores are pruned or discarded.
9. A completion method for constructing high-density completion input for repository-level long code, characterized in that: For the positions to be completed in long code at the repository level, a high-density completion input is first generated using any of the methods described in claims 1 to 8, and then input into the code large language model to obtain the completion result of the position to be completed.
10. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to perform the method of any one of claims 1 to 8.