anthropics / anthropics/claude-code

[BUG] Context prune can leave conversation in unrecoverable 400 loop

未关闭
#93,998 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
area:core bug platform:wsl
主要语言
Python
星标
145k
派生
23.1k
PR 合并指标
PR 指标待抓取

描述

### Preflight Checklist

- [x] I have searched [existing issues](https://github.com/anthropics/claude-code/issues?q=is%3Aissue%20state%3Aopen%20label%3Abug) and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code

### What's Wrong?

After a context prune (either automatic compaction or manual), the conversation history can end on an assistant message. When the inference endpoint rejects trailing assistant messages, every subsequent request fails with 400. Claude Code then appends the error as an assistant message, ensuring the next request also ends on assistant - creating a permanent, self-reinforcing failure loop.

### Observed behavior

```
user ...
assistant "API Error: 400 Bad Request"
user ...
assistant "API Error: 400 Bad Request"
... (13+ iterations observed)
```

The inference endpoint log shows zero incoming requests during these failures.

### Environment

- Backend: custom inference endpoint (localhost proxy)
- The direct Anthropic API accepts trailing assistant messages (prefill), so this only manifests with custom endpoints

### What Should Happen?

### Expected behavior
1. Context prune should validate message structure before committing (ensure no trailing assistant message that could be rejected by the endpoint)
2. Repeated identical API errors (same status, same message) should halt after N retries rather than accumulating indefinitely
3. Background hooks should stop firing when the session is in an error state

### Error Messages/Logs

```shell

```

### Steps to Reproduce

1. Use Claude Code with a custom inference endpoint that requires the last message to have role=user
2. Run a long session until context compaction triggers
3. If the compaction leaves the history ending on an assistant message, every subsequent turn returns 400
4. Background hooks keep firing, each adding another failed assistant turn
5. The session becomes unrecoverable - even /exit and --resume replays the same invalid history

### Claude Model

Other

### Is this a regression?

I don't know

### Last Working Version

_No response_

### Claude Code Version

v2.1.269

### Platform

Anthropic API

### Operating System

Windows

### Terminal/Shell

WSL (Windows Subsystem for Linux)

### Additional Information

_No response_

贡献指南

这个仓库没有索引到贡献指南

调研方向

Review the context-prune/compaction path and the error/retry and background-hook entry points described in the report; no specific files or tests are named. Use a custom endpoint that rejects trailing assistant messages to verify the failure, then confirm pruning avoids invalid history, repeated identical 400s stop, and hooks stop in the error state.

由索引模型根据 Issue 内容生成。

评估

领域
api, cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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