A stage-aware large model context reversible compression and caching method and system

CN122927306APending Publication Date: 2026-10-16ZHONGKE NANJING SOFTWARE TECH RES INST
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611407466.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-11
Publication Date
2026-10-16

AI Technical Summary

Technical Problem

[0010]本发明的目的是提供一种阶段感知的大模型上下文可逆压缩与缓存方法及系统,以解决现有大模型上下文管理方案在智能体式软件研发运维领域中,因缺乏任务阶段感知、压缩信息不可精确可逆恢复、缓存与记忆调度机制僵化以及缺少闭环自适应优化,导致推理精度与处理效率难以兼顾的技术问题

Benefits of technology

本发明通过规划阶段优先保留目标与约束,工具调用阶段侧重参数与异常码,反思阶段聚焦失败轨迹,输出生成阶段保留证据原文,相比统一压缩方式,该机制在同等token预算下有效避免了关键约束丢失、参数缺失和证据断裂,提升了长链任务执行的稳定性与准确性。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122927306A_ABST
    Figure CN122927306A_ABST
Patent Text Reader

Abstract

The application discloses a stage-aware large model context reversible compression and caching method and system, and relates to the technical field of artificial intelligence and large model inference optimization. The method comprises the following steps: obtaining an original software research and development operation context, performing structured segmentation and semantic annotation, and constructing a context dependency graph; identifying a current task stage and matching a compression strategy template, calculating a context importance score, and dividing a hard reservation set and a candidate compression set; performing a memory operation on a candidate object through a rule constraint and a reinforcement learning mixed decision architecture; performing hierarchical reversible compression on the context to be reserved, and generating a reversible compression package supporting on-demand expansion and accurate recovery; realizing double-channel cache reuse and incremental update based on a composite cache key; assembling a hierarchical context package to input a large model, and dynamically adjusting strategy parameters according to monitoring feedback. The application can adaptively compress the context according to the task stage, significantly improve the context processing efficiency while ensuring the inference accuracy.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of artificial intelligence and large model inference optimization technology, specifically to a stage-aware large model context reversible compression and caching method and system. Background Technology

[0002] The application of large language models and multimodal large models in the field of intelligent agent software R&D and operation is becoming increasingly in-depth, covering key aspects such as code generation, code repair, code review, automated test generation, build deployment diagnosis, defect localization, knowledge base retrieval, complex task planning, automatic tool invocation, and execution result reflection. In such intelligent agent systems, the scale of context to be processed in a single inference operation expands dramatically. Moreover, the context data not only includes natural language descriptions but also involves highly structured, semantically dense, and extremely precise content such as source code files, function signatures, API contracts, test assertions, build parameters, exception stacks, version numbers, and patch differences.

[0003] To address the aforementioned issues, the industry is exploring solutions in three main directions: first, directly expanding the model's context window and improving the model's ability to handle long sequences by refining attention mechanisms or positional encoding; second, compressing the context on the input side by summarizing or truncating the context length; and third, compressing or reusing the key-value cache (KVCache) within the model to reduce memory usage and computational overhead during inference.

[0004] Although the above solutions have achieved certain results in specific scenarios, they still face several unavoidable problems in real-world business deployments.

[0005] In agent-based scenarios, the context requirements differ significantly across task phases. The planning phase relies more on goal definition, constraints, and global dependencies; the tool invocation phase focuses on interface parameters, recent observations, and execution context; and the reflection phase emphasizes failure reasons, abnormal trajectories, and decision biases. Existing compression schemes mostly employ fixed summary lengths, uniform truncation ratios, or single similarity thresholds, making it difficult to achieve a balance between efficiency and effectiveness.

[0006] Specifically, in the field of intelligent body software R&D and operation and maintenance, the planning phase usually needs to retain the requirements, objectives, repository structure, dependency configuration and acceptance criteria; the tool invocation phase needs to retain the tool name, command parameters, path, branch, version number, status code and exception stack; the execution phase needs to retain the code block to be modified, test assertions, build script and environment variables; and the reflection and error correction phase needs to retain the failure trajectory, rollback record, patch differences and information on failed tests.

[0007] In software development and operations intelligent agents, scenarios involving code repair, code review, automated test generation, build deployment diagnosis, and defect localization can all directly affect the correctness of inference results, including source code order, function signatures, interface parameters, exception stack traces, version numbers, test assertions, configuration items, and even punctuation. Existing solutions often employ semantic summarization or generative completion to achieve so-called recovery, which is essentially still lossy compression. The recovered content cannot be guaranteed to be completely consistent with the original text at the token level, and the introduced information distortion risk is unacceptable in high-precision tasks.

[0008] When the same or similar context is repeatedly input in RAG, long session or loop agent process, existing systems usually repeatedly perform embedding computation, importance scoring and summary generation, lacking an effective caching and incremental update mechanism for historical compression results, resulting in a significant waste of computing power.

[0009] Most systems rely on fixed thresholds or simple rules to determine whether to write context into long-term memory. This can lead to low-value information being repeatedly written while high-value information is missed. As the memory expands with task execution, its quality continues to decline, affecting the hit rate and relevance of subsequent searches. Summary of the Invention

[0010] The purpose of this invention is to provide a phase-aware large model context reversible compression and caching method and system to solve the technical problems in the development and maintenance of intelligent agent software in the field of existing large model context management schemes, which are difficult to balance inference accuracy and processing efficiency due to the lack of task phase awareness, the inability to accurately and reversibly recover compressed information, the rigidity of caching and memory scheduling mechanisms, and the lack of closed-loop adaptive optimization.

[0011] To achieve the above objectives, the technical solution provided by this invention is: a stage-aware large model context reversible compression and caching method, comprising the following steps: S1: Obtain the original software development and operation context data and represent it as a unified set of context objects; perform structured segmentation and semantic annotation on the context objects; establish a collaborative storage mechanism between the original context repository and the activity context pool; and construct a context dependency graph. S2: Identify the current task stage based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, calculate the importance score of each context object in the current task stage based on the target compression strategy template and the context dependency graph, and divide the context objects into a hard retention set and a candidate compression set according to the importance score and hard retention rules. S3: For context objects in the candidate compression set, a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network outputs memory operation actions, and the memory set is updated according to the memory operation actions; according to the target compression policy template and importance score, hierarchical reversible compression is performed on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; S4: Generate cache keys based on the feature information of each context object; if the cache keys match, the cache is reused directly; if the semantic similarity is higher than the preset threshold, the difference part is incrementally processed based on the historical compressed package and then reused; if the cache is not hit, full compression is performed and written to the cache. S5: Assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, input it into the large model to execute the current task; during and after the task execution, collect feedback information based on the task execution results, and dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

[0012] To optimize the above technical solution, the specific measures also include: In step S1, the structured segmentation and semantic annotation of the context object includes: determining the segmentation granularity parameters and segmentation strategy according to the current task stage or the predicted task stage, performing differentiated segmentation on context objects of different modalities, wherein the modalities include natural language text, code, tool return results, image data and audio data; each segmentation unit records its aggregation mapping relationship and splitting mapping relationship with the previous level granularity and the next level granularity, forming a multi-granularity hierarchical index.

[0013] Further, in step S1, the specific process of constructing the context dependency graph is as follows: A context dependency graph is constructed using each context object as a node and the temporal adjacency, semantic similarity, causal association, reference relationship, tool call chain relationship, or code dependency relationship between context objects as edges. Each node maintains a popularity value, which is increased when the node is successfully recalled and updated periodically according to a time decay function. When the number of nodes in the context dependency graph exceeds a preset threshold, nodes and their associated edges that meet the conditions of having a popularity value lower than the preset threshold, time decay exceeding a preset window, and not belonging to the hard retain set are deleted.

[0014] In step S2, the calculation of the importance score of each context object in the current task stage based on the target compression strategy template and the context dependency graph specifically includes: Importance scores are calculated based on a weighted sum of factors including the task relevance of the context object to the current task objective, the criticality of the context object in the context dependency graph, information timeliness, source credibility, future reuse probability, information novelty, precise retention of tokens, cross-modal alignment relevance, and normalized logarithmic token length.

[0015] In step S2, the current task stage includes the retrieval preparation stage, planning stage, tool invocation stage, execution stage, reflection and error correction stage, and output generation stage; the Agent's running status includes the current system controller status, the type of the most recent model output action, whether there is a tool to be invoked, whether there is a tool execution failure or anomaly, whether it is currently in the summary output stage, the current token budget balance, and the completion status of sub-goals in the current task chain.

[0016] Furthermore, in step S2, the target compression strategy template includes at least compression granularity, hard retention rules, scoring weight parameter groups, context budget allocation scheme, and reversible compression mode identifier; the compression strategy templates corresponding to different task stages adopt differentiated configurations: During the retrieval preparation and planning phases, the compression granularity is configured to be either paragraph-level or file-level, and hard retention rules prioritize the retention of task objectives, user constraints, and global dependencies. During the tool invocation phase, the compression granularity is configured to be either sentence-level or function signature-level, the hard retention rule prioritizes the retention of tool name, parameters and exception codes, and the reversible compression mode is configured to be strongly reversible. During the execution phase, the compression granularity is configured at the code block level or the function level, and the hard retention rules prioritize the retention of reentrant execution contexts, key observations, and exception scenarios; Furthermore, during the reflection and error correction phase, the compression granularity is configured at the error propagation path level or the semantic fragment level, the hard retention rule prioritizes the retention of failure trajectories, error messages and attempt history, and the reversible compression mode is configured as a strongly reversible mode. During the output generation stage, the compression granularity is configured at the evidence fragment level, and the hard retention rule prioritizes retaining supporting evidence for the conclusion and citations of the original text.

[0017] Furthermore, in step S3, the memory operation actions include writing to the original memory, writing to the compressed memory, merging with existing memory, delayed writing, deletion upon forgetting, updating only the index, memory upgrade, and memory downgrade; The reversible compressed package contains a set of key segments, a mapping relationship between key segments and the original interval, the complete residual of the omitted segments, or a pointer to the original text repository, the source version number and start and end offsets, and a checksum. In strong reversible mode, it can be independently restored by relying on the complete residual within the package. In standard mode, it can be restored by loading the corresponding version from the immutable original text repository using the pointer to the original text. It supports expansion as needed to restore the original segments or the original context object.

