An embedded AIAgent three-layer memory management and context retrieval method and system
By adopting a three-layer memory management architecture and a multi-dimensional importance scoring mechanism, the tight coupling problem between memory management and the ReAct decision loop in embedded AI Agents is solved, improving retrieval efficiency and storage efficiency, and supporting long-term autonomous operation and maintenance of embedded devices.
Patent Information
- Application Number
- CN202611160329.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-03
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2046-08-03
AI Technical Summary
In embedded AI agents, existing technologies lack lightweight memory management solutions suitable for extremely memory-constrained environments. This results in a lack of tight coupling between memory management and the ReAct decision loop, low retrieval efficiency, high-value historical experience being squeezed out by low-value redundant memory, and a lack of fault mode accumulation and reuse mechanisms, making it impossible to effectively support long-term autonomous operation and maintenance in unattended scenarios.
A three-layer memory management architecture is adopted, including a short-term memory circular buffer, a long-term memory persistent database, and a reflective memory circular buffer. Combined with multidimensional importance scoring and fault mode memory mechanism, it realizes automatic classification and storage of memories and on-demand retrieval. Storage space utilization is optimized through parallel retrieval and adaptive eviction mechanism.
The tightly coupled integration of the ReAct loop was achieved with an extremely low memory budget, which improved retrieval response speed and storage efficiency, significantly enhanced fault diagnosis and processing efficiency, ensured the long-term retention of high-value historical experience, and supported the long-term autonomous operation and maintenance of embedded devices.
Smart Images

