anthropics / anthropics/claude-code

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

Aperta
#93,998 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
area:core bug platform:wsl
Lingua principale
Python
Stelle
145k
Fork
23.1k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Ambito
api, cli
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Attiva
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.