anthropics / anthropics/claude-code

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

Ouverte
#93,998 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
area:core bug platform:wsl
Langage dominant
Python
Étoiles
145k
Forks
23.1k
Métriques de merge des PR
Métriques de PR en attente

Description

### 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_

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Domaine
api, cli
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Active
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.