agentscope-ai / agentscope-ai/QwenPaw

[Bug] RetryChatModel hardcoded 32768 context_size fallback causes CONTEXT_UNFIT (>31130 tokens) for all models

Open
#7,576 5 comments 0 reactions 1 assignee Claimed by @wangfei010313 View on GitHub
Dominant language
Python
Stars
34.9k
Forks
3.1k
Avg merge
1d 15h
Merged PRs (30d)
225

Description

## QwenPaw Version
v2.1.0 through v2.2.0 (confirmed buggy in all published releases)

## Description
The `RetryChatModel.__init__` in `src/qwenpaw/providers/retry_chat_model.py` contains a hardcoded fallback that forces ALL models to use a 32768 token context window:

```python
context_size=getattr(inner, "context_size", 32768),
model=getattr(inner, "model", "unknown"),
```

This means:
- Even models configured with `max_input_length=131072` (128K) in agent.json get capped at 32768
- The compaction hard limit becomes `32768 × 0.95 = 31130 tokens`
- When conversation context exceeds ~31K tokens, QwenPaw throws `MODEL_CONTEXT_LENGTH_EXCEEDED` / `CONTEXT_UNFIT`
- Error message shows `model 'unknown'` because `getattr(inner, "model", "unknown")` also defaults

## Error Log Example
```
ContextWindowUnfitError: CONTEXT_UNFIT: context compaction could not fit the active request into the model input window (31430 > 31130 tokens)
```

First observed: 2026-08-23 16:35:41 UTC on hosted QwenPaw (AgentScope platform)

## Root Cause Analysis
The `get_model_max_input_length()` function in `src/qwenpaw/config/config.py` was updated (likely in PR #5586) to query `ProviderManager.get_active_model()`, but this only affects the **display** of context usage. The actual compaction hard limit comes from `agent.model.context_size` in `manager.py`, which reads from the `RetryChatModel` wrapper — and that wrapper still has the hardcoded 32768 fallback.

Evidence:
- `src/qwenpaw/agents/context/scroll/manager.py` line ~16566:
```python
hard_limit = int(agent.model.context_size)
```
- `src/qwenpaw/providers/retry_chat_model.py` line ~13518:
```python
context_size=getattr(inner, "context_size", 32768),
```

Both are **identical** across v2.1.0, v2.2.0-beta.1, and v2.2.0. The bug persists in all published releases.

## Verification
Checked source code for:
- v2.1.0: ❌ BUGGY (32768 fallback present)
- v2.2.0-beta.1 (2026-08-27): ❌ BUGGY (32768 fallback present)
- v2.2.0 (2026-09-03): ❌ BUGGY (32768 fallback present)
- main branch: ❌ BUGGY (32768 fallback present)

PR #5586 is still open and unmerged, but it only fixes the config-level function, NOT the RetryChatModel wrapper.

## Expected Behavior
Models configured with larger context windows (e.g., 128K via `max_input_length: 131072`) should have those values respected. The 32768 fallback should only apply when no context size can be determined, not as a global cap.

## Workarounds Used
1. Start fresh chat sessions before context reaches ~31K tokens
2. Self-host QwenPaw v2.0.1 (which predates this regression)

## Requested Fix
Remove the hardcoded `32768` default in `RetryChatModel.__init__` and properly resolve the inner model's context size from the provider configuration.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.