anthropics / anthropics/claude-code

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

Abierto
#93,998 0 comentarios 0 reacciones 0 asignados Ver en GitHub
area:core bug platform:wsl
Lenguaje dominante
Python
Estrellas
145k
Forks
23.1k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Área
api, cli
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Activo
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.