Skip to content

fix: migrate legacy llm_compress_keep_recent config - #10173

Open
iuiu-py wants to merge 2 commits into
AstrBotDevs:masterfrom
iuiu-py:fix/migrate-compression-recent-ratio
Open

iuiu-py wants to merge 2 commits into
AstrBotDevs:masterfrom
iuiu-py:fix/migrate-compression-recent-ratio

Conversation

@iuiu-py

@iuiu-py iuiu-py commented Sep 21, 2026

Copy link
Copy Markdown

Fixes #8598

Motivation

Upgrades could leave the legacy provider_settings.llm_compress_keep_recent count in place without populating the replacement ratio. The recent-context preference was therefore lost when the new compression configuration was used.

Changes

  • Convert the legacy recent-message count to keep_recent_ratio using the legacy max_context_length denominator.
  • Clamp the migrated ratio to the supported 0.0-0.3 range and fall back to 0.15 when the legacy count is invalid or cannot be expressed against an unbounded/invalid limit.
  • Preserve an existing ratio when both keys are present. For an already-migrated local runner, populate a missing runner ratio before removing the legacy input.
  • Add coverage for legacy-only migration, both-key precedence, invalid legacy values, and preserving an existing runner ratio.

Testing

  • uv run pytest tests/unit/test_agent_runner_config.py -q - 45 passed
  • uv run pytest tests/unit/test_agent_runner_config.py tests/unit/test_config.py -q - 116 passed
  • uv run ruff format --check .
  • uv run ruff check .
  • git diff --check

Summary by Sourcery

Preserve recent-context compression preferences when migrating legacy agent runner configuration.

Bug Fixes:

  • Migrate the legacy recent-message compression count into the new bounded keep-recent ratio so upgrades preserve context preferences.

Enhancements:

  • Preserve explicitly configured ratios and populate missing ratios in existing local agent runners while removing legacy settings.
  • Use a safe default for invalid or unsupported legacy values and cap migrated ratios at the supported maximum.

Tests:

  • Add coverage for legacy count conversion, ratio precedence, invalid-value fallback, and existing runner migration.

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


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

Comment on lines +164 to +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

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.

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.

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

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.

fix: migrate legacy llm_compress_keep_recent config

1 participant