[0018] In step S3, the hybrid decision architecture combining the rule constraint layer and the reinforcement learning policy network is specifically as follows: The first layer of rules and constraints is used to hard filter candidate context objects according to preset security rules and force the output of the corresponding memory operation actions; The second layer is the reinforcement learning policy network layer, which is used to output the Q value of each memory operation action by the online Q network for the candidate context objects filtered by the rule constraint layer and select the final action according to the ε-greedy policy; Furthermore, the third post-processing verification layer is used to perform secondary security verification on the actions output by the reinforcement learning policy network layer. If the selected action will cause high-value memories to be forgotten and the current stage is the planning stage or the reflection stage, it will be forcibly downgraded to delayed writing; if the selected action will merge two conflicting memories, it will trigger manual verification marking or downgrade to updating the index only.

[0019] In step S3, the reinforcement learning policy network layer adopts a dual deep Q-network structure, including an online Q-network and a target Q-network. The online Q-network updates parameters based on experience replay samples and is used to select the next action. The target Q-network is used to calculate the temporal difference target value and synchronize parameters from the online Q-network every preset number of steps. The reward is based on a comprehensive calculation of multiple factors, including task success rate, retrieval hit effect, number of tokens saved, cache reuse effect, cost of incorrect inference due to compression, additional latency of on-demand restoration, and degree of accumulation of noisy memory.

[0020] In step S3, the memory operation actions include writing to the original memory, writing to the compressed memory, merging with existing memory, delayed writing, deletion upon forgetting, updating only the index, memory upgrade, and memory downgrade; The reversible compressed package contains a set of key segments, a mapping relationship between key segments and the original interval, the complete residual of the omitted segments, or a pointer to the original text repository, the source version number and start and end offsets, and a checksum. In strong reversible mode, it can be independently restored by relying on the complete residual within the package. In standard mode, it can be restored by loading the corresponding version from the immutable original text repository using the pointer to the original text. It supports expansion as needed to restore the original segments or the original context object.

[0021] Furthermore, in step S3, the memory set includes short-term operational memory, phased episodic memory, long-term semantic memory, tool experience memory, and error pattern memory; the memory set supports bidirectional transfer, items frequently recalled in short-term operational memory can be upgraded to phased episodic memory, items repeatedly verified as stable knowledge in phased episodic memory can be abstracted and written into long-term semantic memory, and experience items in long-term semantic memory are downgraded to error pattern memory if they are corrected multiple times.

[0022] In step S3, the layered reversible compression includes the following layers: First-level segment-level filtering: Only each segment unit in the candidate compression set is judged to retain, compress, or omit the payload; segment units with a local importance score higher than the first threshold are retained in their entirety, segment units between the first and second thresholds are entered into the second-level compression, and segment units below the second threshold are omitted from the model inference payload; for payload omission units, the strong reversible mode writes its complete residual into the reversible compressed package, and the standard mode writes its original pointer, source version number, start and end offsets, and checksum into the reversible compressed package; Furthermore, compression within the second-layer unit: the segmented units entering the second layer are compressed using at least one of the following methods: key element extraction, structure-preserving summary, deduplication and merging, parameter simplification, or differential encoding. The third layer of cross-unit reference compression: For multiple segmented units with dependencies, if the content similarity between units is higher than a preset threshold, one of them stores the complete content, and the remaining units store reference pointers to that unit and difference patches.

[0023] In step S3, the recovery information of the reversible compressed package is adaptively encoded according to the compression mode and content type: the strong reversible mode stores the complete residual of all omitted content to achieve self-contained recovery within the package; the standard mode stores at least the original text pointer pointing to the immutable original context repository, the source version number, the start and end offsets and the check value to achieve system-level reversible recovery that depends on the original context repository.

[0024] For natural language text, use original text pointers plus offsets of the start and end positions of deleted segments, or differential encoding plus position mapping; Furthermore, the code is processed using either plaintext pointers plus the range of line numbers of the deleted code lines or token-by-token differential encoding plus AST node position mapping; For structured data, add a default value marker to the path of the deleted field or add a value change log to the full field-level location mapping; Differential encoding with a first item plus a difference sequence is used for log or time-series data.

[0025] Furthermore, in step S3, the reversible compressed package supports on-demand expansion to restore the original fragments or the original context object, specifically as follows: The fragment-level recovery is as follows: based on the identifier of the requested fragment, locate the storage location of the target fragment in the compressed package in the mapping relationship, extract the corresponding content from the residual storage or the original text pointer, and then concatenate it with the relevant content in the key fragment set and return it. The context object-level recovery is as follows: the fragments in the key fragment set and the residual fragments in the residual storage are sequentially spliced ​​together according to the order of the position-encoded records, the structural hierarchy is restored through structural tags, and the complete original context object is reconstructed. After recovery, a consistency check is performed using the aforementioned check value. If the check fails, the system reverts to the original version in the original context repository.

[0026] In step S4, the incremental compression includes: The system locates difference fragments based on rolling hashing; it extracts newly added, deleted, or modified fragments using the Diff algorithm to generate difference descriptions; it performs importance scoring and compression only on newly added or modified fragments in the difference descriptions to generate incremental compressed packages; it merges the mapping relationships and residual storage in the incremental compressed packages with the corresponding structures in the historical compressed packages, updates the position offsets and reference pointers, and increments the version number.

[0027] Further, in step S4, generating a cache key based on the feature information of each context object specifically involves: performing normalized encoding on the feature information of each context object and concatenating it into a byte string, then performing a hash operation on the byte string to obtain a composite cache key; the feature information of each context object includes: original content hash, tokenizer version, model version, compression strategy version, task stage identifier, task target hash, budget interval identifier, precise retention flag, dependency graph signature, and data structure version number; the normalized encoding includes field name sorting, null value placeholders, character set unification, and floating-point quantization.

[0028] If two context blocks have the same original content, but differ in any of the following: tokenizer version, model version, compression strategy version, task target hash, budget interval identifier, exact retention tag, dependency graph signature, or data structure version, then entry into the exact hit channel is prohibited.

[0029] In step S5, the hierarchical context package is assembled according to the following priority order: objects in the hard-reserved set are loaded only into one corresponding layer of the L1 system constraint layer, L2 task objective layer, or L4 tool call layer, or critical observation and precise retention layer according to their content type, and are not repeatedly loaded into the corresponding layer of the updated memory set or reversible compressed package: The L1 system constraint layer contains system role definitions, security boundaries, and output format specifications. It consists of system-level constraint items in the hard-reserved set, has the highest priority, and does not participate in compression. The L2 task objective layer includes the user's original intent, task completion criteria, and explicit requirements; The L3 core active memory layer contains highly relevant memory entries retrieved from the updated memory set based on the current task stage; The L4 layer, which contains the input parameters, return key values, status codes, and exception information of the most recent tool call, as well as key evidence, code snippets, API contracts, test assertions, build parameters, exception stacks, or version numbers that do not fall into L1 or L2 but meet the hard retention rules; objects in H are fully loaded in this layer and do not participate in compression. L5 compressed evidence layer, containing a set of key fragments from a reversible compressed package; The L6 reversible index appendix layer contains mapping relationships, residual storage, and position encoding in the reversible compressed package; Furthermore, when the accumulated number of tokens exceeds the current stage's budget limit, secondary compression will begin from layer L5, while layers L1 to L4 will not participate in secondary compression.

[0030] Furthermore, in step S5, the collection of feedback information based on the task execution result includes the following triggering conditions and corresponding operations: When the model output confidence score is lower than a preset threshold, fragment-level on-demand expansion is triggered. When the tool returns an error code or exception, the failure trajectory is written to the error mode memory and the context object level is triggered to expand as needed; When the model output claims to reference something but there is no corresponding mapping in the compressed package, a consistency check is triggered. If the check fails, the original text in the original context repository is rolled back. Furthermore, when a parameter conflict is detected, the corresponding fragment is expanded on demand and the conflict record is written to the error mode memory; when the cache misses multiple times in a row, the cache preheating mechanism is triggered.

[0031] In step S5, the dynamic adjustment of the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture includes: Based on one or more of the following metrics: task success rate, answer accuracy, token saving rate, cache hit rate, number of on-demand expansions and recovery attempts, and number of user revisions, update the scoring weight parameter group and budget allocation scheme in the compression strategy template, and update the reward function parameters of the reinforcement learning policy network.

[0032] As another important technical solution, this invention also provides a stage-aware large model context reversible compression and caching system, implementing the stage-aware large model context reversible compression and caching method described above, including: The context acquisition and structured management module is used to acquire raw software development and operation context data and uniformly represent it as a collection of context objects. It performs structured segmentation and semantic annotation on the context objects, establishes a collaborative storage mechanism between the raw context repository and the activity context pool, and constructs a context dependency graph. The task phase identification and compression strategy selection module is used to identify the current task phase based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, calculate the importance score of each context object in the current task phase based on the target compression strategy template and the context dependency graph, and divide the context objects into a hard retention set and a candidate compression set according to the importance score and hard retention rules. The memory scheduling and compression module is used to output memory operation actions for context objects in the candidate compression set through a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network, and update the memory set according to the memory operation actions; according to the target compression policy template and importance score, it performs hierarchical reversible compression on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; The cache management module is used to generate cache keys based on the feature information of each context object. If the cache key matches, the cache is directly reused. If the semantic similarity is higher than the preset threshold, the difference is incrementally processed based on the historical compressed package and then reused. If the cache is not hit, full compression is performed and written to the cache. The context assembly and optimization module is used to assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, which is then input into the large model to execute the current task. During and after the task execution, feedback information is collected based on the task execution results to dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

[0033] The present invention also proposes an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, it implements a phase-aware large model context reversible compression and caching method as described above.

[0034] The present invention also proposes a computer-readable storage medium storing a computer program that enables a computer to execute a phase-aware large model context reversible compression and caching method as described above.

[0035] Compared with the prior art, the beneficial effects of the present invention are: This invention prioritizes the retention of goals and constraints during the planning phase, focuses on parameters and exception codes during the tool invocation phase, focuses on failure trajectories during the reflection phase, and retains the original evidence text during the output generation phase. Compared with a uniform compression method, this mechanism effectively avoids the loss of key constraints, missing parameters, and broken evidence under the same token budget, thereby improving the stability and accuracy of long-chain task execution.

