anthropics / anthropics/claude-agent-sdk-python

Long project paths can resolve to the wrong session directory

Aperta
#1,160 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
8.1k
Fork
1.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

### Summary

For project paths whose sanitized form exceeds 200 characters, session discovery can select a directory belonging to a different project when the SDK-computed hash does not match the CLI-computed hash.

### Affected code

- `src/claude_agent_sdk/_internal/sessions.py:157-184` — `_find_project_dir()`
- `src/claude_agent_sdk/_internal/sessions.py:640-657` — worktree-aware directory matching

### Current behavior

When the exact directory does not exist, `_find_project_dir()` scans the projects directory and returns the first directory whose name starts with the first 200 sanitized characters plus `-`.

The comment explains why this fallback exists: the CLI uses `Bun.hash`, while the SDK uses `simpleHash`. However, two distinct long paths can share the same first 200 sanitized characters. Both hash-suffixed directories then satisfy the fallback, and filesystem iteration order decides which one is returned.

The worktree path performs the same prefix-only match and can include multiple unrelated directories.

### Why this matters

All filesystem session helpers rely on this resolution path. A collision can make listing, reading, renaming, tagging, forking, or deleting sessions operate on another project. This is especially concerning for mutation and deletion APIs.

### Suggested reproduction

1. Create two canonical project paths whose sanitized forms share the first 200 characters but differ afterward.
2. Create CLI-style project directories for both with different hash suffixes.
3. Put distinct session transcripts in each directory.
4. Force the SDK exact hash path to be absent.
5. Call `list_sessions(directory=...)` or `get_session_info(..., directory=...)` for each project.
6. Observe that the first prefix match may be returned for both.

### Expected behavior

A project directory should only be selected after its identity has been verified.

### Possible fix

Either implement the same hash algorithm as the CLI, or inspect all prefix candidates and verify the transcript's recorded `cwd` against the requested canonical path. Ambiguous candidates should not be selected arbitrarily.

Tests should cover two long paths sharing the complete 200-character prefix.

### Environment

- Repository revision: current `main` audit at SDK version 0.2.128
- Bundled CLI version: 2.1.220
- Python test suite: 1,291 passed, 5 skipped
- Ruff and mypy: clean

I searched the existing issues and pull requests using the affected symbols and behavior before filing this.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.