agentscope-ai / agentscope-ai/QwenPaw

[Bug]: Tool_call structure lost during context compaction, causing 400 errors / message count mismatch

未关闭
#5,856 4 条评论 0 个 reaction 已指派 1 人 已被 @qbc2016 认领 在 GitHub 查看
bug
主要语言
Python
星标
34.9k
派生
3.1k
平均合并
1 天 15 小时
30 天内合并 PR
225

描述

## QwenPaw Version

v1.1.12.post2

## Description

During context compaction, `LightContextManager.pre_reasoning()` passes all messages
(including `tool_use`/`tool_result` blocks) to `format_msgs_to_str`, which converts
them to plain text for LLM summarization. The structured tool_call data is permanently
lost after compaction. On the next request, the model receives orphaned `tool_result`
messages with no matching `tool_use`, causing 400 errors or `message count mismatch`.

**Expected:** Recent tool_use/tool_result message pairs should survive compaction
with their structured data intact.

**Actual:** All tool call history is flattened into a plain-text summary. The model
can no longer see the structured tool call context, leading to broken tool call
sequences and API errors.

**Related PR(s):** #(optional)

**Security considerations:** N/A

## Component(s) Affected

- [x] Core / Backend (app, agents, config, providers, utils, local_models)
- [ ] Console (frontend web UI)
- [ ] Channels (DingTalk, Feishu, QQ, Discord, iMessage, etc.)
- [ ] Skills
- [ ] CLI
- [ ] Documentation (website)
- [ ] Tests
- [ ] CI/CD
- [ ] Scripts / Deploy

## Environment

- **QwenPaw version:** v1.1.12.post2
- **OS:** Windows 10
- **Install method:** pip
- **Python version (if applicable):** 3.x

## Steps to Reproduce

1. Configure an agent with `context_manager_backend: light` and tool-calling enabled.
2. Have a long conversation that includes multiple tool calls (e.g. `get_current_time`).
3. Allow context to grow until compaction is triggered (`compact_threshold_ratio` exceeded).
4. After compaction, ask the agent to call a tool again.

## Actual vs Expected

- **Actual:** Post-compaction requests result in 400 errors (`Invalid 'content'`) or
`Assistant message count mismatch after formatting`, because `tool_result` messages
have no matching `tool_use` in the compacted context.
- **Expected:** The most recent tool call pairs should be preserved with full structure
after compaction.

## Logs / Screenshots

```
Assistant message count mismatch after formatting (N expected survivors, got M)
Invalid 'content': 'content' objects must have a 'type' field that is either
'text' or 'image_url'
```

## Additional Notes

Root cause: `pre_reasoning()` in `light_context_manager.py` calls
`format_msgs_to_str()` on all messages including tool call messages, converting
structured `tool_use`/`tool_result` blocks to plain text before passing to the
compaction LLM. The summary retains only narrative content (goals/progress/decisions),
permanently discarding tool call structure.

Suggested fix: Before compaction, extract the N most recent `tool_use`/`tool_result`
message pairs from `messages_to_compact` and move them to `messages_to_keep`, so they
are never passed through `format_msgs_to_str`.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。