Conversation
There was a problem hiding this comment.
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/utils/migra_helper.py" line_range="164-169" />
<code_context>
+ if legacy_count_value is None:
+ return _DEFAULT_KEEP_RECENT_RATIO
+
+ try:
+ legacy_count = int(legacy_count_value)
+ except (TypeError, ValueError):
+ return _DEFAULT_KEEP_RECENT_RATIO
+ if isinstance(legacy_count_value, bool) or legacy_count < 0:
+ return _DEFAULT_KEEP_RECENT_RATIO
</code_context>
<issue_to_address>
**issue (bug_risk):** A boolean `max_context_length` is converted to `1` instead of being rejected, so `True` causes the legacy count to be divided by one and commonly produces the maximum ratio `0.3` instead of the invalid-limit fallback `0.15`.
**Triggers:** When the legacy configuration contains a boolean `max_context_length`.
**Suggested fix:** Reject boolean values for `max_context_length` before converting it to an integer.
```suggestion
if isinstance(provider_settings.get("max_context_length", -1), bool):
return _DEFAULT_KEEP_RECENT_RATIO
try:
max_turns = int(provider_settings.get("max_context_length", -1))
except (TypeError, ValueError):
return _DEFAULT_KEEP_RECENT_RATIO
if max_turns <= 0:
return _DEFAULT_KEEP_RECENT_RATIO
```
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the count-to-ratio conversion is wrong, the migrated compression ratio is persisted in the agent-runner configuration and remains after reverting the code, so subsequent compression behavior can be incorrect. The setting is bounded and can be repaired by updating or re-running configuration migration; it does not delete data or trigger an irreversible external action.
Blocking findings: astrbot/core/utils/migra_helper.py:169
| try: | ||
| max_turns = int(provider_settings.get("max_context_length", -1)) | ||
| except (TypeError, ValueError): | ||
| return _DEFAULT_KEEP_RECENT_RATIO | ||
| if max_turns <= 0: | ||
| return _DEFAULT_KEEP_RECENT_RATIO |
There was a problem hiding this comment.
issue (bug_risk): A boolean max_context_length is converted to 1 instead of being rejected, so True causes the legacy count to be divided by one and commonly produces the maximum ratio 0.3 instead of the invalid-limit fallback 0.15.
Triggers: When the legacy configuration contains a boolean max_context_length.
Suggested fix: Reject boolean values for max_context_length before converting it to an integer.
| try: | |
| max_turns = int(provider_settings.get("max_context_length", -1)) | |
| except (TypeError, ValueError): | |
| return _DEFAULT_KEEP_RECENT_RATIO | |
| if max_turns <= 0: | |
| return _DEFAULT_KEEP_RECENT_RATIO | |
| if isinstance(provider_settings.get("max_context_length", -1), bool): | |
| return _DEFAULT_KEEP_RECENT_RATIO | |
| try: | |
| max_turns = int(provider_settings.get("max_context_length", -1)) | |
| except (TypeError, ValueError): | |
| return _DEFAULT_KEEP_RECENT_RATIO | |
| if max_turns <= 0: | |
| return _DEFAULT_KEEP_RECENT_RATIO |
Fixes #8598
Motivation
Upgrades could leave the legacy
provider_settings.llm_compress_keep_recentcount in place without populating the replacement ratio. The recent-context preference was therefore lost when the new compression configuration was used.Changes
keep_recent_ratiousing the legacymax_context_lengthdenominator.0.0-0.3range and fall back to0.15when the legacy count is invalid or cannot be expressed against an unbounded/invalid limit.Testing
uv run pytest tests/unit/test_agent_runner_config.py -q- 45 passeduv run pytest tests/unit/test_agent_runner_config.py tests/unit/test_config.py -q- 116 passeduv run ruff format --check .uv run ruff check .git diff --checkSummary by Sourcery
Preserve recent-context compression preferences when migrating legacy agent runner configuration.
Bug Fixes:
Enhancements:
Tests: