anthropics / anthropics/claude-agent-sdk-python

PreToolUse hooks not called for Read tool when file doesn't exist

Abierto
#316 4 comentarios 0 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Python
Estrellas
8.1k
Forks
1.3k
Merge medio
2 d 31 min
PR fusionados (30 d)
1

Descripción

PreToolUse hooks are **not invoked** when Claude calls the `Read` tool with a file path that doesn't exist. The hook is only called when the file exists. This contradicts the [official documentation](https://code.claude.com/docs/en/hooks) which states that PreToolUse hooks execute "after Claude creates tool parameters and **before processing the tool call**".

This breaks legitimate use cases like path translation where hooks need to modify file paths before validation occurs.

## Expected Behavior

According to the documentation, PreToolUse hooks should be called **before** the tool processes the call, allowing hooks to:
- Validate inputs
- Modify tool parameters using `updatedInput`
- Block execution with `permissionDecision: "deny"`

The hook should be invoked **regardless of whether the file exists**.

## Actual Behavior

When Claude calls `Read` with a non-existent file path:
1. The SDK validates that the file doesn't exist
2. The SDK returns "File does not exist" error
3. **The PreToolUse hook is never called**

When Claude calls `Read` with an existing file path:
1. The PreToolUse hook is called ✅
2. The file is read successfully ✅

## Reproduction

See the minimal reproduction script: [reproduce_hook_bug.py](https://gist.github.com/feroult/731c564f7920859d9dec2e47a358fa11)

**Run it:**
```bash
pip install claude-agent-sdk
python reproduce_hook_bug.py
```

**Expected output:**
```
[HOOK CALLED] for TEST 1 (/workspace/test.txt)
[HOOK CALLED] for TEST 2 (/tmp/hook_test/test.txt)
```

**Actual output:**
```
[HOOK CALLED] for TEST 2 only (/tmp/hook_test/test.txt)
```

## Environment

- **Claude Code**: 2.0.35 (latest)
- **claude-agent-sdk**: 0.1.6 (latest - tested on 0.1.4, 0.1.5, 0.1.6)
- **Python**: 3.11
- **OS**: Linux

## Impact

This bug breaks:
1. **Path translation hooks** - Cannot translate paths like `/workspace/*` to actual locations before validation
2. **Custom file resolution** - Cannot implement custom file lookup logic
3. **Input sanitization** - Cannot normalize/validate file paths before they're checked
4. **Audit logging** - Cannot log all Read attempts, only successful ones

The `updatedInput` feature is essentially useless for file path modification because hooks don't fire early enough.

## Workaround

None found. The only way to make hooks work is to ensure files exist at the exact paths Claude uses, which defeats the purpose of path translation hooks.

## Related Issues

Similar to #7082 but different - that issue was about permission deny rules, this is about hooks not being called at all for non-existent paths.

Guía de contribución

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

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.