[0036] This invention constructs a context dependency graph based on temporal adjacency, causal association, reference relationship, and tool call chain. It combines stage-differentiated weights to comprehensively evaluate the context value, which can identify short but key-constraint information and filter out long and low-value background content. This provides an interpretable decision basis for compression and memory scheduling, and improves budget utilization efficiency.

[0037] This invention employs hierarchical memory management, with rules constraining the security of hard-retained content. A reinforcement learning strategy network autonomously decides whether to write, merge, delay, or forget based on reward signals such as task success rate, retrieval hit rate, and token saving. Post-processing verification serves as a fallback, effectively suppressing the disorderly expansion of the memory bank and enabling the system to maintain the recall quality of high-value memories during long-term operation.

[0038] This invention generates reversible compressed packages through a three-layer structure of fragment-level filtering, intra-unit compression, and cross-unit reference compression. It supports on-demand expansion at the fragment or object level to accurately restore the original text, and ensures consistency with the original repository through checksum rollback. It is suitable for high-precision scenarios in intelligent software R&D and maintenance, such as code review, code repair, automated test generation, build deployment diagnosis, and defect localization. Users can enjoy the efficiency improvements brought by compression while accurately restoring the original text when needed, eliminating the inference bias risk of lossy compression.

[0039] This invention integrates task phase, target hash, dependency graph signature and other dimensions to generate composite cache keys to achieve accurate hits; for similar content, incremental compression is performed only on the changed parts and merged with historical packages for reuse, effectively avoiding the computational overhead of repeated compression, while version control prevents incorrect reuse caused by model or strategy changes.

[0040] This invention forms a closed loop between task execution and result feedback. Based on indicators such as success rate, cache hit rate, saving rate, number of recovery attempts and user revision frequency, the strategy weight and reward parameters are continuously updated. As the deployment time increases, the system continuously adapts and optimizes, which has strong engineering implementation value.

[0041] In the intelligent body-based software R&D and operation system, this invention incorporates context objects from code repositories, continuous integration pipelines, testing platforms, defect management systems, and interface documentation libraries into the same reversible compression and caching chain. This enables tool call parameters, build logs, exception stacks, test assertions, and patch differences to be retained, compressed, cached, and restored on demand in stages, thereby reducing the risks of repetitive calculations, error reuse, and loss of key technical details in long-chain automated R&D tasks. Attached Figure Description

[0042] Figure 1 This is a schematic diagram of the process in an embodiment of the present invention.

[0043] Figure 2 This is a schematic diagram of the process of constructing a reversible compressed package in an embodiment of the present invention. Detailed Implementation

[0044] The present invention will be further described in detail below through specific embodiments, but it should not be construed as limiting the scope of the subject matter of the present invention to the following embodiments. All technologies implemented based on the above content of the present invention fall within the scope of the present invention.

[0045] In the following implementation, the original software development and operation context data is specifically the software development and operation intelligent agent context data in the field of intelligent agent software development and operation. The software development and operation intelligent agent is used to perform code generation, code repair, automated testing, build deployment diagnosis and defect location tasks among code repositories, continuous integration pipelines, defect management systems, interface document libraries and test platforms.

[0046] In some implementations, such as Figure 1 As shown, this invention provides a stage-aware large model context reversible compression and caching method, including the following steps: S1: Obtain the original software development and operation context data and represent it as a unified set of context objects; perform structured segmentation and semantic annotation on the context objects; establish a collaborative storage mechanism between the original context repository and the activity context pool; and construct a context dependency graph. S2: Identify the current task stage based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, calculate the importance score of each context object in the current task stage based on the target compression strategy template and the context dependency graph, and divide the context objects into a hard retention set and a candidate compression set according to the importance score and hard retention rules. S3: For context objects in the candidate compression set, a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network outputs memory operation actions, and updates the memory set according to the memory operation actions; based on the target compression policy template and importance score, hierarchical reversible compression is performed on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; S4: Generate cache keys based on the feature information of each context object; if the cache keys match, the cache is reused directly; if the semantic similarity is higher than the preset threshold, the difference part is incrementally processed based on the historical compressed package and then reused; if the cache is not hit, full compression is performed and written to the cache. S5: Assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, input it into the large model to execute the current task; during and after the task execution, collect feedback information based on the task execution results, and dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

[0047] In some implementations, the raw software development and operation context data obtained in step S1 is acquired by the data access layer of the software development and operation intelligent agent through code repository connectors, continuous integration pipeline connectors, defect management system connectors, test platform connectors, interface documentation library connectors, and tool call listeners to obtain multi-source context data. Specifically, the code repository connector reads source code files, directory structure, function signatures, patch differences, and commit notes from the code hosting platform's commit history, branch snapshots, file reading interface, or local repository workspace; the continuous integration pipeline connector obtains build scripts, dependency configurations, environment variables, status codes, build logs, and deployment results through pipeline event callbacks, build task interfaces, or log collection interfaces; the defect management system connector obtains defect titles, reproduction steps, affected versions, exception stacks, processing status, and historical repair records through defect ticket interfaces; the test platform connector obtains test cases, test assertions, coverage results, failed test cases, and assertion differences through test task interfaces or test report files; the interface documentation library connector obtains interface descriptions, parameter constraints, return codes, version descriptions, and change records through document retrieval interfaces; and the tool call listener records tool names, input parameters, execution time, return results, and exception information before and after the Agent calls compilation, testing, retrieval, code analysis, or deployment tools.

[0048] In some implementations, the acquisition of raw software development and maintenance context data is initiated by task triggering, event triggering, and periodic synchronization triggering: When a user submits a code generation, code repair, build failure analysis, or defect location task, the system parses the repository address, branch, file path, defect number, pipeline number, or test task number according to the task objective and generates a collection plan; when code submission, pipeline completion, test failure, or defect status change events occur, the system incrementally pulls the affected objects through event messages or Webhooks; when long-running tasks are running continuously, the system synchronizes the repository, test, and defect data related to the current task according to a preset period. After the collected data undergoes permission verification, format recognition, character set unification, de-identification filtering, timestamp marking, and version snapshot processing, the system generates the raw content hash, source type, source system identifier, source version number, access permission scope, and raw text pointer. The uncompressed raw content is written to the immutable raw context repository R0, and the object references related to the current task are written to the active context pool A0 for subsequent structured segmentation, semantic annotation, dependency graph construction, and reversible recovery.

[0049] In step S1, the context object collection Each context object It carries several metadata fields to support subsequent compression, caching, and recovery decisions, specifically: In some implementations, the software development and operation intelligent agent context object is further divided into requirement objects, code objects, interface objects, test objects, build and deployment objects, defect objects, and tool observation objects according to their source. Among them, code objects include source files, functions, classes, code blocks, and patch differences; build and deployment objects include build scripts, dependency configurations, environment variables, pipeline steps, status codes, and build logs; and defect objects include defect titles, reproduction steps, exception stacks, affected versions, and repair records.

