refactor: redesign context config with orthogonal model and add /compact command
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— 新增ContextConfig12 个正交字段,废弃旧字段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 providerastrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py—initialize()读取 12 个新字段,构建MainAgentBuildConfigastrbot/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.txtandpyproject.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.
@sourcery-ai review
/gemini review
/gemini review
@sourcery-ai review
/gemini review
[!CAUTION] The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.
@sourcery-ai review
@sourcery-ai review
@sourcery-ai review
[!CAUTION] The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.
@sourcery-ai review
Sorry @Rail1bc, your pull request is larger than the review limit of 150000 diff characters
重新整理历史