Skip to content

feat: make context compression trigger ratio configurable - #10728

Open
iona-s wants to merge 2 commits into
AstrBotDevs:masterfrom
iona-s:master
Open

iona-s wants to merge 2 commits into
AstrBotDevs:masterfrom
iona-s:master

Conversation

@iona-s

@iona-s iona-s commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Context compression and truncation are triggered when context tokens exceed a hard-coded 82% of the model context window (compression_threshold = 0.82 in both compressors). It cannot be changed without editing the source, so large-window models only compress very late, and users cannot compress earlier to save tokens. The only workaround is lowering the provider's context window, which also changes the real window for that provider.

Closes #9252. Partially addresses #8348 (ratio-based only; an absolute token threshold is not included). This is a smaller take on the same need as #9261, which was closed as stale.

Modifications / 改动点

  • Add agent_runner.config.compression.trigger_ratio (default 0.82, clamped to 0.1–0.95 during Agent Runner normalization).

  • Map it through resolve_context_compression_config → MainAgentBuildConfig.context_compression_threshold → ToolLoopAgentRunner.reset → ContextConfig.compression_threshold, following the existing keep_recent_ratio path. Chat, Cron and background wakeups all use the shared mapping.

  • ContextManager clamps the value again and passes it to both LLMSummaryCompressor and TruncateByTurnsCompressor, so both strategies and the post-compression check honor it. A custom_compressor keeps its own threshold.

  • Show the setting in WebUI under the context management strategy for the local runner, with en-US / zh-CN / ja-JP / ru-RU translations.

  • Update docs/zh and docs/en context-compress.md, which previously stated a fixed ~82%.

  • Existing configs pick up the default 0.82 through normalization, so behavior is unchanged unless the setting is modified.

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

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

  • New tests: normalization/clamping and mapping of trigger_ratio, the threshold reaching both compressors, a lower ratio compressing at usage where the default does not, and build_main_agent forwarding the value.
  • Affected suites: tests/unit/test_agent_runner_config.py, tests/agent/test_context_manager.py, tests/unit/test_astr_main_agent.py, tests/unit/test_cron_context_compression.py, tests/test_tool_loop_agent_runner.py — 351 passed.
  • Full suite on the change merged with master: 4742 passed, 79 skipped.
  • ruff check and ruff format --check pass on the changed Python files.

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 checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog.
    / 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新 docs/zh 和 docs/en 的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。

  • 🤓 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 文件相应位置。

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

Summary by Sourcery

Make the context compression trigger ratio configurable so users can control when context compression or truncation begins without changing the provider context window.

New Features:

  • Add a configurable context compression trigger ratio for local agent runners, defaulting to 0.82 and supporting values from 0.1 to 0.95.
  • Expose the compression trigger ratio in the WebUI and document its configuration and behavior in English and Chinese.

Enhancements:

  • Apply the configured ratio consistently to truncation and LLM summarization while preserving custom compressor behavior.
  • Propagate and validate the setting across agent runner configuration and shared context management flows.

Documentation:

  • Update context compression documentation to describe the configurable trigger ratio and its default behavior.

Tests:

  • Add coverage for trigger-ratio defaults, clamping, propagation, and earlier compression behavior.

Context compression and truncation were triggered at a hard-coded 82% of the
model context window. Add `agent_runner.config.compression.trigger_ratio`
(default 0.82, clamped to 0.1-0.95) and pass it through the build config,
the tool loop runner and ContextManager to both compressors, so chat, Cron
and background wakeups all honor it. Expose the setting in the WebUI with
translations and update the context compression docs.

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/agent/context/config.py" line_range="32" />
<code_context>
     """Percent of current context tokens to keep as exact recent context during LLM-based compression."""
     llm_compress_provider: "Provider | None" = None
     """LLM provider used for compression tasks. If None, truncation strategy is used."""
+    compression_threshold: float = 0.82
+    """Share of max_context_tokens above which the context is compressed. Clamped to 0.1-0.95."""
     custom_token_counter: TokenCounter | None = None
</code_context>
<issue_to_address>
**Existing positional callers break**

When callers pass existing optional positional arguments to `ContextConfig` or `ToolLoopAgentRunner.reset`, `compression_threshold` shifts the positional bindings, so `ContextManager` can fail converting a former `custom_token_counter` argument to `float`, and `reset` can interpret a former `truncate_turns` argument as the threshold and use unintended truncation settings.

Move the new parameters after the existing positional parameters or make them keyword-only.

Also at `astrbot/core/agent/runners/tool_loop_agent_runner.py:226`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: astrbot/core/agent/context/config.py:32


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/agent/context/config.py Outdated
Move the new compression threshold to the end of ContextConfig, MainAgentBuildConfig and ToolLoopAgentRunner.reset so callers passing earlier optional arguments positionally keep their bindings.

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

Sourcery assessment

Approved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 上下文压缩阈值可配置化

1 participant