[0050] The data includes: raw content, source type (e.g., system message, user message, search results, tool return, etc.), timestamp, initial task stage tag, dependency set (pre-embedded causal relationships, reference relationships, or call chain relationships), confidence (indicating the credibility of the object's content itself, determined by source credibility and content consistency, used as the confidence dimension in subsequent importance scoring), and exact retention flag (a boolean value; true indicates that the object cannot be lost and is directly retained). This includes: (1) Influences hard retention determination, compression mode selection, and cache key generation; (2) Pointer to the corresponding original version in the original context repository R0 (rawPointer, used for lazy loading, residual storage, and recovery rollback); (3) Original content pre-computed hash (cachePreHash, which is calculated and persisted immediately when the context object is collected into the original context repository, and all subsequent hash-related operations reuse this value to avoid duplicate calculations); (4) Task goal hash (taskGoalHash); (5) Budget range identifier (budgetBand); (6) Dependency graph signature (dependencySig); and (7) Data structure version number (schemaVer).

[0051] In some implementations, each context object Generate corresponding semantic embedding vectors through the embedding model. It is used to calculate semantic similarity between nodes when constructing context dependency graphs, and to measure task relevance when calculating importance scores.

[0052] The context object also includes extended fields such as unique identifier (ID), modality, token range, consistency signature (generated by hashing the key-value pairs in the context object and used to quickly compare whether there are parameter value conflicts between different objects), and restore policy identifier to support more granular control.

[0053] When performing structured segmentation and multi-dimensional semantic annotation on context objects, the system determines the segmentation granularity parameters and segmentation strategy based on the current task stage or the predicted task stage, and performs differentiated segmentation on context objects of different modalities: In some implementations, modal types include software requirement text, defect descriptions, interface documentation, code, tool return results, build logs, test reports, and image / OCR data extracted from screenshots or screen recordings. Specifically, natural language text is segmented by sentence, paragraph, heading level, or semantic fragment; code is segmented by file, class, function, code block, or call chain node; tool return results are segmented by field, parameter group, or status code; image data is segmented by region, target box, or OCR text block; and audio data is segmented by time slice or speech turn.

[0054] The granularity of segmentation is dynamically adjusted according to the task stage. Specifically: paragraph-level segmentation is preferred in the planning stage; sentence-level or function signature-level segmentation is preferred in the tool invocation stage; and error propagation path-level segmentation is preferred in the reflection and error correction stage. File-level segmentation is suitable as a fallback strategy for the initial loading stage or when the token budget is extremely tight. Token-level segmentation is suitable as the smallest recovery unit in high-precision recovery scenarios or strong reversibility modes. The granularity of segmentation is divided into multiple levels from coarse to fine, including file-level, paragraph / heading-level, sentence / semantic fragment-level, and token-level. The paragraph level is the higher level (coarse-grained) than the sentence level, and the sentence level is the lower level (fine-grained) than the paragraph level. Increasing one level indicates adjustment towards a coarser granularity, and decreasing one level indicates adjustment towards a finer granularity in some implementations.

[0055] If the current available token budget is lower than a preset threshold The system can adjust the granularity of the segmentation in advance, raising the granularity by one level (adjusting towards a coarser granularity) to reduce management overhead.

[0056] Each segmentation unit records its aggregation and splitting mapping relationships with the upper and lower granularities, forming a multi-granularity hierarchical index. The segmentation unit at the upper granularity is called the parent object (parent node) of that unit, and the segmentation unit at the lower granularity is called the child object (child node). This index is stored in a tree structure, with the root node being the original context object and the child nodes being segmentation units of different granularities. This is used for subsequent selection of compression benchmarks by granularity and location of target segments during on-demand recovery.

[0057] Each smallest granularity segmentation unit after segmentation is supplemented with multi-dimensional semantic labels, including: structural labels (describing the structural role of the segmentation unit in the original text, such as title, paragraph, function definition), entity labels (recording extracted key entities, such as person names, function names, parameter names; text-type segmentation units are extracted using a named entity recognition model, and code-type segmentation units are extracted using abstract syntax tree parsing), keyword labels, and topic labels (determined by the LDA model using the number of topics K and the silhouette coefficient, or by using a preset set of topic labels and a confidence threshold). The system includes a zero-shot classifier (default 0.7) that predicts the high-level topic category of the segmentation unit, such as tool calls, error handling, and parameter validation, used for calculating the matching degree between the topic and the current task stage in importance scoring; task association tags (labeling the specific association type with the task objective, such as objective constraints and evidence support, used for filtering the hard-reserved set, with segmentation units whose association type is objective constraints or tool parameters being preferentially added to the hard-reserved set); evidence tags (indicating whether the fragment can be used as inference evidence, calculating the evidence credibility level, used for trigger judgments for on-demand expansion and filtering of compressed evidence layers in the hierarchical context package); and version tags.

[0058] When establishing a collaborative storage mechanism between the original context repository and the active context pool, the uncompressed original context is written into the immutable original context repository R0 for rollback during subsequent reversible recovery.

[0059] In some implementations, R0 can be a database, object store, document repository, vector library-attached raw document repository, or cache store, but it must support locating the original fragment based on pointers and checksums. Each context object maintains a version linked list pointing to all active context pool representations and downstream compressed packages derived from that object. The context object to be involved in inference is written to the active context pool A0 using copy-on-write. Each object in A0 stores a pointer to R0 and a difference overlay, which records modification instructions, pruning instructions, or compression instructions relative to the original version. Objects in A0 inherit all generated tags and pre-embedded dependencies, which are used to construct the context dependency graph.

[0060] The copy-on-write method works as follows: Initially, the context object in the activity context pool A0 only holds a read-only reference (i.e., a pointer) to the original content in the original context repository R0, without actually copying the original data. When it is necessary to modify, trim, or compress the context object, the system creates an independent data copy for the object in A0 and records the modification instructions, trim instructions, or compression instructions (i.e., a difference overlay) on the copy. The content in the original context repository R0 remains unchanged. By delaying data copying, memory usage is reduced, and the immutability of the original data is guaranteed to support subsequent accurate recovery and rollback.

[0061] In some implementations, the context object to be involved in reasoning refers to all the context data required for a single model call that will be input into the large model and participate in the current task. In the field of intelligent agent software R&D and operation and maintenance, the current task includes Agent execution stages such as planning, tool selection, tool invocation, result judgment, reflection and error correction, response generation, code generation, test case generation, analysis of build failures, defect location, generation of patch repairs, or generation of code review comments; the context data includes user requirement input for the current round, system prompts, code repository snippets, interface documents, test cases, build logs, defect reports, tool return results, historical dialogues, and related content loaded from the original context repository.

[0062] When constructing a context dependency graph, each context object is used as a node, and edges are formed by temporal adjacency, semantic similarity, causal association, reference, utility call chain, or code dependency relationships between context objects. Each context object... Corresponding to a node Node attributes include: original content pointer, semantic embedding vector. timestamp Heat value And the hard-hold set membership (if the node belongs to the hard-hold set H, it cannot be deleted during pruning).

[0063] Popularity value Initially set to 1.0, it increases each time the node is successfully recalled. (Default 0.1, maximum 1.0), and updated periodically according to a time decay function: ; in, This is the heat attenuation coefficient (default 0.05). The time interval since the last update (in days, or calculated based on the number of inference rounds).

[0064] Edges between nodes represent dependencies, references, complements, conflicts, or alignment relationships, with each edge having a dependency strength value. The calculation formula is: ; in, This is the time decay factor (default 0.1). This is an indicator function; it is 1 when the dependency field is explicitly referenced, and 0 otherwise. For causal path strength (if and If the paths are in the same call chain or dependency path, take the reciprocal of the path length; otherwise, take 0. Indicates the default weight: , , , Only when When the value is 0.35 (default), edges are created in the graph.

[0065] In some implementations, when a new context object Upon arrival, the set of candidate nodes is the union of the set of valid nodes explicitly pointed to by the dependency field and the set of Top-K nearest neighbors retrieved from the existing valid nodes based on the semantic embedding vector. The system only calculates the explicit dependency validity, semantic similarity and edge weight wij for the candidate nodes. An edge is established when the edge weight is higher than the threshold θedge. For nodes that have neither explicit dependency relationship nor are included in the Top-K nearest neighbor set, edge weight calculation is not performed.

[0066] Compared to traversing the entire graph to calculate pairwise semantic similarity, the union of explicit dependency candidates and vector Top-K nearest neighbor candidates not only preserves semantic similarity edges without pre-embedded dependency fields, but also limits the computational scope of incremental graph construction to the candidate node set.

[0067] When the node size of the context dependency graph exceeds a preset threshold When the default value is 1000, delete nodes and their associated edges that meet all of the following conditions: popularity value h i Below (Default 0.2); Time decay exceeds the preset window (default 7 days); Not part of the hard-reserved set H.

[0068] In some implementations, the semantic embedding vectors and edge connections of the deleted nodes are aggregated into a super node. The semantic embedding vector of the super node is calculated by weighting the embedding vectors of all deleted nodes according to the popularity value of each deleted node. The edge weight between the super node and the retained nodes is the maximum value of the original edge weight. The super node is denoted as a gray node and is used only as a weak connection placeholder in subsequent dependency calculations. That is, it is only used to indicate that a deleted node once existed at this position to maintain the integrity of the graph topology. It does not participate in the pre-calculation of node isolation degree, nor is it used as a valid node for semantic similarity calculation.

[0069] Pre-calculate isolation degree for each node : ; in, For the diagram arrive The number of edges in the shortest path; if and If nodes are not connected, the path length is the maximum value of the graph diameter plus 1; the final isolation degree is normalized to the range [0,1], with higher values ​​indicating more isolated nodes. This isolation degree is used to guide priority pruning (prioritizing the pruning of non-hard-to-retain nodes according to their isolation degree from high to low), trigger active expansion or supplementation of context (when key dependency information is detected to be missing in the current context, nodes with lower isolation degree are located and the original text is loaded from the original context repository), and serve as a candidate basis for memory merging (when multiple high-isolation nodes are semantically similar, they are merged into a representative memory).

[0070] A weak connection placeholder indicates that a supernode in the context-dependent graph is only used to represent a previously existing, deleted node to maintain the topological integrity of the graph (i.e., preserving the connectivity between the deleted node's original neighbors). It does not participate in any actual computational operations, including node isolation pre-computation, semantic similarity calculation, popularity value propagation, or edge weight updates. The meaning of a weak connection is that although the node exists in the graph, it only serves as a structural placeholder and does not possess the functional attributes of an active node. When a new node is added subsequently, this supernode will not be considered a valid semantic neighbor for association.

[0071] In some implementations, the specific process for guiding priority pruning is as follows: when the size of the context dependency graph nodes exceeds a preset threshold... When the system sorts nodes by their isolation degree from high to low, it prioritizes pruning non-hard-retained nodes with high isolation degree (i.e., nodes that are sparsely connected to other nodes in the graph and located at the graph edge), because these nodes contribute the least to the overall context structure and pruning them has the least impact on the dependencies and semantic connectivity of the remaining nodes.

[0072] Triggering proactive context expansion or supplementation means that when the model detects missing key dependency information in the current context during inference, the system locates the node with low isolation (i.e., a node closely connected to other nodes in the graph and located in the central region of the graph) corresponding to that information, proactively loads the complete original text content of that node from the original context repository R0 and adds it to the active context pool A0 to supplement the context completeness. Furthermore, when two or more high-isolation nodes are semantically highly similar, the system uses them as candidate criteria for memory merging, that is, merging the memory entries corresponding to these nodes into a single representative memory to reduce redundant storage.

[0073] In some implementations, the current task stage is identified based on the Agent's running status. The Agent's running status includes: the current system controller status (such as initialization, planning, action execution, observation, reflection, output, etc.), the type of the most recent model output action (such as generating plan text, calling tools, generating final response, generating reflection summary, generating code, etc.), whether there are tools to be called, whether there are tool execution failures or exceptions, whether it is currently in the summary output stage, the current token budget balance, and the completion status of sub-goals in the current task chain (COMPLETED, IN_PROGRESS, FAILED, PENDING, etc.).

[0074] In some implementations, the task phases include a retrieval preparation phase (G1), a planning phase (G2), a tool invocation phase (G3), an execution phase (G4), a reflection and error correction phase (G5), and an output generation phase (G6). Based on the above operational status elements, the current task phase is determined according to the following rules: G1 is identified when the system is in a data retrieval or document loading state and has not yet invoked a model; G2 is identified when the controller status is PLANNING, or the most recent output is plan text and there are no tools to be invoked; G3 is identified when the model output contains the tool_calls field and the tool has not yet been executed; G4 is identified when the tool is executing, or the most recent output is the tool execution result and there are still pending actions; G5 is identified when the tool returns an error code, or the model output contains keywords such as retry, error, or failure; and G6 is identified when the model output is the final response, there are no pending tool actions, and there are no exceptions. When multiple conditions are met simultaneously, G5 is matched first; then, if there are incomplete tool actions, G3 or G4 is matched according to the invocation status; then G2 and G1 are matched; and G6 is matched only when there are no incomplete actions or exceptions.

[0075] Based on the identification result g, the corresponding target compression policy template Pg is matched. This template includes at least: compression granularity, retain rule, score weight parameter group, context budget allocation scheme, reversible compression mode identifier, memory write tendency, cache key formation rule, restore policy, and cache invalidation condition.

[0076] Different compression strategy templates are configured differently for different task stages: When the identification result is in the planning stage (G2), the compression granularity is configured at the paragraph or heading level. Hard retention rules prioritize retaining task objectives, constraints, completion criteria, global dependencies, and key decision points. The reversible compression mode is the standard mode. The cache key rule introduces at least taskGoalHash and dependencySig. The invalidation condition stipulates that cache invalidation is triggered when taskGoalHash or dependencySig changes. In the tool invocation stage (G3), the compression granularity is configured at the sentence or function signature level. Hard retention rules prioritize retaining tool name, parameters, schema, most recent observation, exception code, and dependency parameter chain. The reversible compression mode is strongly reversible. For the parameter part, the cache key rule must include at least the exactFlag and the most recent tool parameter signature. The expiration condition stipulates that cache expiration is triggered when the taskGoalHash, tool parameter signature, or schema version changes. The compression granularity configuration of the reflexive error correction phase (G5) is configured as error propagation path level or semantic fragment level. The hard retention rule prioritizes the retention of failure traces, error messages, attempt history, hypothesis bias, and evidence conflict items. The reversible compression mode is strongly reversible mode (fully enabled). The memory write tendency is high write tendency. The cache key rule must include at least the failure trace signature and error code signature. The expiration condition stipulates that cache expiration is triggered when the error code signature or failure trace signature changes.

[0077] In some implementations, based on the target compression strategy template and the context dependency graph, an importance score for each context object in the activity context pool at the current task stage is calculated. This score integrates multiple dimensions, and the calculation formula is as follows:

[0078] ; in, This represents the task relevance of the context object to the current task objective, based on the object. semantic embedding vector Embedded vector with the current task target Cosine similarity calculation: ; in, The content corresponding to taskGoalHash is obtained through embedding model encoding; The higher the value, the stronger the semantic association between the object and the current task target.

[0079] To represent the criticality of a context object in the context dependency graph, based on the constructed context dependency graph G, compute nodes... Degree centrality and betweenness centrality The weighted sum; ; in, The total number of nodes. ,default , ; The higher the value, the stronger the structural importance of the node in the dependency graph.

[0080] Indicates the timeliness of information, based on timestamps. With current time Calculation of the exponential decay function; ; in, This is the time-related decay factor (default 0.1, in days).

[0081] The source credibility is indicated by looking up the table based on the sourceType field to obtain the preset credibility. ; Example preset values: System message = 1.0, User message = 0.9, Search result = 0.7, Tool result = 0.8, History memory = 0.6.

[0082] Indicates the probability of future reuse, based on historical recall frequency. (Same heat value) and isolation Joint forecasts; ; in, The sigmoid normalization function. This represents the maximum number of recalls across all nodes. and This is the adjustment factor (the default value is 1.0).

[0083] Representing information novelty based on object embedding vectors Calculate the complement of the maximum cosine similarity between the embedding vectors of all memory items in the existing memory set M and the embedding vectors of all memory items in the existing memory set M; ; Where M is empty or the maximum similarity is below the threshold (default 0.5), then... .

[0084] The exact value is determined by the dynamically assigned exactFlag. ; Representing cross-modal alignment correlation, calculating the context object. Modal features and the modalities of interest in the current task Mutual information content; ; in, For mutual information functions, This is the normalization constant (defaulting to the logarithm of the modal feature dimension). For plain text scenarios, this value is set to 0.5 by default.

[0085] Represents the context object The normalized logarithm of the original token length is calculated using the following formula: ; in, The maximum number of object tokens allowed by the current compression strategy template; Constrained to the [0,1] interval, it maintains the same dimension as other normalized scoring dimensions while preserving the monotonicity of the length penalty; , , , , , , , and Represents dynamic weighting coefficients, satisfying The weight coefficients for each dimension change dynamically depending on the current task stage, and are continuously optimized through a feedback loop. For example, improving [something] during the planning stage. and Improve during the tool invocation phase and Improve during the reflection phase and .

[0086] In some implementations, based on hard retention rules, non-lossable context objects are preferentially selected from the activity context pool A0 to form a hard retention set H, and the remaining objects form a candidate compression set Q, with H and Q being mutually exclusive. Objects in H bypass the memory action decision and compression process in step S3 and are loaded only once by content type in step S5; objects in Q are used for subsequent memory decisions, hierarchical compression, summarization, or delayed loading. Hard retention rules include at least: information that the user explicitly requires cannot be omitted; the current task objective, constraints, and completion criteria; the input parameters and return key values ​​of the most recent tool call; unresolved exceptions, conflicts, and pending items; key evidence that exactFlag is true, code snippets, API contracts, test assertions, build parameters, exception stacks, and version numbers.

[0087] For candidate compression set The context object in the memory dynamically determines which memory operation to perform through a hybrid decision architecture that combines rule constraint layers with reinforcement learning policy networks, in order to update the multi-level memory set and control its quality.

[0088] In some implementations, context objects in the hard-retained set H are not included in the reinforcement learning policy network, are not written to the updated memory set M', and are not written to the reversible compressed package of candidate objects; the system only retains their original text references and hierarchical affiliations, which are directly loaded in step S5. Only objects in the candidate compressed set Q determine their memory operation actions through the hybrid decision architecture of this step.

[0089] In some implementations, memory is divided into the following five levels: M1 (Short-Term Running Memory): Stores the instantaneous state within the current task cycle, with a lifespan limited to a single inference process.

[0090] M2 (Stage Episodic Memory): Stores the complete execution trajectory of a specific task stage, with an effective retention period spanning multiple reasoning rounds of the same task.

[0091] M3 (Long-Term Semantic Memory): Stores abstract knowledge and facts across tasks and sessions, with an effective retention period across tasks and sessions, until the source version becomes invalid, a preset window is reached due to continuous non-recall, or consistency verification fails.

[0092] M4 (Tool Experience Memory): Stores the success and failure patterns of specific tool calls to accelerate subsequent tool selection.

[0093] M5 (Error Pattern Memory): Stores abnormal traces, conflict patterns, and correction history for quick retrieval during the reflection phase.

[0094] Bidirectional transfer is supported between memory levels. Specifically, items that are frequently recalled in M1 can be upgraded to M2; items in M2 that have been repeatedly verified as stable knowledge can be abstracted and written into M3; and experience items in M3 that have been modified multiple times can be downgraded to M5.

[0095] In some implementations, the set of memory operation actions A includes: writing to original memory (a1), writing to compressed memory (a2), merging with existing memory (a3), delayed writing (a4), deletion upon forgetting (a5), updating only the index (a6), memory upgrade (a7), and memory downgrade (a8).

[0096] A task round refers to a complete invocation process from when the model receives one input to when it produces a corresponding output. A single task round may involve multiple agent execution phases (such as planning, tool invocation, execution, reflection, etc.), but it usually does not span multiple rounds of dialogue or multiple rounds of tool invocation chains. A task round consists of one model inference request and its corresponding response.

[0097] Lifecycle refers to the effective time range during which a memory entry is retained in the system. The lifecycle of short-term running memory (M1) is limited to a single task round, meaning that once the model is invoked once, the memory entry is cleared or marked as invalid and does not continue to subsequent rounds; the lifecycle of staged episodic memory (M2) spans multiple reasoning rounds of the same task; and the lifecycle of long-term semantic memory (M3) is persistent and is retained across tasks and sessions.

[0098] In some implementations, tool selection refers to the process by which the Agent determines which tool or set of tools to call from among multiple available tools during the tool invocation phase (G3). Tool experience memory (M4) stores the success patterns (such as parameter configuration, invocation chain order, and return result characteristics) and failure patterns (such as common error codes, parameter combinations that trigger exceptions, and timeout scenarios) of each tool invocation in the past. This enables the Agent to prioritize tools and their parameter configurations with higher success rates when encountering similar task requirements in the future, avoiding repeated attempts at known failure paths, thereby accelerating the tool invocation decision.

[0099] An exception trajectory refers to a complete execution path record from the starting point to the end point of an error or exception, including: the operation or call that triggered the exception, the function call chain that the exception propagates through, the input parameters and return values ​​of each node, and the final exception type and error message.

[0100] In some implementations, conflict patterns refer to typical forms of logical inconsistencies that occur in the context or memory, including: the same constraint being assigned contradictory values ​​in different locations, circular dependencies in the same tool call chain, and logical violations between the current inference result and previously confirmed conclusions.

[0101] Correction history refers to a complete record of corrective actions taken in response to the above-mentioned anomalies or conflicts, including: correction time, correction method (such as overwrite, rollback, merge), comparison of content before and after correction, and whether the correction was successful.

[0102] In some implementations, the reinforcement learning policy network at time... status Includes: the current task phase The importance score of the context object is denoted as Information novelty Similarity to existing memories, calculated based on vector cosine similarity or locality-sensitive hashing; number of times the memory was recalled in the past; contribution to task success, based on weighted statistics of whether the task was successful when the memory was used in historical reasoning; conflict risk, based on logical consistency signature judgment; time decay value, based on the exponential decay of the timestamp and the current moment; compression cost (normalized weighted sum of compression time and peak memory during the compression process) and recovery cost (normalized weighted sum of on-demand recovery time, number of bytes read, and verification failure rate); cache hit probability, based on historical cache hit statistics; all features are normalized and concatenated into a fixed-dimensional vector.

[0103] To more accurately measure the long-term retention value of memory, this application introduces a recall estimation function based on time decay and recall behavior. : ; in, The time interval between the current time and the memory entry; This represents the number of successful recalls in the history of this memory item; The number of times it was merged; This represents the number of conflicts or corrections for that memory entry; This is a time decay coefficient, which can optionally be a stage-adaptive coefficient, with different coefficients used for different task stages. value. The higher the value, the greater the likelihood that the memory will be recalled in the future. The lower the value, the more likely the memory can be forgotten or only the index can be retained.

[0104] In some implementations, the reward function of the reinforcement learning policy network includes the following objectives, and normalization and dimensional alignment are performed on each metric to improve the stability and reproducibility of policy training:

[0105] ; in, This indicates an improvement in the success rate, accuracy, or completion rate of a task; This indicates the subsequent search hit rate, calculated based on Top-K recall and precision. The number of context tokens saved; This indicates the effect of compressed cache reuse; This represents the cost of incorrect reasoning due to improper compression or memory. Specifically, it is calculated based on the proportion of illusory content (i.e. content that is claimed to exist in the output but has no corresponding mapping relationship in the compressed package or is inconsistent with the original facts) in the model output to the total output tokens, and is combined with a weighted statistical analysis of the number of incorrect references found in subsequent automatic verification (such as consistency signature verification). This represents the difference between the time elapsed from triggering on-demand restoration to the completion of verification and the time elapsed when restoration is not performed, and is normalized according to a preset delay upper limit; It indicates the degree of noise memory accumulation, calculated based on the proportion of low-recall items to the total number of items.

[0106] In some implementations, the reinforcement learning algorithm specifically employs a dual deep Q network (DDQN), with an online Q network responsible for action selection and a target Q network responsible for target value evaluation, thereby reducing overestimation of action value and maintaining consistency with the dual-network update process described below.

[0107] The hybrid decision architecture, which combines a rule constraint layer with a reinforcement learning policy network, is as follows: The first layer is the rule constraint layer (rule sandbox). For each candidate object, it first determines whether it belongs to the category that cannot be decided by reinforcement learning according to preset rules. The input to the rule constraint layer is limited to objects in the candidate compression set Q; if an object is detected to belong to the hard retention set H, the action decisions from a1 to a8 are stopped and the object is returned to H, and then directly loaded in step S5.

[0108] like If exactlyFlag=1, then merge (a3) ​​and write to compressed memory (a2) are prohibited. If the current stage is a strongly reversible mode and If the code or API contract, test assertion, or build configuration is included, then forget deletion (a5) and writing to compressed memory (a2) are prohibited. In some implementations, if none of the above rules are triggered, the decision-making power is delegated to the second-layer reinforcement learning policy network.

[0109] The second-layer RL policy network (soft decision layer) is used to process candidates selected through the rule sandbox via online... Network output of each action Value, and based on - Greedy strategy selects the final action: ; ; in, To reinforce the learning of state vectors; For a set of memory operations; For online Network state Next action Value estimation; The exploration rate is initially set to 0.1 and decays to 0.01 with each training step. The agent uses... Probability choice The action with the highest value, The probability from Randomly select an action.

[0110] In some implementations, DDQN includes online... network ,Target network And the experience replay pool. Each interaction will... Write the data into the experience replay pool and randomly sample small batches of samples to update the online database. Network, in which This indicates a termination status flag.

[0111] ; Among them, online network Choose the optimal action in the next state, the goal is... network Assess the value of this action; Discount factor, default 0.95; Termination status. At that time, the target value Equivalent to instant reward .

[0112] Online The network updates its parameters by minimizing the following temporal difference loss. : ; Target network parameters Every Step from online network parameters Synchronize once, The default is 100 steps; the target network parameters remain fixed between two synchronizations.

[0113] In some implementations, online Network and Target The networks use the same structure: the input layer receives the state feature vector. The hidden layers are two fully connected networks with 256 and 128 neurons respectively, activated by ReLU; the output layer is a linear layer that processes action sets. In to Output one respectively Values, without using softmax probability output.

[0114] The third layer: the post-processing verification layer, performs a second security check on the actions output by the reinforcement learning policy network layer. If the selected action will lead to high-value memories (recall count) Threshold If a memory is forgotten and the current stage is the planning or reflection stage, it will be forcibly downgraded to a4 (delayed writing) instead of a5 (forgotten deletion); if the selected action will merge two conflicting memories (conflict level)... Threshold If the index is not updated, a manual verification flag will be triggered or the index will be downgraded to a6 (index only).

[0115] In some implementations, memory operation actions are output based on a hybrid decision architecture, specifically: When the information is of high value, novelty, and has a high probability of reuse, execute a1 (write to original memory) or a2 (write to compressed memory), and store the memory item together with the current task stage, timestamp, and dependencies. When information is highly similar to existing memories, perform a3 (merge operation) to form memory clusters, retaining representative samples and differential summaries within the clusters; When information is effective in the short term but has low long-term value, execute a4 (delayed write), temporarily store the memory in the write queue, and automatically downgrade to a5 (forget) if it is not triggered again within the preset time window. When information is outdated, has a low recall probability, or has too many conflicts, execute a5 (forgotten deletion) or downgrade retention (migrate from M2 / M3 to M1 / M5).

[0116] When the value of information cannot be determined, execute a6 (update index only), which writes metadata to the index but does not store the complete content. This metadata will then be retrieved from the original context repository when the information is subsequently retrieved. Lazy loading; When a memory is frequently and successfully recalled, execute a7 (memory upgrade), which migrates from M1 to M2 or abstracts from M2 and writes it to M3. When a memory is not recalled for a long time or conflicts with a large amount of new information, execute a8 (memory downgrade), migrate it from M3 to M5 or mark it as to be forgotten.

[0117] After this step, an updated memory set is generated. Update the recall count for each memory item. Number of merges Number of conflicts And timestamp.

[0118] In some implementations, such as Figure 2 As shown, this step only performs hierarchical reversible compression on the context objects in the candidate compression set Q, forming a compact expression for model inference and an index mapping for accurate recovery, provided that the token budget of the current stage is met. The standard mode implements system-level reversible recovery that depends on the immutable original context repository R0, while the strongly reversible mode implements self-contained recovery within the compressed package.

[0119] Importance score based on calculation The target compression ratio for each context object is determined using a score-compression ratio mapping function. : ; in, Set to the preset minimum compression ratio (retaining the most content); For maximum compression (retaining minimal content); The sigmoid normalization function maps the importance score to the [0,1] interval; The higher the compression ratio The lower the value (the more content is retained); The lower the compression ratio The higher the value, the more content is discarded.

[0120] In some implementations, the mapping function is also subject to the current stage budget constraint. Correction: If the current used token budget exceeds the planned value, then for all... Multiplied by compression enhancement factor ( To further reduce overall occupancy; if the budget is sufficient, multiply by ( (to retain more details).

[0121] For each object to be compressed Its original token sequence is denoted as The compression generates a reversible compressed file. , It should include at least the following six types of fields: A collection of key tokens or key fragments stored in the original sequence order, where each fragment contains a unique identifier (anchorId) and fragment content; A mapping table consisting of the anchorId of each anchor segment, the original interval [start, end], the predecessor / successor segment identifier, and the cross-segment jump pointer; : A differential byte string stored segment by segment according to the omitted fragments, or a raw pointer consisting of a rawPointer, the original version number, and the start and end offsets; : An ordered table recording the original sequence numbers of each Anchor segment and Residual segment; Records the structure type, hierarchical path, and field path of each fragment, used to restore the original hierarchy; The digest and hash algorithm identifier of the normalized original object calculated by SHA-256 are used for byte-by-byte consistency verification after recovery.

[0122] In some implementations, reversible compressed packages Supports multi-level compression modes: Standard compression mode: Anchori preserves semantic key points, and Residuali stores at least the rawPointer, source version number, start and end offsets of the omitted fragments, and Checksumi; during restoration, the corresponding version of the original text fragment is loaded from the immutable original context repository R0, which is a system-level reversible mode that depends on R0; Strongly reversible compression mode: Anchori preserves the token-level key sequence, Residuali stores the complete differential encoding of all omitted content, and SpanMapi records the precise index per token, enabling the reversible compressed package to independently reconstruct the original object without relying on R0. It is suitable for high-precision scenarios in intelligent software development and maintenance such as code snippets, API contracts, test assertions, build configurations, exception stacks, and version numbers.

[0123] The selection of compression mode is determined by the reversibleMode flag output by the strategy template and the dynamically assigned exactFlag: when reversibleMode is strong reversible or exactFlag=1, strong reversible compression mode is forcibly enabled.

[0124] In some implementations, the hierarchical reversible compression operation is strictly aligned with the multi-granularity segmentation structure to ensure that the compressed fragments are traceable back to the original segmentation unit, including the following levels: First-level fragment-level filtering: only for the candidate compression set Q, based on the target compression ratio. For each segmentation unit (at the smallest granularity), a three-category decision of retain / compress or discard is performed, where discarding refers to omitting the payload and does not indicate deletion or recovery of information. For each segmentation unit in the candidate compression set Q, the unit is scored based on its local importance (as determined by its context object). (and normalized logarithmic weighted calculation of task similarity, entity or keyword hit rate, evidence credibility, structural role weight, and token length) and target compression rate. Make one of the following three choices: Reservation: When the local importance score is above the first threshold (At the default value of 0.7), this unit is not compressed and is retained in its entirety; Compression: When the local importance score is between the first threshold With the second threshold When the value is between 0.3 (default), perform intra-cell compression of the second level on that cell; Load omission: When the local importance score is lower than the second threshold θdiscard (default 0.3), the unit is only omitted from the model inference load, but the recovery information is retained; the strong reversible mode writes the complete residual of the unit into the reversible compressed package, and the standard mode writes the rawPointer, source version number, start and end offsets and check value into the reversible compressed package.

[0125] Among them, the first threshold Second threshold Adjust dynamically based on the current task phase and token budget.

[0126] In some implementations, compression within the second-layer cell: based on the "compression" determination result in the first layer (i.e., the local importance score is between...). and (Units between), content compression is performed using at least one of the following methods: Retain key sentences, key fields, key lines of code, key parameters, and key evidence anchors, and remove redundant descriptions, duplicate observations, and low-relevance background information; For long documents, retain chapter titles, paragraph first sentences, and core data points, and record their original position indexes; Multiple rounds of repeated descriptions are merged into a single structured expression, recording the number of repetitions and the first occurrence position, and maintaining cross-round reference relationships; For structured data, retain only the key fields and values, delete default values ​​and auxiliary fields, and retain the field-level position and source mapping; For consecutive similar content (such as log sequences and sensor data), retain the difference (diff) between the first and subsequent items instead of storing the complete data.

[0127] Third-level cross-unit reference compression: for cases where dependencies exist ( For multiple partitioned units (related to field records), perform cross-unit reference compression: If the content of unit B is highly similar to that of unit A (similarity > threshold) If B depends on A, then B only stores a reference pointer to A and a difference patch, and does not store the complete content; during restoration, the content of A is located according to the reference pointer, and the difference patch is superimposed to rebuild B.

[0128] In some implementations, in step S4, the encoding method of the residual storage or original pointer of the reversible compressed package is adaptively selected according to the content type and compression mode: For natural language text, the standard compression mode uses the original text pointer plus the start and end position offsets of the deleted segment, while the strongly reversible compression mode uses differential coding plus sentence-by-sentence position mapping.

[0129] For code, the standard compression mode uses the original pointer plus the line number range of the deleted code line, while the strongly reversible compression mode uses token-by-token differential encoding plus AST node position mapping.

[0130] For structured data (JSON / XML), the standard compression mode uses the XPath path of the deleted field plus a default value marker, while the strongly reversible compression mode uses field-level position mapping plus a value change log.

[0131] For image or audio descriptions, the standard compression mode uses a modal pointer plus a description fragment index.

[0132] For log or time-series data, differential encoding (first item plus difference sequence) is used in standard compression mode.

[0133] When it is necessary to restore the original content, refer to the contents of the compressed file. , , and Reconstruct the original sequence: ; in, This represents the original token sequence obtained after recovery.

[0134] In some implementations, the recovery process supports multi-granularity recovery: Restore only the requested specific fragment (such as a line of code or a clause), rather than the entire object, by... Locate the storage location of the target fragment within the compressed file, from Extract the corresponding residual and... Return after concatenating the relevant content; restore the complete original object, by... The order of the records will Fragments and The residual fragments in the data are spliced ​​together sequentially, through... Restore the structural hierarchy.

[0135] After the recovery is complete, via Perform a consistency check. Calculate the hash value of the recovered content. ;like If the verification passes, the restored content is confirmed to be consistent with the original content; if the verification fails, the process can be rolled back to the original context repository. The corresponding rawPointer points to the original text version and records the verification failure event, which is used to trigger a safety rollback and strategy optimization.

[0136] In some implementations, integrity checks are performed on the generated residuals and recovery indexes, including residual data integrity, index integrity, and hierarchical consistency. If the checks fail, error handling is triggered, including rollback, recompression, or repair of the indexes and residuals to ensure that the compressed package is recoverable.

[0137] After completing layered compression, reversible encoding, residual generation, and consistency verification, Anchori, SpanMapi, Residuali, Posi, Structi, Checksumi, rawPointer, compression mode identifier, source version number, and data structure version number are integrated to form the final layered reversible compressed package.

[0138] This compressed package supports cache reuse, incremental updates, and on-demand expansion and recovery; the strongly reversible mode independently and accurately reconstructs the original context based on the complete residual within the package, while the standard mode loads the matching source version from the immutable original context repository R0 based on the rawPointer and accurately reconstructs it after verification by Checksumi.

[0139] During model generation, tool invocation, or result verification, on-demand expansion is triggered when any of the following conditions are met: the current output requires citing original evidence; the confidence level of the model output is lower than a preset threshold; a key parameter conflict is detected based on consistencySignature; the current task is marked as a high-precision task for intelligent body software R&D and maintenance (such as code generation, code repair, automated test generation, build deployment diagnosis, or defect localization); or the user requests to view the original text, original code, or original records.

[0140] On-demand expansion can be either object-level expansion (expanding the entire object) or fragment-level expansion (expanding only the requested specific fragment), with the expansion depth determined by the triggering conditions and the current token budget. The expanded content is temporarily added to the activity context pool A0, but the original compressed file is not modified. This is to ensure the stability and reusability of the compression results.

[0141] In some implementations, the specific process of generating the composite cache key in step S5 is as follows:

[0142] ; in, Represents the original content The pre-computed hash value; Indicates the version of the word segmenter; Indicates the model version; Indicates the compression strategy version; increments when there are actual changes to the stage template score weights, budget schemes, reward function parameters, rule constraints, or DDQN network parameters. Indicates the task phase identifier; The `<context>` element represents the domain identifier; `taskGoalHash` represents the hash value of the task objective, user explicit constraints, and system hard constraints after normalization encoding; `budgetBand` represents the discrete budget interval identifier to which the current token budget belongs; `exactFlag` represents a flag indicating whether exact preservation or strong reversibility is required; `dependencySig` represents the dependency graph signature formed by the set of dependency edges and edge weights associated with this context object; and `schemaVer` represents the data structure version of the context object, compressed package, and cached entries. The formula contains... This represents the canonical byte string concatenation operator. Before generating the cache key, the system performs canonical encoding on the fields involved in the calculation. Canonical encoding includes field name sorting, null value placeholders, character set unification, floating-point or weighted quantization, time field normalization, and permission scope writing. The concatenation between fields uses length prefix encoding or equivalent deterministic structured encoding.

[0143] cache key During the generation process, the original content The hash value is directly reused from the pre-computed hash value. Fields to avoid duplicate calculations; , , Read from system configuration. Determined by the current stage g, Extract from context metadata.

[0144] In some implementations, The corresponding compressed file, rating result, version number, and validity label are stored in the cache library.

[0145] If two context blocks have the same original content but different taskGoalHash, budgetBand, exactFlag, or dependencySig, then entering the exact match channel is prohibited; incremental compression based on historical compressed packages is only allowed when the approximate channel meets the similarity threshold and passes version verification. This constraint prevents the same text from being misused as the same compression result at different task stages or under different budget ranges.

[0146] When a new input arrives, the system first uses the cache key. The system queries the cache; if a match is found, the compressed package and its verification information are reused directly.

[0147] In some implementations, if If they are completely identical, then the corresponding compressed file and scoring results can be reused directly; if... If no match is found, but the semantic similarity or locality-sensitive hash matching degree is higher than the threshold, it is considered an approximate match, and the incremental reuse process is initiated, and a historical version reference pointer is bound. If no reusable cache exists (neither an exact match nor an approximate match), a new full compression process is executed, and the compression result is written to the cache library, while the strategy version and model version are recorded.

[0148] When reusing the cache from an exact hit channel, the system still needs to verify the following conditions: current With cache records Consistent; Current With cache records Consistent; current With cache records Consistent; in the current stage, g matches the cached record. Match; compressed file of Consistent with expectations.

[0149] If any verification fails, the process is downgraded to either an approximate hit channel or a full compression process.

[0150] In some implementations, for a new context that is highly similar to historical content, the differences between it and historical versions are calculated. Only for Perform scoring, compression, and index mapping, and merge it with the original compressed file to form an updated compressed file. .

[0151] Incremental compression includes the following sub-steps: Rolling hashing (such as the Rsync algorithm) is used to locate unchanged segments and can be combined with dependency graph G to identify associated nodes affected by changes, thus narrowing the difference search space. Rolling hashing uses the segmentation unit in step S1 as the basic operation unit. Within each segmentation unit, hash values ​​are further calculated in a fixed window to locate the changed positions within the unit. If the entire segmentation unit remains unchanged, the entire unit does not participate in incremental compression.

[0152] based on Algorithms or editing scripts (such as Myer'sdiff algorithm) extract newly added, deleted, and modified fragments and generate difference descriptions. .

[0153] Only for For newly added or modified segments, the importance scoring process is reused for calculation. .

[0154] Based on the score-compression ratio mapping function and the hierarchical compression strategy, Perform compression to generate incremental compressed packages. .

[0155] Will In and Merge with the corresponding structure of the historical compressed package, and update the position offset and reference pointer.

[0156] Will With historical compressed files Merge to form an updated compressed file And increment the version number.

[0157] renew Restore the verification mechanism.

[0158] In some implementations, cache entries are invalidated, rebuilt, or downgraded in the following situations: Original document version changes or original content hash changes; Version changes; changes in the large model version or inference kernel; changes in the compression strategy version policyVer; when changes occur in the stage template scoring weight or budget scheme, reward function parameters, rule constraints or DDQN network parameters, the system first increments policyVer and then invalidates the cached entries of the old policyVer; there is a risk of verification failure, recovery anomaly or cross-version inconsistency.

[0159] By using the above mechanism and combining weighted LRU-LFU with compression cost factors for eviction control, the correctness, consistency and resource efficiency of cache reuse are guaranteed.

[0160] In some implementations, step S5 assembles the hierarchical context package according to the following priority: objects in H are mapped to a corresponding layer of L1, L2, or L4 based on their content type and are loaded only once; M' and RCI both contain only the processing results of Q: The L1 system constraint layer contains the Agent identity, permission scope and responsibility boundaries, security boundaries and output format specifications defined in the system messages. It consists of system-level constraint items in the hard-reserved set, has the highest priority and does not participate in compression. The L2 task target layer contains the user's original intent, task completion criteria, and explicit requirements. Task target class objects that hit hard-reservation rules are fully loaded from H only once in this layer, and the rest of the content is extracted from the activity context pool A0. If necessary, the original text is loaded from R0 through rawPointer. The L3 core active memory layer contains highly relevant memory entries retrieved from the updated memory set M' based on the current task stage; M' contains only the memory decision results of Q and excludes objects that have been loaded into L1, L2 or L4 by H; The L4 layer, which includes tool calls, critical observations, and precise retention, contains the input parameters, return key values, status codes, and exception information of the most recent tool call, as well as key evidence, code snippets, API contracts, test assertions, build parameters, exception stacks, or version numbers that do not fall into L1 or L2 but meet the hard retention rules. Objects in H are loaded completely only once in this layer and are not compressed, while other utility classes or high-precision objects in Q are compressed and loaded using a strongly reversible mode. The L5 compressed evidence layer contains only the set of key fragments from the reversible compressed package generated from the candidate compressed set Q, and the retention ratio is determined according to the score-compression ratio mapping function. The L6 reversible index appendix layer contains reversible compressed packages, as well as trigger conditions and rollback pointers for on-demand expansion. The on-demand expansion trigger conditions are not encoded in the cache key because they are runtime policy behaviors and do not affect the correctness of the compression results. The cache only stores the compressed data itself, and the trigger conditions are determined in real time by the context assembly and feedback optimization module of this step based on the current stage and monitoring status. The content of this layer is only accessed when on-demand expansion needs to be performed and does not participate in the token calculation of regular inference.

[0161] When the accumulated number of tokens exceeds the current stage's budget limit, secondary compression will begin from layer L5. Layers L1 to L4 will not participate in secondary compression.

[0162] In step S5, the feedback information collected based on the task execution results includes the following triggering conditions and corresponding operations: When the model output confidence score is lower than a preset threshold, fragment-level on-demand expansion is triggered. When the tool returns an error code or exception, the failure trajectory is written to the error mode memory and the context object level is triggered to expand as needed; When the model output claims to reference something but there is no corresponding mapping in the compressed package, a consistency check is triggered. If the check fails, the original text in the original context repository is rolled back. In some implementations, the parameter values ​​in the current inference result are compared with the historical values ​​of the same parameter recorded in the context dependency graph. If the two are different and cannot coexist logically, it is determined to be a parameter conflict. When a parameter conflict is detected, the on-demand expansion of the corresponding fragment is triggered and the conflict record is written into the error mode memory. When the cache misses multiple times in a row, the cache preheating mechanism is triggered: the system proactively compresses and writes the context objects most likely to be used in the current task stage (the top K objects sorted by importance score, with the K value dynamically determined based on the currently available computing resources) into the cache.

[0163] In some implementations, step S5, dynamically adjusting the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture, includes: After the task is completed, the following metrics are summarized as the basis for updating the reinforcement learning policy network and the stage compression policy template: The task success rate, i.e. whether the task objective has been achieved (with a value of 0 or 1), is used to update the reward function of the reinforcement learning policy network.

[0164] The answer accuracy rate, or the proportion of correct answers (ranging from 0 to 1), is used to update the reward function of the reinforcement learning policy network.

[0165] The token saving rate, which is 1 minus the ratio of the number of tokens after compression to the original number of tokens, is used to update the score weight parameter group and score-compression rate mapping function in the compression strategy template.

[0166] Cache hit rate, which is the ratio of the number of hits to the total number of queries, is used to update the weights of cache reuse and cache eviction policies.

[0167] The number of recovery attempts, i.e. the total number of times the data is expanded as needed, is used to update the budget allocation scheme in the compression strategy template. When the number of recovery attempts is high, the budget ratio of the compressed evidence layer is increased.

[0168] User revision count, i.e. the number of times a user modifies the model output, is used to provide a negative reward signal to the reward function of the reinforcement learning policy network.

[0169] Based on the above metrics, the system dynamically updates the scoring weight parameters in the compression strategy template. , , …, The budget allocation plan is used to update the reward function parameters of the reinforcement learning policy network. , , …, Whenever the aforementioned weights, budget schemes, or reward parameters actually change, policyVer is first incremented and the old version of the cached entries is invalidated before the new policy is enabled, forming a continuous optimization loop.

[0170] In another embodiment, this invention proposes a stage-aware large model context reversible compression and caching system to implement the stage-aware large model context reversible compression and caching method described above, including: The context acquisition and structured management module is used to acquire raw software development and operation context data and uniformly represent it as a collection of context objects. It performs structured segmentation and semantic annotation on the context objects, establishes a collaborative storage mechanism between the raw context repository and the activity context pool, and constructs a context dependency graph. The task phase identification and compression strategy selection module is used to identify the current task phase based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, calculate the importance score of each context object in the current task phase based on the target compression strategy template and the context dependency graph, and divide the context objects into a hard retention set and a candidate compression set according to the importance score and hard retention rules. The memory scheduling and compression module is used to output memory operation actions for context objects in the candidate compression set through a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network, and update the memory set according to the memory operation actions; according to the target compression policy template and importance score, it performs hierarchical reversible compression on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; The cache management module is used to generate cache keys based on the feature information of each context object. If the cache key matches, the cache is directly reused. If the semantic similarity is higher than the preset threshold, the difference is incrementally processed based on the historical compressed package and then reused. If the cache is not hit, full compression is performed and written to the cache. The context assembly and optimization module is used to assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, which is then input into the large model to execute the current task. During and after the task execution, feedback information is collected based on the task execution results to dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

[0171] In another embodiment of the present invention, an electronic device is proposed, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements a phase-aware large model context reversible compression and caching method as described above.

[0172] In another embodiment of the present invention, a computer-readable storage medium is provided storing a computer program that causes a computer to execute a phase-aware large model context reversible compression and caching method as described above.

[0173] In the embodiments disclosed in this application, a computer storage medium may be a tangible medium that may contain or store programs for use by or in conjunction with an instruction execution system, apparatus, or device. The computer storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of computer storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0174] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Any simple modifications, equivalent substitutions, and improvements made by those skilled in the art to the above embodiments without departing from the scope of the technical solution of the present invention, based on the technical essence of the present invention, shall still fall within the protection scope of the technical solution of the present invention.

Claims

1. A phase-aware large model context reversible compression and caching method, characterized in that, Includes the following steps: S1: Obtain the original software development and operation context data and represent it as a unified set of context objects; perform structured segmentation and semantic annotation on the context objects; establish a collaborative storage mechanism between the original context repository and the activity context pool; and construct a context dependency graph. S2: Identify the current task stage based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, and calculate the importance score of each context object in the current task stage based on the target compression strategy template and the context dependency graph. Based on importance scores and hard retention rules, the context objects are divided into a hard retention set and a candidate compression set; S3: For context objects in the candidate compression set, a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network outputs memory operation actions, and the memory set is updated according to the memory operation actions; according to the target compression policy template and importance score, hierarchical reversible compression is performed on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; S4: Generate cache keys based on the feature information of each context object; If the cache key matches, the cache is reused directly. If the semantic similarity is higher than the preset threshold, the difference is processed incrementally based on the historical compressed package and then reused. If the cache is not hit, full compression is performed and written to the cache. S5: Assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, input it into the large model to execute the current task; during and after the task execution, collect feedback information based on the task execution results, and dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

2. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S1, the structured segmentation and semantic annotation of the context object includes: determining the segmentation granularity parameters and segmentation strategy according to the current task stage or the predicted task stage, performing differentiated segmentation on context objects of different modalities, wherein the modalities include natural language text, code, tool return results, image data and audio data; each segmentation unit records its aggregation mapping relationship and splitting mapping relationship with the previous level granularity and the next level granularity, forming a multi-granularity hierarchical index.

3. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S1, the specific process of constructing the context dependency graph is as follows: A context dependency graph is constructed using each context object as a node and the temporal adjacency, semantic similarity, causal association, reference relationship, tool call chain relationship or code dependency relationship between context objects as edges; each node maintains a popularity value, which increases when the node is successfully recalled and is periodically updated according to a time decay function; When the number of nodes in the context dependency graph exceeds a preset threshold, delete nodes and their associated edges that meet the following conditions: their popularity value is lower than the preset threshold, their time decay exceeds the preset window, and they do not belong to the hard-retain set.

4. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S2, the calculation of the importance score of each context object in the current task stage based on the target compression strategy template and the context dependency graph specifically includes: The importance score is calculated by weighting the context object's relevance to the current task objective, the criticality of the context object in the context dependency graph, information timeliness, source credibility, future reuse probability, information novelty, precise retention of tags, cross-modal alignment relevance, and normalized logarithmic token length. The current task phase includes the retrieval preparation phase, planning phase, tool invocation phase, execution phase, reflection and error correction phase, and output generation phase; the Agent's running status includes the current system controller status, the type of the most recent model output action, whether there are tools to be invoked, whether there are tool execution failures or anomalies, whether it is currently in the summary output stage, the current token budget balance, and the completion status of sub-goals in the current task chain.

5. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S3, the memory operation actions include writing to the original memory, writing to the compressed memory, merging with existing memory, delayed writing, deletion upon forgetting, updating only the index, memory upgrade, and memory downgrade; The reversible compressed package contains a set of key segments, a mapping relationship between key segments and the original interval, the complete residual of the omitted segments, or a pointer to the original text of the immutable original context repository, the source version number and start and end offsets, and a checksum. In strongly reversible mode, recovery is achieved independently by relying on the complete residual within the package. In standard mode, recovery is achieved by relying on the original text pointer to load the corresponding version from the immutable original context repository. On-demand expansion is supported to restore the original fragment or the original context object.

6. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S3, the hybrid decision architecture combining the rule constraint layer and the reinforcement learning policy network is specifically as follows: The first layer is the rule constraint layer, which is used to perform hard filtering on candidate context objects according to preset security rules and force the output of the corresponding memory operation actions; The second layer is the reinforcement learning policy network layer, which is used to output the Q value of each memory operation action by the online Q network for the candidate context objects filtered by the rule constraint layer and select the final action according to the ε-greedy policy; The third layer is the post-processing verification layer, which performs secondary security verification on the actions output by the reinforcement learning policy network layer. If the selected action will cause high-value memories to be forgotten and the current stage is the planning stage or the reflection stage, it will be forcibly downgraded to delayed writing; if the selected action will merge two conflicting memories, it will trigger manual verification marking or downgrade to only updating the index.

7. The method for reversible compression and caching of large model context with stage awareness according to claim 1, characterized in that: In step S4, generating a cache key based on the feature information of each context object specifically involves: performing normalized encoding on the feature information of each context object and concatenating it into a byte string, then performing a hash operation on the byte string to obtain a composite cache key; the feature information of each context object includes: original content hash, tokenizer version, model version, compression strategy version, task stage identifier, task target hash, budget interval identifier, precise retention flag, dependency graph signature, and data structure version number; the normalized encoding includes field name sorting, null value placeholders, character set unification, and floating-point quantization.

8. A stage-aware large model context reversible compression and caching system, used to implement the stage-aware large model context reversible compression and caching method as described in any one of claims 1 to 7, characterized in that, include: The context acquisition and structured management module is used to acquire raw software development and operation context data and uniformly represent it as a collection of context objects. It performs structured segmentation and semantic annotation on the context objects; establishes a collaborative storage mechanism between the raw context repository and the activity context pool; and constructs a context dependency graph. The task phase identification and compression strategy selection module is used to identify the current task phase based on the Agent's running status, match the corresponding target compression strategy template based on the identification result, and calculate the importance score of each context object in the current task phase based on the target compression strategy template and the context dependency graph. Based on importance scores and hard retention rules, the context objects are divided into a hard retention set and a candidate compression set; The memory scheduling and compression module is used to output memory operation actions for context objects in the candidate compression set through a hybrid decision architecture combining rule constraint layer and reinforcement learning policy network, and update the memory set according to the memory operation actions; according to the target compression policy template and importance score, it performs hierarchical reversible compression on the context objects that need to be retained in the candidate compression set to generate a reversible compressed package; the reversible compressed package contains recovery information for restoring the original content, and supports expansion as needed to restore the original fragment or the original context object; The cache management module is used to generate cache keys based on the characteristic information of each context object; If the cache key matches, the cache is reused directly. If the semantic similarity is higher than the preset threshold, the difference is processed incrementally based on the historical compressed package and then reused. If the cache is not hit, full compression is performed and written to the cache. The context assembly and optimization module is used to assemble the hard-preserved set, the updated memory set, and the reversible compressed package into a hierarchical context package, which is then input into the large model to perform the current task. During and after the task execution, feedback information is collected based on the task execution results to dynamically adjust the weight parameters of the compression strategy template and the parameters of the hybrid decision architecture.

9. An electronic device, characterized in that, include: The memory, the processor, and the computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, it implements a stage-aware large model context reversible compression and caching method as described in any one of claims 1 to 7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program causes the computer to execute a phase-aware large model context reversible compression and caching method as described in any one of claims 1 to 7.