anthropics / anthropics/claude-agent-sdk-python

Long project paths can resolve to the wrong session directory

未關閉
#1,160 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
8.1k
分支
1.3k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。