AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

refactor: redesign context config with orthogonal model and add /compact command

Open Rail1bc opened this issue 4 months ago • 7 comments

Motivation / 动机

重构上下文管理配置模型,使其更加正交、可组合,并支持主动触发压缩。解决 #9252、#8348、#9281 等 issue 的实际需求。

核心改动:

  • ContextConfig:7 个旧字段(隐式值:-1 = 不限制、<=0 = 关闭)→ 12 个正交字段(显式 bool 开关,分离触发条件 WHEN 与处置行为 WHAT),并新增 summary_provider 字段
  • MainAgentBuildConfig:新增正交字段,旧字段标记为废弃
  • provider_settings 默认值:替换为新的正交默认值
  • 迁移:_migra_context_config() + _validate_context_config() 接入 migra(),覆盖全部旧到新的字段映射,自动迁移,向后兼容
  • Dashboard 国际化:3 个语言文件(zh-CN/en-US/ru-RU)更新为 context_management 结构,共 13 个新字段翻译
  • /compact 命令:新增手动触发压缩,读取对话历史 → 调用 ContextManager.process() → 写回 DB。第三方 runner 跳过,异常保留上下文,群聊非隔离默认仅管理员
  • 测试:67 个新测试(ContextConfig、ContextManager、迁移、构建配置)+ /compact 回归测试。108 passed, 0 failed

Modifications / 改动点

重构图(ContextConfig 正交化):

  • astrbot/core/agent/context/config.py — 新增 ContextConfig 12 个正交字段,废弃旧字段
  • astrbot/core/agent/context/manager.py — 重写 process(),统一 trigger → disposal → retention → double-check 流程
  • astrbot/core/agent/context/__init__.py — 导出更新
  • astrbot/core/agent/runners/tool_loop_agent_runner.py — 使用新 ContextManager 替换旧压缩逻辑
  • astrbot/core/main_agent_build_config.py — MainAgentBuildConfig 新增正交字段
  • astrbot/core/astr_main_agent.py — 构建新 ContextConfig,_get_compress_provider() 解析 summary provider
  • astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py — initialize() 读取 12 个新字段,构建 MainAgentBuildConfig
  • astrbot/core/config/default.py — provider_settings 默认值替换
  • astrbot/core/config/migration.py — _migra_context_config() 旧→新字段映射迁移, _validate_context_config() 校验

国际化:

  • astrbot/dashboard/api/i18n/zh-CN.json — 更新 context_management 翻译(13 字段)
  • astrbot/dashboard/api/i18n/en-US.json — 更新
  • astrbot/dashboard/api/i18n/ru-RU.json — 更新

主动触发(/compact 命令):

  • astrbot/builtin_stars/builtin_commands/commands/conversation.py — 新增 compact() 方法

  • astrbot/builtin_stars/builtin_commands/main.py — 注册 @filter.command("compact")

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

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

测试结果:108 passed, 0 failed, 9 skipped(旧 API 跳过)

测试文件 新增 覆盖内容
tests/agent/test_context_config_new.py 10 ContextConfig 默认值、字段校验、序列化/反序列化
tests/agent/test_context_manager_new.py 30 process() 完整流程:触发 → 处置 → 保留 → 双检
tests/unit/test_context_migration.py 12 旧→新字段映射 6 种、混合状态、无需迁移;校验 8 种警告
tests/unit/test_main_agent_build_config_new.py 4 新字段默认值、废弃字段兼容性
tests/agent/test_context_manager.py 67(扩增) 含旧兼容测试,总计 104

覆盖路径:

  • ✅ ContextConfig 全部 12 字段默认值 + 显式赋值
  • ✅ Trigger: enable_turn_limit / enable_token_guard(含阈值边界 0.5~0.99)
  • ✅ Disposal: summary → discard fallback / enable_summary=False → discard
  • ✅ Retention: turns / percentage / null 三种方法
  • ✅ Double-check: 处置后仍超阈值时无条件截半
  • ✅ 错误处理:压缩异常返回原消息,不断言不静默修改
  • ✅ 迁移:全部 6 个旧字段到新字段映射 + 自动检测已迁移状态
  • ✅ 校验:8 种非法/警告配置独立验证

Checklist / 检查清单

  • [x] 😊 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. / 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"。

  • [x] 🤓 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.txt 和 pyproject.toml 文件相应位置。

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

Summary by Sourcery

Redesign context management into an orthogonal trigger/disposal model, add a manual /compact command for conversation compression, and wire the new configuration through agents, runners, defaults, migration, and UI metadata with comprehensive tests.

New Features:

  • Introduce a /compact command that manually triggers context processing and compression for the current conversation with appropriate permission checks and third-party runner safeguards.
  • Expose new orthogonal context management fields (trigger, disposal, and retention) in MainAgentBuildConfig and provider_settings, including summary provider selection and retention strategies.

Bug Fixes:

  • Ensure context processing gracefully handles errors and corrupted or missing conversation history by returning user-friendly messages and preserving original messages when compression fails.

Enhancements:

  • Refactor ContextConfig and ContextManager to use explicit boolean triggers, separate token and turn-based conditions, unified disposal (summary, discard, or custom compressor), retention constraints, and a double-check halving step based on provider context tokens.
  • Add automatic migration and validation from legacy context-related settings to the new orthogonal configuration model during config migration.
  • Update the internal agent runner to construct and use the new ContextConfig, including provider-level max_context_tokens for token guard, and pass the new fields from MainAgentBuildConfig.
  • Extend dashboard configuration metadata and i18n keys to represent the new context_management structure and fields.
  • Refactor conversation reset command permission handling into a reusable helper shared with the new /compact command.

Tests:

  • Add extensive unit and integration tests for the redesigned ContextManager behavior, new ContextConfig defaults and constraints, context config migration and validation, and the new /compact command flow including permissions, third-party runner skipping, history preservation, and error handling.
  • Update existing context manager tests to align with the new API and mark obsolete scenarios as skipped while retaining backward-compatibility coverage.

Rail1bc avatar Jul 21 '26 09:07 Rail1bc

@sourcery-ai review

Rail1bc avatar Jul 21 '26 10:07 Rail1bc

/gemini review

Rail1bc avatar Jul 21 '26 10:07 Rail1bc

/gemini review

Rail1bc avatar Jul 21 '26 10:07 Rail1bc

@sourcery-ai review

Rail1bc avatar Jul 21 '26 10:07 Rail1bc

/gemini review

Rail1bc avatar Jul 22 '26 01:07 Rail1bc

[!CAUTION] The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

gemini-code-assist avatar Jul 22 '26 01:07 gemini-code-assist

@sourcery-ai review

Rail1bc avatar Jul 22 '26 01:07 Rail1bc

@sourcery-ai review

Rail1bc avatar Jul 22 '26 02:07 Rail1bc

@sourcery-ai review

Rail1bc avatar Jul 22 '26 02:07 Rail1bc

[!CAUTION] The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

gemini-code-assist avatar Jul 22 '26 02:07 gemini-code-assist

@sourcery-ai review

Rail1bc avatar Jul 22 '26 03:07 Rail1bc

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

sourcery-ai[bot] avatar Jul 22 '26 03:07 sourcery-ai[bot]

重新整理历史

Rail1bc avatar Jul 22 '26 03:07 Rail1bc