Skip to content

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

Open
Rail1bc wants to merge 6 commits into
AstrBotDevs:devfrom
Rail1bc:refactor/context-config
Open

refactor: redesign context config with orthogonal model and add /compact command#9346
Rail1bc wants to merge 6 commits into
AstrBotDevs:devfrom
Rail1bc:refactor/context-config

Conversation

@Rail1bc

@Rail1bc Rail1bc commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

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

核心变更

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

Modifications / 改动点

文件 变更说明
astrbot/core/agent/context/config.py ContextConfig 12 个正交字段,废弃旧字段
astrbot/core/agent/context/manager.py 重写 process():trigger → disposal → retention → double-check
astrbot/core/agent/runners/tool_loop_agent_runner.py 使用新 ContextManager 替换旧压缩逻辑
astrbot/core/astr_main_agent.py 构建新 ContextConfig,解析 summary provider
astrbot/core/pipeline/process_stage/method/agent_sub_stages/internal.py 读取新字段构建 MainAgentBuildConfig
astrbot/core/config/default.py provider_settings 默认值替换,新增 CONFIG_METADATA_3
astrbot/core/utils/migra_helper.py _migra_context_config() + _validate_context_config()
dashboard/src/i18n/locales/en-US/features/config-metadata.json 13 个 context_management 字段翻译
dashboard/src/i18n/locales/ru-RU/features/config-metadata.json 13 个 context_management 字段翻译
dashboard/src/i18n/locales/zh-CN/features/config-metadata.json 13 个 context_management 字段翻译
astrbot/builtin_stars/builtin_commands/commands/conversation.py 新增 /compact 命令 + 共享 helpers
astrbot/builtin_stars/builtin_commands/main.py 注册 @filter.command("compact")
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Test Results

220 passed, 6 failed, 9 skipped in 8.30s

(6 failed 为预先存在的测试失败,非本 PR 引入)

测试文件 覆盖内容
tests/agent/test_context_config_new.py ContextConfig 默认值、字段校验、序列化/反序列化
tests/agent/test_context_manager_new.py process() 完整流程:触发→处置→保留→双检
tests/unit/test_context_migration.py 旧→新字段映射 6 种、混合状态、无需迁移;校验 8 种警告
tests/unit/test_main_agent_build_config_new.py 新字段默认值、废弃字段兼容性
tests/agent/test_context_manager.py 旧兼容测试(扩增)
tests/test_conversation_commands.py /compact 权限/错误处理/压缩场景/元数据保留
tests/unit/test_astr_main_agent.py 新字段兼容

覆盖路径:

  • ✅ 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

  • 😊 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.
    / 我的更改没有引入恶意代码。

Rail1bc and others added 6 commits July 22, 2026 11:31
…rigger/disposal model

Replace the old implicit-value config (-1 = no limit, <=0 = disabled) with
explicit bool switches and a clean separation between trigger conditions
(WHEN) and disposal behaviors (WHAT).

Core changes:
- ContextConfig: 7 old fields → 12 orthogonal fields + summary_provider
- ContextManager.process(): independent trigger checks → unified disposal
  (summary first, discard fallback) → retention constraint → double-check
- Add _migra_context_config() + _validate_context_config() migration helpers
  for zero-downtime config upgrade

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
… agent pipeline

- Add new context_management config fields to MainAgentBuildConfig
- Update default config with new orthogonal trigger/disposal defaults
- Thread context_config through ToolLoopAgentRunner and agent sub-stages
- Update existing tests to match new config field names and defaults

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
…ocales

Replace old truncate_and_compress keys with new context_management
field translations covering all 13 new trigger/disposal fields.

Updates zh-CN, en-US, and ru-RU locale files.

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
Add comprehensive test suites for:
- ContextConfig validation and field defaults
- Context migration helpers (_migra_context_config, _validate_context_config)
- MainAgentBuildConfig new context_management fields

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
Add a /compact command that manually triggers ContextManager.process()
on the current conversation, applying the orthogonal trigger/disposal
model (summary first, discard fallback, retention bound, double-check)
without fully clearing the conversation history.

Includes shared helper extraction for /compact and /reset commands,
robustness fixes (json corruption, None guards, multimodal content),
and migration retention_method bug fix.

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
Add comprehensive tests for:
- ContextManager.process() with all disposal strategies
- /compact command: permission, error handling, compression scenarios
- Tool call metadata preservation across conversation round-trips
- Edge cases: empty history, no-conversation, compressor failures

Co-Authored-By: deepseek-v4-flash <deepseek-ai@claude-code-best.win>
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

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

@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jul 22, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, we are unable to review this pull request

The GitHub API does not allow us to fetch diffs exceeding 300 files, and this pull request has 1064

@dosubot dosubot Bot added the area:core The bug / feature is about astrbot's core, backend label Jul 22, 2026
@Rail1bc
Rail1bc changed the base branch from master to dev July 22, 2026 04:01
@Rail1bc Rail1bc changed the title Refactor/context config refactor: redesign context config with orthogonal model and add /compact command Jul 22, 2026
@Rail1bc

Rail1bc commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

@sourcery-ai

sourcery-ai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

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

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 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