Figure CN122673347B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, specifically to an embedded AI agent three-layer memory management and context retrieval method and system. Background Technology
[0002] With the rapid development of artificial intelligence technology, AI Agents are gradually being introduced into the field of communication equipment operation and maintenance. They achieve closed-loop operation and maintenance through the ReAct (Reasoning + Acting) decision-making loop, enabling autonomous perception, reasoning, decision-making, and execution. In the ReAct loop, the Agent needs to continuously refer to historical memory for reasoning and decision-making; therefore, the memory system constitutes a critical infrastructure for the long-term autonomous operation of the AI Agent. However, in embedded scenarios such as intelligent operation and maintenance of small base stations, the memory budget of RU Agents for EU devices (ARM / RISC-V architecture, maximum 128MB memory) and RU devices (maximum 128MB memory, at least 16MB) is strictly limited to within 5MB. Under such extremely limited resource conditions, traditional memory management solutions based on general-purpose databases, caching systems, or vector databases cannot be deployed due to excessive memory consumption. Therefore, a lightweight memory management solution with extremely low resource consumption, efficient retrieval, and intelligent replacement is needed.
[0003] Existing technologies suffer from the following shortcomings: They lack a lightweight three-level memory architecture suitable for embedded environments, resulting in mixed storage of high-value long-term experience and low-value instantaneous data, leading to low retrieval and storage efficiency; memory management and the ReAct decision loop lack tight coupling integration, with memory writing, retrieval, and eviction operating independently of the Agent's decision loop, causing memories generated in each stage (Observe→Reason→Act→Feedback) to be unable to be automatically categorized, stored, and retrieved on demand; they lack experience accumulation and reuse mechanisms oriented towards failure modes, failing to summarize successful processing experiences into reusable failure modes; and they lack a hybrid storage strategy combining memory and flash memory, along with an adaptive eviction mechanism, causing low-value redundant memories to crowd out limited storage resources, and high-value historical experience to be prematurely overwritten. Summary of the Invention
[0004] The purpose of this invention is to provide an embedded AI Agent three-layer memory management and context retrieval method and system to solve the problems mentioned in the background art, such as low retrieval efficiency and storage efficiency, lack of tight coupling and integration between memory management and ReAct decision loop, and lack of experience accumulation and reuse mechanism for failure modes.
[0005] To achieve the above objectives, the present invention adopts the following technical solution: According to one aspect of the present invention, a three-layer memory management and context retrieval method for embedded AI agents is provided, the method comprising: Initialize a three-layer memory storage structure, which includes: a short-term memory circular buffer, a long-term memory persistent database, and a reflective memory circular buffer; In each stage of the ReAct decision loop, memory entries are automatically routed to the corresponding memory layer for storage based on memory type, and importance scores are calculated. In the Reason phase of the ReAct loop, historical memories are retrieved in parallel from the short-term memory circular buffer, the long-term memory persistent database, and the reflective memory circular buffer. The retrieval results are merged, deduplicated, and truncated according to a preset size threshold, and the reasoning context is output. When the number of entries in the long-term memory persistence database exceeds the capacity limit or the storage medium space is insufficient, memory entries are deleted and storage space is reclaimed according to the importance score. Similar events within consecutive time windows in the long-term memory persistence database are merged into summary records.
[0006] Based on the aforementioned scheme, the automatic routing of memory entries to the corresponding memory layers for storage according to memory type includes: observation-type memories and reasoning-type memories are routed to the short-term memory circular buffer; action-type memories and system-type memories are simultaneously routed to the long-term memory persistence database and the short-term memory circular buffer; and feedback-type memories are routed to the reflective memory circular buffer.
[0007] Based on the aforementioned scheme, the calculation dimensions of the importance score include: memory freshness, anomaly severity, decision impact, and access frequency, wherein the freshness is calculated based on the memory age according to an exponential decay relationship, the anomaly severity is determined based on the degree of anomaly in the observed data, the decision impact is determined based on the action execution result, and the access frequency is determined based on the number of times the memory is retrieved.
[0008] Based on the aforementioned scheme, the method further includes: In telecommunications equipment operation and maintenance scenarios, when an abnormal telecommunications indicator is detected, the observation memory containing a telecommunications fault type flag is written to the short-term memory circular buffer. In the Reason phase, based on the currently detected telecommunications fault type, historical processing records of the same telecommunications fault type are retrieved from the long-term memory persistent database, the time series of indicators before and after the fault occurrence are extracted from the short-term memory circular buffer, relevant fault pattern summaries are retrieved from the reflective memory circular buffer, and the retrieval results are weighted by relevance and then merged into the reasoning context. During the Feedback phase, after successful fault handling, the telecommunications fault modes are summarized, the fault mode summary is written into the reflective memory circular buffer, and the complete fault mode record is written into the long-term memory persistent database.
[0009] Based on the aforementioned scheme, the relevance weighting of the search results includes: multiplying historical memories that completely match the current fault type by a first weighting coefficient, and multiplying memories with importance scores higher than a first threshold by a second weighting coefficient.
[0010] Based on the aforementioned scheme, the step of merging and deduplicating the search results and truncating them according to a preset size threshold includes: merging the results from the three-way search from the short-term memory circular buffer, the long-term memory persistent database, and the reflective memory circular buffer into a single list, deduplicating them using the unique memory identifier, arranging them in descending order by timestamp and importance score, and concatenating them one by one into a text sequence until the preset size threshold is reached, discarding any excess.
[0011] Based on the aforementioned scheme, the step of merging similar events within a continuous time window in the long-term memory persistence database into a summary record includes: using the same device identifier and the same memory type as similarity criteria, merging multiple event records falling within the same continuous time window into a single summary record, setting a compression status flag, deleting the merged original records, and taking the highest value among the original records as the importance score of the summary record.
[0012] Based on the aforementioned scheme, when the available space of the storage medium is lower than the space shortage threshold, the upper limit of capacity is automatically adjusted to the preset shortage cleanup threshold.
[0013] Based on the aforementioned scheme, the long-term memory persistence database is an SQLite database, which is configured with a composite index consisting of three columns: timestamp, memory type, and importance score, as well as a composite index consisting of fault type and device identifier.
[0014] According to another aspect of the present invention, an embedded AI agent three-layer memory management and context retrieval system is provided, the system comprising: The short-term memory module is equipped with a circular buffer for storing observation-type memories and reasoning-type memories; The long-term memory module is equipped with a persistent database for persistently storing action-related memories, system-related memories, and fault mode records. The reflective memory module is configured with a circular buffer for storing feedback-type memories and failure mode summaries; The routing module is used to automatically route memory entries to the short-term memory module, the long-term memory module, or the reflective memory module for storage according to the memory type at each stage of the ReAct decision loop. The context building module is used to retrieve historical memories in parallel from the short-term memory module, the long-term memory module, and the reflective memory module during the Reason phase of the ReAct loop. The retrieval results are merged, deduplicated, and truncated according to a preset size threshold to output the reasoning context. The cleaning module is used to delete memory entries and reclaim storage space based on importance score when the number of entries in the long-term memory module exceeds the capacity limit or the storage medium space is insufficient. A compression module is used to merge similar events within a continuous time window in the long-term memory module into a summary record.
[0015] As can be seen from the above technical solutions, the present invention has at least the following advantages and positive effects compared with the prior art: The present invention achieves tight coupling integration of the ReAct loop under extremely low memory budget by combining a three-layer memory hierarchical architecture with six types of memory automatic routing and a three-way parallel retrieval mechanism, so that the memory generated in each stage is automatically classified, stored and called on demand, and the retrieval response meets the real-time requirements; the human-like intelligent adaptive elimination of memory value is achieved by the time exponential decay method in the multi-dimensional importance scoring, so that high-value historical experience is effectively preserved in limited storage space and is not prematurely covered by low-value redundant memory; through the fault mode memory mechanism, the experience of successfully handled equipment faults is summarized into reusable fault modes, so that subsequent similar faults can be directly matched with historical handling solutions, significantly improving the efficiency of fault diagnosis and handling; through continuous event compression and semantic caching deduplication mechanism, the storage space occupation is greatly reduced and redundant memory writing is reduced, so that the Agent can accumulate more effective operation and maintenance experience in limited Flash space, providing complete technical support for the long-term autonomous operation of AI Agent in unattended embedded device scenarios.
[0016] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit the invention. Attached Figure Description
[0017] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention. It is obvious that the drawings described below are merely some embodiments of the invention, and those skilled in the art can obtain other drawings based on these drawings without any inventive effort. In the drawings: Figure 1 This is a schematic diagram of an embedded AI Agent three-layer memory management and context retrieval method according to the present invention; Figure 2 This is a schematic diagram of the overall process of an embedded AI Agent three-layer memory management and context retrieval method according to the present invention. Detailed Implementation
[0018] To more clearly illustrate the purpose, technical solutions, and advantages of the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The exemplary embodiments can be implemented in many forms and should not be construed as limited to the examples set forth herein. On the contrary, these embodiments are provided so that the present invention will be more comprehensive and complete, and fully convey the concept of the exemplary embodiments to those skilled in the art.
[0019] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a full understanding of embodiments of the invention. However, those skilled in the art will recognize that the technical solutions of the invention can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of the invention.
[0020] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0021] The flowcharts shown in the accompanying drawings are merely illustrative and do not necessarily include all content and operations / steps, nor do they necessarily have to be performed in the described order. For example, some operations / steps can be broken down, while others can be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.
[0022] The present invention will now be described in detail with reference to specific embodiments.
[0023] Example 1
[0024] like Figure 1 , 2 As shown, this embodiment provides a three-layer memory management and context retrieval method for embedded AI agents. Figure 2The entire process of the method from startup to operation is illustrated exemplarily: After system startup, step S1 is executed first to initialize the memory system. After completing the resource allocation of the three-layer memory storage structure, the ReAct decision loop is entered. In the Observe phase, the acquisition engine collects device data and writes the observation memory into the short-term memory circular buffer in step S2. If a telecommunications fault is detected, step S6 is executed to perform fault marking, dedicated retrieval, and pattern induction processing. In the Reason phase, step S3 is triggered to perform three-way parallel retrieval, simultaneously retrieving historical memories from short-term memory, long-term memory, and reflective memory. After weighting, sorting, deduplication, and truncation, the inference context is output for the AI inference engine to generate decisions. In the Act phase, the Agent executes maintenance actions and writes the action memory into both long-term memory and short-term memory in step S2. In the Feedback phase, the Agent evaluates the effect of the actions and writes the feedback memory into the reflective memory in step S2, and updates the user model. Background scheduled tasks run concurrently with the main loop: The timer triggers step S4 at a predetermined period to perform space-aware adaptive cleaning, eliminates low-value memories by importance scoring and reclaims storage space; at the same time, it triggers step S5 to merge similar events within a continuous time window in long-term memory into summary records, compressing storage space.
[0025] like Figure 1 As shown, the specific steps of this method are as follows: S1: Initialize a three-layer memory storage structure, which includes: a short-term memory circular buffer, a long-term memory persistent database, and a reflective memory circular buffer.
[0026] A three-layer memory storage architecture is established when the embedded AI Agent starts up. In the ARM / RISC-V embedded Linux environment, the resource allocation, data structure creation and configuration parameter loading of the short-term memory circular buffer, long-term memory SQLite database and reflective memory circular buffer are completed, so that the memory system can be put into a serviceable state.
[0027] The short-term memory circular buffer uses a fixed-capacity circular array structure with a preset capacity of 100 memory entries. During initialization, a standard memory allocation function (such as `calloc`) is called to allocate contiguous memory space in the heap. Each memory entry is stored according to the field layout of the `MemoryItemInternal` structure. The buffer maintains a head pointer (write pointer) and a tail pointer (read pointer), both of which initially point to the starting address of the buffer, and the valid flags of all entries are set to invalid. The circular buffer uses a FIFO (First-In, First-Out) eviction policy; when it is full, a new entry automatically overwrites the oldest entry. The time complexity of both write and read operations is O(1). The storage space for each memory entry does not exceed 256 bytes, and the entire circular buffer occupies approximately 25KB of memory.
[0028] Long-term memory uses an embedded SQLite database as its persistent storage engine. During initialization, the `sqlite3_open` function is called to open or create the database file; if the file does not exist, it is created automatically. After the database is opened, DDL (Data Definition Language) statements are executed to create three core data tables: the `agent_memory_event` table stores event-related memories, the `agent_memory_decision` table stores decision-related memories, and the `agent_memory_action` table stores action-related memories. Each table contains the following fields: item_id (UUID type, 16 bytes, globally unique identifier), type (integer type, enumerated memory type), timestamp (integer type, Unix millisecond-level timestamp), content (text type, storing structured JSON data), importance_score (floating-point type, value range 0~1), access_count (integer type, access count counter), last_access_time (integer type, most recent access timestamp), expires_at (integer type, expiration timestamp, 0 indicates never expires), and compressed (integer type, boolean flag, indicating whether it has been compressed). After the table is created, the CREATE INDEX statement is executed to create a composite index consisting of the timestamp, type, and importance_score columns. This index is used to accelerate multidimensional retrieval queries filtered by time range and importance, keeping the retrieval time within 5ms.
[0029] The initialization method for the reflective memory circular buffer is the same as that for the short-term memory circular buffer, with a fixed capacity of 100 entries. Each entry stores no more than 256 bytes of space, occupying approximately 25KB of memory. Unlike short-term memory, reflective memory stores reflective abstracts, each no more than 256 bytes, and includes the decision quality score and fault type flag required for user model updates. During initialization, both the head and tail pointers of the circular buffer point to the starting address, and all entry flag positions are invalid.
[0030] The UserModel is stored in JSON key-value pair format. This model structure is loaded or created during initialization and includes an operations preference dictionary (user-manually adjusted collection frequency, alarm thresholds, handling methods, etc., stored in JSON key-value format), a habitual pattern queue (stores the most recent 20 user operation histories, recording common user operation sequences), and a learning rule set (operations rules automatically learned from user behavior, used to optimize agent decisions). MemoryStats resets all counters to zero during initialization, including the number of memory hits at each level (stm_hits, ltm_hits, rfm_hits), total number of queries (total_queries), number of cleanup executions (cleanup_count), and the current entry count for each level of memory.
[0031] In the final initialization stage, all adjustable parameters are loaded from the configuration file into the MemoryConfig structure. These parameters include: short-term memory capacity (default 100 entries), maximum long-term memory entries (default 5000 entries), reflection memory capacity (default 100 entries), maximum context size (default 4096 bytes), maximum reflection summary size (default 256 bytes), weighting coefficients for the four-dimensional importance score (freshness weight 0.3, anomaly severity weight 0.3, decision impact weight 0.25, access frequency weight 0.15), scheduled cleanup cycle (default 300 seconds), Flash space shortage threshold (default 10%), cleanup threshold when space is scarce (default 3000 entries), and long-term memory cleanup ratio (default 10%). After configuration loading is complete, the memory system enters a serviceable state.
[0032] Through the above initialization steps, the three-layer memory system is ready immediately upon device power-on, without relying on an external network or additional runtime environment. Actual testing showed that the total memory usage of the entire memory system is approximately 2.8MB, including approximately 25KB for the short-term memory circular buffer, approximately 25KB for the reflective memory circular buffer, and the memory usage of the long-term memory SQLite database is dynamically managed by the SQLite engine. The total memory usage meets the hard budget constraint of 5MB.
[0033] S2: In each stage of the ReAct decision loop, memory entries are automatically routed to the corresponding memory layers for storage based on memory type, and importance scores are calculated.
[0034] In each stage of the ReAct decision loop (Observe→Reason→Act→Feedback), the unified memory recording interface is called to automatically classify and store memory entries according to memory type, and calculate multidimensional importance scores and expiration time for each memory, so as to achieve tight coupling between memory content and agent reasoning process.
[0035] In this embodiment, six memory type enumeration values are defined to distinguish the memory content generated in different decision-making stages: MEM_TYPE_OBSERVE (value 0) is observation memory, generated from snapshots of device operation data collected in the Observe stage; MEM_TYPE_REASON (value 1) is reasoning memory, generated from the intermediate reasoning process in the Reason stage; MEM_TYPE_ACT (value 2) is action memory, generated from the specific operation and maintenance actions executed in the Act stage; MEM_TYPE_FEEDBACK (value 3) is feedback memory, generated from the evaluation and reflection summary of the action effect in the Feedback stage; MEM_TYPE_SYSTEM (value 4) is system memory, generated from changes in the internal state of the system or timed tasks; and MEM_TYPE_COT (value 5) is thought chain memory, generated from the intermediate steps of the chain reasoning process.
[0036] Each memory type corresponds to a base importance weight (base_type_weight), which is used for the calculation of subsequent importance scores. The base weights for different types are preset by the system to reflect the inherent value differences of memory content at different decision-making stages.
[0037] At various stages of the ReAct loop, the Agent calls the `agent_memory_record(type, content, metadata)` function to write memory entries. Here, `type` is one of the six enumerated values mentioned above, `content` is the memory content in structured JSON format, and `metadata` is additional information (such as device ID, anomaly score, alarm severity, etc.). This function is the sole entry point for memory writing; internally, it automatically performs route distribution based on the `type` value, without requiring the caller to concern itself with the specific storage layer details.
[0038] The `agent_memory_record` function executes routing rules based on the memory type, including: (1) Observation memory and reasoning memory: directly written to the short-term memory circular buffer. During writing, the current head pointer position is obtained, the memory entry is filled according to the MemoryItemInternal structure format (including item_id, type, timestamp, content, importance_score, access_count, last_access_time, expires_at, and compressed fields), and stored in that position. Then the head pointer is incremented (if the end of the buffer is reached, it returns to the starting position), and the current entry count of the short-term memory is updated. If the buffer is full (i.e., the head pointer and tail pointer coincide and all valid flags are valid), the new entry automatically overwrites the oldest entry pointed to by the tail pointer, the tail pointer is incremented synchronously, and the eviction count is accumulated. The time complexity of this write operation is O(1).
[0039] (2) Action Memory (ACT), System Memory (SYSTEM), and Cognitive Chain Memory (COT): These are written simultaneously to the long-term memory SQLite database and the short-term memory circular buffer. The method of writing to short-term memory is the same as described in (1) above; when writing to long-term memory, each field of the memory entry is mapped to the corresponding column of the SQLite table agent_memory_decision (action memory) or agent_memory_event (system memory and cognitive chain memory), an INSERT statement is constructed and parameterized binding is performed, and the transaction mechanism of SQLite is used for commit. To maintain data consistency between short-term memory and long-term memory, if the long-term memory write fails, the short-term memory write is also rolled back (the written entries are undone), ensuring that the data in the two storage paths eventually becomes consistent.
[0040] (3) Feedback Memory: Only written to the reflective memory circular buffer, the writing method is the same as short-term memory, the capacity is 100 entries, and a FIFO eviction strategy is adopted. The writing of feedback memory triggers the update of the user model, extracts the decision quality score (decision_quality) and fault type label (telecom_fault_type) from the memory content, adds them to the user model's habitual pattern queue (the most recent 20 operation histories), and adjusts the decision weights in the learning rule set according to the feedback results of success or failure.
[0041] Each memory entry requires an importance score to be calculated during writing. This score uses a four-dimensional weighted algorithm, with the four dimensions and their weight coefficients as follows: 1) Freshness (F), reflecting the newness or oldness of a memory, is calculated using the following formula: Where age is the memory age, i.e., the number of days since the memory was made (calculated by the difference between the current timestamp and the memory timestamp), and the exponential decay factor of 7 days ensures that more recent memories receive a higher freshness score; the weighting coefficient of this dimension... .
[0042] 2) Anomaly Severity (A): Reflects the degree of anomaly in the observed data or event, ranging from 0 to 1. It is obtained by normalizing the anomaly_score or alarm_severity detected in the Reason phase of the ReAct loop. If the memory type is OBSERVE and there is no anomaly marker, A is 0; if it is an alarm or fault event, A is the anomaly severity value output in the Reason phase. The weighting coefficient for this dimension... .
[0043] 3) Decision Impact (I): Reflects the degree of influence of the action execution result on the system state, with a value ranging from 0 to 1. It is mapped from the effect evaluation after the action execution in the Act phase (such as changes in CPU utilization, whether the fault is resolved). If the memory type is not ACT or FEEDBACK, I is 0; otherwise, it is calculated based on the success or failure of the action and the magnitude of the impact. The weight coefficient of this dimension is... .
[0044] 4) Access frequency (Q), reflecting the activity level of the memory being referenced in subsequent queries, is calculated using the following formula: This is calculated by dividing the current access count by the maximum access count in all memories (access_count is initially 0 when the first record is written, Q=0). Each subsequent query that hits this memory increments access_count, and the Q value is dynamically updated. This dimension's weight coefficient... .
[0045] The final importance score s is calculated using the following formula: ; The calculation result ranges from 0 to 1. For telecommunications fault mode memories (such as VSWR anomalies, PA overheating, etc.), an additional base type weight and alarm severity bonus are applied during writing to ensure that the initial score is not lower than 0.7, thus preventing critical fault experience from being prematurely eliminated. The scoring calculation process is completed within the write function, using floating-point operations, with a single calculation taking less than 1 millisecond. The specific scoring formula for telecommunications fault mode memories is as follows: ; In the formula, `base_type_weight` is the base type weight, determined by the type enumeration value (type) of the memory entry. Different base weights are preset according to the memory type: 0.4 for EVENT type (event memory), 0.3 for DECISION type (decision memory), and 0.2 for OBSERVATION type (observation memory). For telecommunications fault mode memory, the memory type is fixed as EVENT, and `base_type_weight` is 0.4. `alarm_severity_bonus` is the alarm severity bonus, ranging from 0 to 0.4, mapped from the alarm level detected by the acquisition engine during the Observe phase; it is 0.4 for "Critical", 0.25 for "Major", 0.1 for "Minor", and 0 for no alarm. This mapping relationship is preset in the system configuration to reflect the different impacts of different alarm levels on the importance of the memory. `anomaly_score_bonus` is the anomalous score bonus, ranging from 0 to 0.3. It is obtained by multiplying the anomalous score (anomaly_score, ranging from 0 to 1) output in the Reason stage by 0.3 (i.e., `anomaly_score_bonus = anomaly_score × 0.3`). `anomaly_score` is a normalized measure of the deviation between the collected index value and the preset baseline. The larger the deviation, the higher the score, giving greater importance bonuses to anomalous observations that significantly deviate from the normal range. `clamp(x, 0.0, 1.0)` is a clipping function that restricts the input value `x` to the closed interval [0.0, 1.0]. If `x < 0.0`, it takes 0.0; if `x > 1.0`, it takes 1.0; otherwise, it takes `x` itself. This function ensures that the final importance score `importance_score` always falls within the valid range of 0 to 1, avoiding invalid scores due to weight overflow.
[0046] The `expires_at` field for each memory entry is automatically set based on the memory type and system configuration, and is used to prioritize the eviction of expired entries during subsequent cleanup. Specific rules include: Real-time collected observation memory (OBSERVE type): retained for 7 days (expires_at = current timestamp + 7 × 24 × 3600 seconds), covering trend analysis needs within one week; Heartbeat records (system periodic status reports, SYSTEM type): retained for 14 days; Operation and maintenance logs (ACT type): Retained for 30 days; AI analysis reports (REASON or COT type): Retained for 30 days; Alarm history (FEEDBACK or alarm-related events): Retained for 90 days, covering the quarterly model learning window; Configuration change log (system configuration modification): The most recent 50 versions are retained, and the expires_at of the corresponding entries is set to the current timestamp plus a maximum value (such as 10 years), which means it never expires. The number of entries is controlled by the number of versions. For other types not explicitly specified, the default value for expires_at is 0, indicating that it will never expire.
[0047] The expiration time mentioned above can be adjusted through configuration parameters. When writing, the expires_at value is calculated based on the current system time and type mapping table and stored in the corresponding field of the memory entry.
[0048] Memory write operations involve coordinated updates to both memory and persistent storage. To ensure data integrity, when writing to long-term memory, all INSERT operations are wrapped in an SQLite transaction. If the transaction fails to commit, a rollback is performed, and an error flag is set. At this time, short-term memory writes are synchronously reversed (if already written), ensuring consistency between the two storage paths. Because writes to the circular buffers of short-term and reflective memory are memory operations and do not involve external I / O, if the buffer becomes full, this is considered a normal FIFO eviction mechanism and not an anomaly. All write operations update memory statistics, including the total write count and the write counts for each level.
[0049] Through the aforementioned routing and writing mechanism, the automatic classification and storage of memories at each stage of the ReAct loop is realized, enabling the Agent to retrieve the corresponding type of memory as needed in subsequent reasoning. At the same time, through multi-dimensional importance scoring and differentiated expiration strategies, priority is provided for subsequent cleaning and compression, thereby ensuring the long-term retention of high-value memories with extremely low resource overhead.
[0050] S3: In the Reason phase of the ReAct loop, historical memories are retrieved in parallel from the short-term memory circular buffer, the long-term memory persistent database, and the reflective memory circular buffer. The retrieval results are merged, deduplicated, and truncated according to a preset size threshold, and the reasoning context is output.
[0051] Execution is triggered in the Reason phase of the ReAct loop. Through the unified context building interface, historical information related to the current decision is retrieved in parallel from the three layers of memory. After sorting, deduplication and truncation, a structured reasoning context is generated and injected into the Agent's reasoning engine to provide historical experience support for decision-making.
[0052] In the Reason phase of the ReAct loop, the Agent calls the `agent_memory_context_build()` function to trigger context construction. This function takes no input parameters and automatically retrieves the following context parameters from the current Agent runtime environment: the current device ID (`device_id`), the current observation timestamp (`current_timestamp`), the currently detected anomaly type (`anomaly_type`, if any), and the current telecom fault type (`telecom_fault_type`, if any). This function returns a context buffer (`ContextBuffer`) with a maximum size of 4096 bytes, containing a sorted, deduplicated, and truncated sequence of historical memory text, which can be directly used by the Agent's inference engine. The entire construction process is completed within the 5-second time budget of the ReAct loop, with a measured total time of less than 50 milliseconds.
[0053] When the agent_memory_context_build() function is executed, it simultaneously initiates parallel retrieval requests to the three layers of memory. The specific retrieval methods include: (1) Short-term memory retrieval. Using the current device ID as the retrieval key, a linear scan is performed in the short-term memory circular buffer from the tail pointer (latest data) to the head pointer (oldest data) to retrieve the 5 most recent observation memories of the device (i.e., type=OBSERVE and device_id matches). The timestamp and content fields are extracted to form time series data. If a telecommunications fault type parameter exists, observation memories containing the same telecom_fault_type tag are retrieved first. Short-term memory retrieval uses an O(n) linear scan (n≤100), and the retrieval time is less than 1 millisecond.
[0054] (2) Long-term memory retrieval. An SQL query is constructed using the current device ID, time range (7 days prior to the current time, filtered by the expires_at field), and importance score threshold (default 0.3) as combined conditions. The retrieval is performed using the timestamp+type+importance_score composite index and the (device_id, timestamp) composite index. If a telecommunications fault type parameter exists, an equal-value matching condition for telecom_fault_type is added to the query conditions, and the (telecom_fault_type, device_id) composite index is used to accelerate the process, returning historical processing records for the same device and fault type. Retrieval results are sorted by importance_score DESC, with a maximum of 10 results returned. Long-term memory retrieval utilizes SQLite index scanning, with a retrieval time of less than 5 milliseconds.
[0055] (3) Reflection Memory Retrieval. Using the current device ID and telecommunications fault type (if any), a linear scan of all 100 records in the reflection memory circular buffer is performed to retrieve reflection abstracts related to the current device, prioritizing records containing the same telecom_fault_type tag. If no matching reflection memory exists for the current device, the retrieval is broadened to retrieve general reflection abstracts for all devices. The reflection memory retrieval uses an O(n) linear scan (n≤100), with a retrieval time of less than 1 millisecond.
[0056] The above three retrieval methods are executed concurrently through multithreading or coroutines; in an embedded environment, polling pseudo-concurrency can be used because each retrieval path is independent and has no data dependency; the total retrieval waiting time is the maximum of the three, that is, no more than 5 milliseconds.
[0057] When the current observation contains a telecommunications fault type parameter (i.e., telecom_fault_type is not empty and takes the value of VSWR_ANOMALY, PA_OVERHEAT, SFP_DEGRADATION, or CPRI_LINK_LOSS), the context builder performs an additional dedicated retrieval step: In short-term memory, it is not limited to the most recent 5 observation records, but rather extracts 3 telecommunications indicator observation records before and after the time of the equipment failure (no less than 5 in total) that contain the same telecom_fault_type tag. The time series data (such as the VSWR gradual process: 1.5→2.1→2.8→3.2) is completely extracted and used as the context for trend analysis. In long-term memory, an additional independent query is added to retrieve all historical fault mode records with importance_score ≥ 0.7 by combining telecom_fault_type and device_id, regardless of the time range, and return all matching fault handling records, sorted by similarity (based on complete matching of fault type) and importance score. In the reflective memory, retrieve all fault mode summary summaries containing the same telecom_fault_type tag, sorted from newest to oldest by write time.
[0058] The above-mentioned telecommunications-specific search results and general search results are merged and then entered into the subsequent sorting process.
[0059] After the three-way parallel retrieval is completed, three retrieval result sets (STM_result, LTM_result, RFM_result) are obtained. The context builder executes the merging process. The three result sets are merged into a single list, and deduplication is performed using the item_id (UUID) of the memory entry as the unique key. When the same item_id appears in multiple result sets (e.g., a decision memory appears in both short-term and long-term memory), only one record is retained, with the complete content in long-term memory being the preferred version, as long-term memory stores the most complete content (including complete fields such as access_count and last_access_time). In telecommunications fault scenarios, a relevance weighting coefficient is applied to each memory in the merged list. The weighting rules include: if the telecom_fault_type field of a memory completely matches the current fault type, the overall score of that memory is multiplied by the first weighting coefficient of 1.5; if the importance_score field value of a memory is ≥0.7 (i.e., a high-importance memory), the overall score of that memory is multiplied by the second weighting coefficient of 1.3. If the same memory meets both conditions simultaneously, the weights are combined, resulting in a combined weighting factor of 1.5 × 1.3 = 1.95. In non-telecom failure scenarios or when a memory does not meet any conditions, the weighting factor is 1.0 (unweighted). After deduplication and weighting, all memories in the list are sorted in descending order of priority as follows: the primary sorting key is the timestamp, with newer memories appearing first; the secondary sorting key is the weighted overall score (i.e., importance_score multiplied by the corresponding relevance weighting factor), with memories with higher scores appearing first. Prioritizing timestamps ensures that most recently occurring memories are included in the context first; prioritizing importance scores ensures that high-value memories within the same time window are displayed first.
[0060] After sorting, the context builder converts the content field (structured JSON format) in the memory list into human-readable text descriptions, concatenating them into a context text sequence. During the conversion, a timestamp and memory type label (such as "[2026-07-21 14:32:15][OBSERVE]") are appended to the text description of each memory so that the agent's inference engine can distinguish between different time periods and different types of memory sources.
[0061] The concatenation of text sequences follows truncation rules, including: appending text line by line starting from the first sorted memory; checking the number of bytes already occupied in the current context buffer before appending each line; if the length of the text after adding the next memory exceeds the maximum size threshold (default 4096 bytes), appending stops, the remaining subsequent memories are discarded, and a truncation marker (such as "...[truncated]") is added to the end of the buffer as a warning. This truncation strategy ensures that the output context buffer does not exceed the preset 4096-byte limit, meeting the memory transfer constraints in embedded environments.
[0062] If the merged memory list is empty (i.e., there is no matching record in any of the three layers of memory), a pre-set empty context message (such as "No relevant historical memory found.") is returned to ensure that the Agent's inference engine always receives valid input rather than a null pointer.
[0063] The constructed context buffer is returned by the `agent_memory_context_build()` function to the caller in the Reason phase of the ReAct loop. The caller uses this buffer content as part of the system prompt, submitting it along with the current observations to the Agent's inference engine (such as a large language model) for decision-making. During context construction, the `access_count` field of all retrieved memory entries is incrementally updated in the long-term memory SQLite database (using `UPDATE ... SET access_count=access_count+1, last_access_time=current_timestamp WHERE item_id=?`). Due to memory limitations, the `access_count` of corresponding entries in short-term memory and reflective memory is not persistently updated, but the updated count will be synchronized the next time the data is reloaded from long-term memory. This access count update operation is performed asynchronously, without blocking the main return path of the context buffer, ensuring that the total construction time is not affected by SQLite write operations.
[0064] S4: When the number of entries in the long-term memory persistent database exceeds the capacity limit or the storage medium space is insufficient, delete the memory entries and reclaim the storage space according to the importance score.
[0065] During the continuous operation of the AI Agent, memory cleanup is performed periodically. A timed trigger mechanism checks the storage status of each memory layer. Adaptive eviction is implemented for the long-term memory SQLite database based on both importance scoring and storage space conditions, ensuring that storage resources remain within a controllable range during long-term system operation. The short-term memory circular buffer and the reflective memory circular buffer, due to their fixed-capacity FIFO natural eviction characteristics, require no additional cleanup logic intervention.
[0066] The Cleanup Engine starts periodic cleanup tasks after system initialization. The cleanup interval is set by the configuration parameter `cleanup_interval`, with a default value of 300 seconds. In an embedded Linux environment, the timed triggering of cleanup tasks is implemented through the `timerfd` (timer file descriptor) mechanism. This mechanism is based on kernel event-driven mechanisms and offers higher precision and lower CPU wake-up overhead compared to traditional `setitimer` or `sleep` methods. At the end of each cleanup interval, the Cleanup Engine calls the `agent_memory_cleanup()` function to execute the complete cleanup process. During execution, this function checks the current system time to ensure that only one cleanup instance is running at any given time (implemented through mutexes or atomic flags), avoiding resource contention caused by concurrent cleanup triggered from incomplete previous cleanup rounds.
[0067] The `agent_memory_cleanup()` function first queries the current total number of entries N_ltm in the long-term memory SQLite database, executing `SELECT COUNT(*) FROM agent_memory_event` (along with the corresponding `decision` and `action` tables, counting and summing the number of entries in each table). It then compares N_ltm with the maximum long-term memory capacity N_max (default 5000 entries) configured in the parameters. If N_ltm > N_max, the entry count cleanup process is triggered, including: (1) Filtering entries to be deleted. Construct an SQL query to filter entries to be deleted from the three tables. The filtering condition is ORDER BY importance_score ASC (i.e., sorted in ascending order of importance score), prioritizing entries with the lowest importance score. Add expires_at as a secondary sorting key to the sorting condition (ORDER BY importance_scoreASC, expires_at ASC) so that expired low-importance entries are deleted first. The deletion ratio is 10% of the difference between the current total number of entries and the maximum capacity, calculated as delete_count=(N_ltm-N_max)*0.1. If the result is less than 1, it is rounded up to 1. Limit the number of entries deleted in a single transaction by LIMIT delete_count to avoid excessive database transactions caused by large-scale deletion.
[0068] (2) Perform the deletion operation. Using the list of item_ids of the selected items to be deleted as conditions, execute the DELETE FROM ... WHERE item_id IN (...) statement in each of the three tables. All deletion operations are wrapped in a single SQLite transaction to ensure atomicity. If deletion fails in any table, the entire transaction is rolled back without deleting any items. After the transaction is committed, record the number of items actually deleted in the statistics.
[0069] (3) Reclaim storage space. After the deletion operation is completed, the VACUUM command is executed to reclaim free space in the SQLite database file. The VACUUM command reclaims fragmented space generated after the deletion of entries by rebuilding the database file, thereby reducing the actual space occupied by the database file on Flash storage. To avoid the VACUUM operation blocking normal memory read and write, this command is executed after the deletion transaction is committed and before the end of the cleanup cycle, and the read and write of short-term memory and reflective memory are not affected during the execution (because the two are independent memory structures). The entire cleanup process (including VACUUM) takes less than 200 milliseconds in actual tests when the number of long-term memory entries is 5000.
[0070] Before performing the entry count check, the agent_memory_cleanup() function first performs a Flash storage space check. It calls a system interface (such as the statvfs function) to obtain the available space information of the file system where the long-term memory database is located, and calculates the available space percentage F_free (available bytes divided by the total number of bytes multiplied by 100%). It compares F_free with the Flash space pressure threshold F_threshold (default 10%) in the configuration parameters: (1) Normal space state (F_free ≥ 10%). The maximum capacity threshold of long-term memory is the default value N_max = 5000 entries, and the cleanup trigger condition is N_ltm > 5000, deleting the least important entries at a rate of 10%. (2) Space pressure state (F_free < 10%). The maximum capacity threshold for long-term memory has been adjusted to a space-constrained cleanup threshold of N_stress = 3000 records. The cleanup trigger condition has been changed to N_ltm > 3000, meaning that cleanup will be triggered in advance when the available Flash space is below 10%, reducing the storage space occupied by database files and prioritizing the protection of the device's basic operating space and log recording space from exhaustion. The threshold adjustment takes effect at the start of this cleanup cycle and automatically reverts to 5000 records once F_free recovers to above 10%.
[0071] This space-aware adaptive mechanism ensures that in extreme cases of limited Flash storage space, the memory system can proactively reduce storage usage, avoiding serious consequences such as device crashes or read-only file systems caused by full storage.
[0072] The agent_memory_cleanup() function updates the following fields in the memory statistics structure (MemoryStats) after each execution: cleanup_count: Increments the cleanup execution count by 1; last_cleanup_time: Records the timestamp of this cleanup completion; ltm_current_count: Records the current number of entries in long-term memory after cleanup; free_flash_percent: Records the current percentage of available Flash space; deleted_count: Records the total number of entries actually deleted during this cleanup (0 if no deletion is triggered).
[0073] The above statistical information can be exported through the debugging interface or the operation and maintenance query interface to monitor the health status and cleanup effect of the memory system.
[0074] The short-term memory circular buffer and the reflective memory circular buffer employ a fixed capacity and a FIFO (First-In, First-Out) natural eviction strategy, with a maximum capacity of 100 entries each. When a new entry is written, if the buffer is full (i.e., the head pointer catches up with the tail pointer and all entries are valid), the new entry automatically overwrites the oldest valid entry, and the tail pointer moves forward synchronously. This eviction process is driven by the write operation itself and requires no separate cleanup task. The short-term memory circular buffer increments the `eviction_count` field when overwriting the oldest entry to count the eviction frequency. The reflective memory circular buffer also records eviction counts, but the user model-related data (such as decision quality scores) in reflective memory is synchronously updated to the user model's persistent file before the entry is evictioned; therefore, entry eviction does not affect the user model's cumulative learning results.
[0075] Through the aforementioned timed cleanup and adaptive elimination mechanism, cleanup can be automatically triggered when long-term memory exceeds its capacity limit or Flash storage space is insufficient. Low-value items are deleted from low to high importance based on their importance score, and storage space is reclaimed in conjunction with VACUUM to ensure that the memory system always maintains a controllable storage occupation range during long-term unattended operation. At the same time, the safety of device operation is prioritized through space-aware threshold adjustment.
[0076] S5: Merge similar events within consecutive time windows in the long-term memory persistence database into summary records.
[0077] Similar historical events in the long-term memory SQLite database are merged and compressed. By identifying recurring events of the same device and type within a continuous time window, they are summarized into a single summary record, thereby reducing storage space usage and extending the retention period of high-value memories. The compression process is executed independently by the Compress Engine and can be triggered in conjunction with scheduled cleanup tasks without interfering with the normal memory read and write operations of the ReAct loop.
[0078] The compression operation is performed by the `agent_memory_compress()` function, which can be triggered periodically or manually. For periodic triggering, after each `agent_memory_cleanup()` cleanup process, if the current number of long-term memory entries exceeds 80% of the maximum capacity threshold (i.e., N_ltm > N_max × 0.8), `agent_memory_compress()` is automatically called to perform a compression operation. This aims to reduce the number of entries and decrease the deletion pressure during subsequent cleanup. For manual triggering, maintenance personnel or upper-level management platforms can actively call the compression function through the management interface for emergency storage consolidation when storage space is limited. Before execution, the compression function checks the system load. If the current CPU utilization exceeds a preset threshold (e.g., 80%) or there are incomplete compression transactions, the current compression is skipped, waiting for the next trigger opportunity to avoid the compression operation consuming computational resources of the ReAct decision loop.
[0079] When the agent_memory_compress() function is executed, it first scans the agent_memory_event table in the long-term memory SQLite database to query event records that meet the following conditions: The compressed flag is set to false (only the raw, unprocessed records are compressed); Event memories of type EVENT or OBSERVATION (compression operation is not applicable to DECISION and REFLECTION types, because the content of decision-making and reflection memories differs greatly, and merging will lose key semantic information). expires_at is 0 or has not expired (expired records are handled by the cleanup process and are not included in the compression candidate).
[0080] The query results are sorted in ascending order by device_id, type, and timestamp, returning a list of records. The compression engine iterates through this list, using a predetermined continuous time window (default 600 seconds, or 10 minutes) as the merging window. Multiple event records from the same device (same device_id), of the same type (same type), and whose timestamps fall within the same 600-second window are grouped into a candidate compression set. The time window is divided starting from the timestamp of the first record, and the timestamps of all records within the window are within the range [window_start, window_start+600). When a record with a timestamp exceeding the current window is encountered, the current window is closed and a new window is opened to ensure that records from different windows are not merged across windows.
[0081] For each identified candidate compression set (containing n original records), the compression engine performs a digest generation process, including: (1) Content Merging. Extract key information from the content field (structured JSON) of each record in the set, including but not limited to: device ID, indicator type, anomaly type, and statistical characteristics of the observed values (minimum, maximum, average, and trend description). During the merging process, the original content of n records is merged into a concise JSON digest structure, which includes fields such as original_count (number of original records), time_start (window start timestamp), time_end (window end timestamp), avg_value (average indicator value, if applicable), and trend (trend description, such as "rising", "falling", "fluctuating").
[0082] (2) Importance score inheritance. The importance score of the summary record is the maximum value of the importance scores of each original record in the set, and a continuous event compression bonus factor (compression_bonus=0.05×log2(n), where n is the number of merged records) is superimposed to reflect the potential value of high-frequency repeated events in pattern recognition and avoid the high-value information being underestimated after compression. The final score is truncated to the upper limit of 1.0.
[0083] (3) Summary text generation. The merged JSON structure is serialized into a text summary with a length limit of 256 bytes (consistent with the summary size of reflective memory). If the summary content exceeds this limit, it is further simplified to the most critical statistical information and trend descriptions, and redundant detailed fields are removed.
[0084] After the summary record is generated, the compression engine executes within an SQLite transaction: The summary record is inserted as a new entry into the `agent_memory_event` table, its `compressed` flag is set to true, the `content` field stores the generated summary JSON, and the `original_record_count` field (as defined in the table) records the number of original records `n` merged in this transaction; the `compressed` flag of all merged original records is updated to true (instead of being immediately physically deleted) to distinguish between processed and unprocessed records during compression ratio statistics; subsequent cleanup processes will gradually physically delete these compressed original records based on their importance scores. The transaction is then committed to ensure the atomicity of the summary insertion and original record marking.
[0085] If an error occurs during the execution of a transaction (such as database locking or write failure), the entire transaction is rolled back, the current compression operation is considered a failure, and it will wait for the next trigger to retry.
[0086] After each compression operation is completed, the compression engine calculates the compression ratio of this compression, using the following formula: ; in, The total number of bytes in the summary records generated for this compression (the sum of the lengths of the content field of all summary records); This is the sum of the lengths of the `content` field of all the original records being merged (based on the original content length read before compression). Compression ratio. The value ranges from 0 to 1, with values closer to 1 indicating better compression. In this example, five consecutive alarm records (original total size approximately 1280 bytes) are merged into one summary (approximately 256 bytes), achieving a compression ratio of 1 − 256 / 1280 = 0.80, which saves 80% of storage space. This compression ratio, along with the number of records processed in this compression and the compression timestamp, is recorded in a statistics structure (stats.compression_ratio and stats.last_compress_time) for operation and maintenance monitoring.
[0087] Both compression and cleanup operations are resource-sensitive tasks, and they may be executed sequentially within the same cleanup cycle. To avoid duplicate scans or transaction conflicts, the compression engine checks at startup whether cleanup is currently in progress (using an atomic flag). If so, it waits for cleanup to complete and then retryes, or skips the current compression operation. Compression only processes records where compressed=false and have not been marked for deletion by the cleanup process, ensuring that low-value records already in the elimination candidate set are not compressed (they are directly deleted by cleanup), thus avoiding wasting compression resources on records that are about to be deleted.
[0088] Through the aforementioned continuous event compression mechanism, a large number of repetitive or highly similar short-term event memories can be summarized into concise summaries. This significantly reduces the number of entries and storage bytes in long-term memory without losing key statistical trends, thus extending the residence time of effective experience memories. Actual compression rates can reach over 0.80, reducing overall storage footprint by approximately 30%, enabling the AI Agent to continuously accumulate historical operational experience over a longer period with limited Flash space.
[0089] S6: Telecommunications Fault Mode Memory and Dedicated Context Retrieval.
[0090] In one implementation, in the embedded small cell operation and maintenance scenario, a special memory processing is implemented for the collection of telecommunications-specific indicators, fault detection, pattern retrieval, and fault experience summarization. This enables the AI Agent to identify and memorize the fault modes of telecommunications equipment and automatically retrieve historical processing experience when similar faults occur in the future, thereby realizing the reuse of fault diagnosis and processing experience.
[0091] In the data structure of the memory system, to support specialized handling of telecommunications faults, four types of telecommunications fault enumeration values are predefined, including: VSWR_ANOMALY, voltage standing wave ratio abnormality, characterizing impedance matching problems in the antenna or feeder system; PA_OVERHEAT, power amplifier overheating, characterizing the PA power amplifier module temperature exceeding the limit; SFP_DEGRADATION, SFP optical module performance degradation, characterizing attenuation of optical module transmit or receive optical power; and CPRI_LINK_LOSS, CPRI fronthaul link loss, characterizing the interruption of the communication link between the baseband and RF units.
[0092] In the long-term memory SQLite database, a `telecom_fault_type` field (TEXT type) is added to both the `agent_memory_event` and `agent_memory_decision` tables to store the aforementioned fault type markers. In the circular buffers of short-term memory and reflective memory, the JSON structure of the memory entries also includes the `telecom_fault_type` field. This field is null (NULL or an empty string) in non-telecom fault scenarios, and takes one of the four enumeration values mentioned above in telecom fault scenarios.
[0093] During the Observe phase of the ReAct loop, the data collection engine libdaqdev.so collects telecom-specific metrics from local hardware interfaces and service software through various data collection adapters. The data collection paths include: the PMBUS adapter via PMBus over I... 2 The C interface acquires the VSWR value, PA temperature, and output power of the PA power amplifier module; the SFP_DDM adapter transmits data via SFP DDM over I... 2 The C interface collects the Tx / Rx optical power, bias current, and temperature of the optical module; the HW_REG adapter collects the CPRI / eCPRI link status and BER bit error rate through the memory-mapped register ( / dev / mem); the SW_COUNTER adapter reads the air interface signal quality indicators (RSRP, SINR, CQI, RTWP) through shared memory. The above telecommunications indicators are collected by the acquisition engine at a 1-minute cycle and transmitted to the Agent memory system through the shared memory ring buffer (DaqSample
[256] ).
[0094] When any telecommunications metric is detected to exceed a preset alarm threshold, the Agent calls the `agent_memory_record` function during the Observe phase to write the observation memory containing the anomaly information into a short-term memory circular buffer. The `content` field in the memory contains: device ID (`device_id`), fault type (`telecom_fault_type`), anomaly metric value, anomaly severity score (`anomaly_score`, a normalized value of 0 to 1), health score (`health_score`), and a timestamp. The importance score of this observation memory is automatically increased (not less than 0.7) due to the alarm severity bonus, ensuring that it is preferentially retained in long-term memory.
[0095] When the ReAct loop enters the Reason phase and the current context contains a valid telecom_fault_type, the context builder agent_memory_context_build() adds a telecom-specific retrieval step to the three-way parallel retrieval, including: (1) Time series extraction of telecommunications indicators from short-term memory. In the short-term memory circular buffer, the device ID and telecommunications fault type are used as filtering conditions. The device is scanned from the tail pointer to the head pointer to extract the 5 most recent observation records. If there are fewer than 5 observation records of the same type of fault in the short-term memory, the limit is relaxed to all observation records (regardless of fault type), ensuring that the context window length is no less than 5. The extracted content is arranged in ascending order of time to form a continuous change sequence of telecommunications indicators (such as the VSWR gradual change process: 1.5→2.1→2.8→3.2), which is used for trend analysis.
[0096] (2) Retrieval of historical fault records in long-term memory. An SQL query is constructed, using `telecom_fault_type` and `device_id` as join conditions, to retrieve all historical fault pattern records with an importance_score ≥ 0.7 from the `agent_memory_event` and `agent_memory_decision` tables. The query is accelerated using a composite index (`telecom_fault_type`, `device_id`), and the retrieval results are sorted by importance_score DESC, returning a maximum of 10 historical processing records. Each record includes the fault type, fault description, root cause analysis results, processing plan, and effect evaluation.
[0097] (3) Fault pattern induction retrieval in reflective memory. A linear scan of all 100 records in the reflective memory circular buffer is performed to retrieve reflective summaries containing the same telecom_fault_type tag in the content. The matched summaries are sorted from newest to oldest by write time, and the top 5 are injected into the context to provide heuristic experience and optimization suggestions for fault diagnosis.
[0098] After the retrieval is complete, the context builder applies a telecommunications relevance weighting coefficient to each memory in the merged list. If the memory's `telecom_fault_type` field value exactly matches the currently detected telecommunications fault type, its overall score is multiplied by a first weighting coefficient of 1.5 (type_match matching weighting); if the memory's `importance_score` field value is greater than or equal to 0.7, its overall score is multiplied by a second weighting coefficient of 1.3 (high importance memory weighting); memories that satisfy both of the above conditions have an overall weighting factor of 1.5 × 1.3 = 1.95.
[0099] The weighted composite score is used as a secondary key for sorting (the primary key is still the timestamp in descending order), ensuring that the most relevant historical experience of telecommunications failures is placed first in the context, so that the Agent prioritizes successful handling experience of the same equipment and the same type of failure, rather than generalized general recommendations.
[0100] Once the Agent executes the fault handling action and confirms successful handling, a telecom fault mode memory is automatically generated during the Feedback phase of the ReAct loop. The Agent summarizes key information from the fault detection and handling process into a structured summary, which includes: faulty device ID, fault type, time-series summary of abnormal indicators (such as initial value, peak value, rate of change, and duration), root cause analysis conclusion, handling solution description, and post-handling effect evaluation (such as indicators returning to normal values). The agent_memory_record(REFLECTION, content={...}) function is called to write the summarized fault mode summary into the reflective memory circular buffer, with a summary length not exceeding 256 bytes; the complete fault mode record is written to the agent_memory_event table (type marked as EVENT) in the long-term memory SQLite database, with its telecom_fault_type field set to the current fault type, and the initial value of importance_score set between 0.7 and 0.9 (determined based on fault severity and handling effect evaluation) to ensure that high-value fault experience is not prematurely eliminated by subsequent cleanup processes. The decision quality score (decision_quality) for each failure mode is synchronously updated to the user model's learned rule set (learned_rules) to optimize the decision weight allocation for subsequent similar failures of the device. If the same type of failure is successfully handled multiple times, the learning weight of the corresponding rule gradually accumulates and increases.
[0101] When the same type of telecommunications fault is detected again (by matching `telecom_fault_type` and `device_id`), the Agent automatically retrieves stored historical fault pattern records from long-term memory during the Reason phase using the telecommunications-specific retrieval step in `agent_memory_context_build()`. The context builder prioritizes injecting historically successful handling schemes into the inference context and labels the effectiveness scores of historical handling for the Agent to directly reference and reuse during decision-making. If the current fault characteristics match multiple historical fault records, the Agent selects the most similar historical case for recommendation based on the time-series similarity of fault indicators (such as the similarity of VSWR change rates).
[0102] Through the aforementioned telecommunications fault mode memory and dedicated context retrieval mechanism, the embedded AI Agent is equipped with the ability to accumulate and reuse fault experience in small cell operation and maintenance scenarios. The matching accuracy of four types of telecommunications fault modes, namely VSWR, SFP, PA, and CPRI, exceeds 90%, and the historical fault mode retrieval time is less than 5 milliseconds. When a telecommunications fault is detected, the Agent can automatically utilize historical operation and maintenance experience to avoid repeatedly trying invalid operation paths, thereby improving the efficiency of fault handling.
[0103] Example 2 This embodiment exemplifies an embedded AI Agent three-layer memory management and context retrieval system, deployed in an EU device (ARM architecture, 128MB RAM) and RU device (RISC-V architecture, 64MB RAM) in a small base station intelligent operation and maintenance scenario. The RU Agent runs with a 5MB memory budget. The system includes: a short-term memory module, a long-term memory module, a reflective memory module, a routing module, a context construction module, a cleanup module, and a compression module.
[0104] The short-term memory module is configured with a circular buffer with a fixed capacity of 100 entries, storing observational and reasoning memories, with a memory footprint of approximately 25KB. The long-term memory module is configured with an SQLite persistent database with a default capacity of 5000 entries, used for persistent storage of action-related memories, system-related memories, and fault mode records. The reflection memory module is configured with a circular buffer with a fixed capacity of 100 entries, storing feedback-related memories and fault mode summaries, with a memory footprint of approximately 25KB. All modules are integrated into the Agent main program as function libraries, and the total system memory footprint was measured to be approximately 2.8MB.
[0105] The short-term memory module uses a circular buffer as its storage medium, with a fixed capacity of 100 entries. Each memory entry is stored compactly according to its internal structure, occupying no more than 256 bytes. The buffer maintains a head pointer indicating the write position and a tail pointer indicating the read position. During writing, a lock-free atomic operation is used to update the pointers, and the time complexity for both reading and writing is O(1). When the buffer is full, the new entry automatically overwrites the oldest valid entry, achieving FIFO natural eviction.
[0106] In telecommunications operation and maintenance scenarios, the short-term memory module stores real-time telecommunications metrics collected by the libdaqdev.so acquisition engine at 1-minute intervals, including the VSWR and temperature of the PA power amplifier, the optical power and bias current of the SFP optical module, the CPRI link status and BER bit error rate, and air interface RSRP / SINR / CQI / RTWP metrics.
[0107] The long-term memory module uses the lightweight embedded SQLite database as its persistent storage engine, comprising three core tables: `agent_memory_event` for storing event-related memories, `agent_memory_decision` for storing decision-related memories, and `agent_memory_action` for storing action-related memories. Each table contains fields such as `item_id`, `type`, `timestamp`, `content` (JSON format), `importance_score`, `access_count`, `last_access_time`, `expires_at`, `compressed`, `device_id`, and `telecom_fault_type`. All three tables have a composite index consisting of the `timestamp`, `type`, and `importance_score` columns, with retrieval time controlled within 5ms. For telecommunications fault scenarios, an additional composite index (`telecom_fault_type`, `device_id`) is created. The long-term memory module stores historical data of telecommunications equipment (real-time indicators are retained for 7 days, aggregate indicators for 30 days, and alarms for 90 days) and records of four types of telecommunications fault modes (VSWR_ANOMALY, PA_OVERHEAT, SFP_DEGRADATION, and CPRI_LINK_LOSS). The initial value of importance_score for fault mode records is set to 0.7~0.9.
[0108] The reflexive memory module uses a streamlined circular buffer as its storage medium, with a fixed capacity of 100 entries, each not exceeding 256 bytes. It stores inductive reflexive summaries, including an abstract text summary, a decision quality score, and a telecom fault type flag. In telecom operations and maintenance scenarios, the reflexive memory stores fault mode summaries automatically summarized by the agent during the feedback phase, such as "VSWR gradual degradation is usually caused by antenna connector oxidation; prioritize checking the connector." It employs a FIFO (First-In, First-Out) natural attrition mechanism, maintaining a limit of 100 entries.
[0109] The routing module receives agent_memory_record(type, content, metadata) calls at each stage of the ReAct decision loop and performs route distribution based on the memory type, as shown in Table 1:
[0110] Routing is implemented using a switch-case structure, with a latency of less than 1 millisecond. For data types that simultaneously write to both long-term and short-term data types, transactional writes are used to ensure data consistency. For telecommunications failure mode memory, importance scoring is performed according to a formula. Calculate and ensure that the initial score is not lower than 0.7.
[0111] The context building module is triggered by agent_memory_context_build() in the Reason phase of the ReAct loop, returning a maximum of 4096 bytes of inference context. The execution flow is as follows: (1) Parallel retrieval in three directions, simultaneously retrieving from short-term memory (the last 5 observations), long-term memory (historical records of the same device, SQL index retrieval), and reflective memory (related summaries, linear scan); (2) Telecommunication relevance weighting, multiplying the memory matching the current fault type by 1.5 times, and multiplying the memory with importance ≥ 0.7 by 1.3 times; (3) Sorting, merging, and deduplication, sorting by timestamp in descending order and weighted score in descending order, and deduplicating by item_id; (4) Truncating the output, appending it one by one to the 4096-byte limit. The entire construction process takes less than 50 milliseconds.
[0112] The cleanup module is driven by a timerfd timer, calling agent_memory_cleanup() every 300 seconds. The execution process is as follows: (1) Check the available Flash space. If it is less than 10%, adjust the capacity limit to 3000 entries; otherwise, maintain 5000 entries. (2) Query the number of long-term memory entries. If it is not exceeded, skip it. (3) Sort by importance_score ASC and expires_at ASC, and delete the lowest 10% of the portion exceeding the capacity. (4) Execute VACUUM to reclaim storage space. The entire cleanup process takes less than 200 milliseconds. Short-term memory and reflective memory are naturally eliminated by the circular buffer FIFO without the need for cleanup module intervention.
[0113] After the compression module completes the cleanup, if the number of long-term memory entries exceeds 80% of the capacity limit, it will automatically call agent_memory_compress(). Execution process: (1) Scan the agent_memory_event table for uncompressed and unexpired event records; (2) Group events of the same device and type into a group with a time window of 600 seconds; (3) Generate a summary record for each group, take the highest importance_score in the original record and add the compression reward factor, and the summary shall not exceed 256 bytes; (4) Insert the summary record into the transaction and mark the original record as compressed; (5) Calculate the compression ratio. Actual compression ratios reached over 0.80, resulting in an overall reduction in storage space of approximately 30%.
[0114] During the initialization phase, the three-layer memory module completes resource allocation, and the cleanup module starts a timer. In the Observe phase, the routing module writes the observed memories into the short-term memory module, and the long-term memory module asynchronously persists key events. In the Reason phase, the context building module searches the three layers of memory in three ways, outputting the inference context for the agent's decision-making. In the Act phase, the routing module writes the decision actions into both long-term and short-term memory simultaneously. In the Feedback phase, the routing module writes the feedback memories into the reflection memory module, and the failure mode is simultaneously written into the long-term memory module. In periodic background tasks, the cleanup module eliminates low-value entries and reclaims space every 300 seconds, and the compression module merges similar events when the number of entries exceeds a threshold. Through this collaboration, the system achieves automatic classification and storage, efficient retrieval, adaptive cleanup, and intelligent compression of memories within a 5MB memory budget.
[0115] Other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of the invention are indicated by the claims. It should be understood that the invention is not limited to the precise structures described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the invention is limited only by the appended claims.
Claims
1. A three-layer memory management and context retrieval method for embedded AI agents, characterized in that, The method includes: A three-layer memory storage structure is initialized, comprising: a short-term memory circular buffer, a long-term memory persistent database, and a reflective memory circular buffer; the reflective memory circular buffer is used to store feedback-type memories and fault mode summaries. In each stage of the ReAct decision loop, memory entries are automatically routed to the corresponding memory layers for storage according to memory type, and importance scores are calculated. Among them, observational memories and reasoning memories are routed to the short-term memory circular buffer, action memories are routed to both the long-term memory persistence database and the short-term memory circular buffer, and feedback memories are routed to the reflective memory circular buffer. In the Reason phase of the ReAct loop, historical memories are retrieved in parallel from the short-term memory circular buffer, the long-term memory persistent database, and the reflective memory circular buffer. The retrieval results are merged, deduplicated, and truncated according to a preset size threshold, and the reasoning context is output. When the number of entries in the long-term memory persistence database exceeds the capacity limit or the storage medium space is insufficient, memory entries are deleted and storage space is reclaimed according to the importance score. Similar events within a continuous time window in the long-term memory persistence database are merged into a summary record; multiple event records falling within the same continuous time window are merged into a single summary record based on the same device identifier and the same memory type as similarity criteria, a compression status flag is set, the merged original records are deleted, and the importance score of the summary record is the highest value among the original records; In telecommunications equipment operation and maintenance scenarios, when an abnormal telecommunications indicator is detected, the observation memory containing the telecommunications fault type is written to the short-term memory circular buffer. In the Reason phase, based on the currently detected telecommunications fault type, historical processing records of the same telecommunications fault type are retrieved from the long-term memory persistent database. The time series of indicators before and after the fault occurrence are extracted from the short-term memory circular buffer. Relevant fault pattern summaries are retrieved from the reflective memory circular buffer, and the retrieval results are weighted by relevance and merged into the inference context. In the Feedback phase, after the fault is successfully handled, the telecommunications fault pattern is summarized, the fault pattern summary is written to the reflective memory circular buffer, and the complete fault pattern record is written to the long-term memory persistent database.
2. The embedded AI Agent three-layer memory management and context retrieval method according to claim 1, characterized in that, The importance score is calculated based on the following dimensions: memory freshness, anomaly severity, decision impact, and access frequency. The memory freshness is calculated based on the memory age according to an exponential decay relationship, the anomaly severity is determined based on the degree of anomaly in the observed data, the decision impact is determined based on the action execution result, and the access frequency is determined based on the number of times the memory is retrieved.
3. The embedded AI Agent three-layer memory management and context retrieval method according to claim 1, characterized in that, The relevance weighting of the search results includes: multiplying historical memories that completely match the current fault type by a first weighting coefficient, and multiplying memories with importance scores higher than a first threshold by a second weighting coefficient.
4. The embedded AI Agent three-layer memory management and context retrieval method according to claim 1, characterized in that, The step of merging and deduplicating the search results and then truncating them according to a preset size threshold includes: merging the results from the three-way search from the short-term memory circular buffer, the long-term memory persistent database, and the reflective memory circular buffer into a single list; deduplicating the results using the unique memory identifier; arranging the results in descending order by timestamp and importance score; and concatenating the results one by one into a text sequence until the preset size threshold is reached, discarding any excess.
5. The embedded AI Agent three-layer memory management and context retrieval method according to claim 1, characterized in that, When the available space of the storage medium is lower than the space shortage threshold, the upper limit of capacity is automatically adjusted to the preset shortage cleanup threshold.
6. The embedded AI Agent three-layer memory management and context retrieval method according to claim 1, characterized in that, The long-term memory persistence database is an SQLite database, which is configured with a composite index consisting of three columns: timestamp, memory type, and importance score, as well as a composite index consisting of fault type and device identifier.
7. An embedded AI Agent three-layer memory management and context retrieval system, used to implement the method as described in any one of claims 1-6, characterized in that, include: The short-term memory module is equipped with a circular buffer for storing observation-type memories and reasoning-type memories; The long-term memory module is equipped with a persistent database for persistently storing action-related memories, system-related memories, and fault mode records. The reflective memory module is configured with a circular buffer for storing feedback-type memories and failure mode summaries; The routing module is used to automatically route memory entries to the short-term memory module, the long-term memory module, or the reflective memory module for storage according to the memory type at each stage of the ReAct decision loop. The context building module is used to retrieve historical memories in parallel from the short-term memory module, the long-term memory module, and the reflective memory module during the Reason phase of the ReAct loop. The retrieval results are merged, deduplicated, and truncated according to a preset size threshold to output the reasoning context. The cleaning module is used to delete memory entries and reclaim storage space based on importance score when the number of entries in the long-term memory module exceeds the capacity limit or the storage medium space is insufficient. A compression module is used to merge similar events within a continuous time window in the long-term memory module into a summary record.
Citation Information
Patent Citations
Large model memory management and optimization system and method based on vector data lake
CN121501798A
Unified context and multi-level memory cooperation system and method for LLM intelligent agent
CN121960553A
