Skip to content

refactor(ltm): redesign long-term memory with context compaction (reopen of #8144)#8226

Open
RC-CHN wants to merge 8 commits into
AstrBotDevs:masterfrom
RC-CHN:refactor-ltm
Open

refactor(ltm): redesign long-term memory with context compaction (reopen of #8144)#8226
RC-CHN wants to merge 8 commits into
AstrBotDevs:masterfrom
RC-CHN:refactor-ltm

Conversation

@RC-CHN
Copy link
Copy Markdown
Member

@RC-CHN RC-CHN commented May 18, 2026

reopen of #8144 , purify commit history

  • Add raw_records / contexts / summaries data model per group
  • Add LLM summary compaction strategy alongside truncation
  • Add turn-based (_split_into_rounds) granularity
  • Add image caption integration into LTM history
  • Add tool_call / tool_result persistence into raw_records
  • Add active reply support driven by LTM state
  • Improve summary injection prefix with system note and delimiters
  • Add info-level logging for summary compaction lifecycle
  • Clarify default summary prompt with explicit preserve/drop rules
  • Add context_guard for history overflow protection in agent runner
  • Add internal agent history compaction in agent_sub_stages
  • Add comprehensive LTM unit tests and compaction test suites

Modifications / 改动点

Core: astrbot/builtin_stars/astrbot/long_term_memory.py

  • Replace max_cnt ring buffer with raw_records (deque) + _raw_cursor + contexts (append-only list). Old segments are never rebuilt.
  • _build_segments() converts raw chat lines into OpenAI-format context segments, handling tool calls, parallel tools, and multi-step chains.
  • <BOT/> markers replace [You/] to avoid nickname collisions.
  • on_agent_done records tool-call chains and now includes the @bot prompt in contexts so future rounds see the user's original message.
  • asyncio.Lock for concurrency safety; remove_session() for cleanup.

Hook wiring: astrbot/builtin_stars/astrbot/main.py

  • Swap @on_llm_response@on_agent_done for accurate tool-chain recording.
  • Lazy toggle detection: false→true cleans stale state on next message.
  • group_icl_enable=true skips Conversation DB query (conversation=None).

Config: astrbot/builtin_stars/astrbot/default.py

  • Default context_limit_reached_strategy"llm_compress".

Agent runner: astrbot/core/astr_main_agent.py

  • _get_compress_provider auto-falls back to the main chat provider when llm_compress_provider_id is unset, preventing silent truncation.

Tests: tests/unit/test_long_term_memory.py

  • Pure functions: extract, parse, truncate, build_segments.

  • Integration: round-trip lifecycle, multi-round accumulation, tool chains, persona preservation, concurrent safety.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

- Add raw_records / contexts / summaries data model per group
- Add LLM summary compaction strategy alongside truncation
- Add turn-based (_split_into_rounds) granularity
- Add image caption integration into LTM history
- Add tool_call / tool_result persistence into raw_records
- Add active reply support driven by LTM state
- Improve summary injection prefix with system note and delimiters
- Add info-level logging for summary compaction lifecycle
- Clarify default summary prompt with explicit preserve/drop rules
- Add context_guard for history overflow protection in agent runner
- Add internal agent history compaction in agent_sub_stages
- Add comprehensive LTM unit tests and compaction test suites
@auto-assign auto-assign Bot requested review from advent259141 and anka-afk May 18, 2026 07:20
@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label May 18, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @RC-CHN, your pull request is larger than the review limit of 150000 diff characters

@dosubot dosubot Bot added area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. labels May 18, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant upgrade to the Long-Term Memory (LTM v2) system, implementing a more robust architecture for group chat context management. Key changes include the introduction of a RequestContextGuard to protect provider requests from token limits without mutating persistent history, and the implementation of dual compaction strategies (turn-based truncation and LLM-based summarization) for both group and private chats. Feedback identifies several critical issues: potential blocking of message recording due to holding locks across network calls, memory leaks from uncleaned session locks, and resource leaks from uncancelled tasks in the tool loop. Additionally, improvements were suggested for handling malformed JSON in tool arguments, preventing system prompt duplication, and ensuring consistent fallback logic for compression providers.

Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py Outdated
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py Outdated
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/core/astr_main_agent.py
Comment thread astrbot/core/agent/runners/tool_loop_agent_runner.py
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements LongTermMemory v2, introducing sophisticated context management for group chats, including logical round splitting and dual compaction strategies (truncation and LLM-based summarization). It also adds a RequestContextGuard to ensure provider requests stay within token limits without mutating canonical history. Feedback identifies several areas for refinement: the raw_records buffer needs trimming during message handling to prevent memory exhaustion, and robust error handling should be added to tool call parsing. Furthermore, the reviewer pointed out a memory leak in the session lock dictionary, suggested optimizing string length calculations for memory checks, and noted that the compression provider fallback logic needs to be correctly implemented to match the intended design.

Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py
Comment thread astrbot/builtin_stars/astrbot/long_term_memory.py Outdated
Comment thread astrbot/core/astr_main_agent.py
RC-CHN added 7 commits May 18, 2026 15:35
- Treat lines starting with <T:CALL>, <T:RES, or <BOT/ as regular user
  messages when their respective parsers return None, instead of silently
  dropping them. Defensive guard against malformed internal markers.
Avoid allocating a new bytes object for every string when calculating
buffer size in _trim_raw_records. Character count is sufficient for
the approximate memory cap.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant