Session store writes and reads derive project_key differently, so a git-worktree session lists and resumes as empty

Author: feiiiiii5Created Sep 27, 2026Updated Sep 28, 2026

The SessionStore write paths and read paths derive project_key differently, and the difference is visible whenever the session lives in a git worktree.

Writes use the on-disk project directory name:

  • _internal/session_import.py:80 — project_key = resolved.parent.name
  • _internal/session_store.py:173 — project_key = parts[0], used by file_path_to_session_key and therefore by the live mirror batcher

Reads use the sanitized hash of the requested directory:

  • _internal/sessions.py:1722-1723 — project_key = _sanitize_path(project_path) in list_sessions_from_store, and the same in get_session_info_from_store, get_session_messages_from_store, list_subagents_from_store and _internal/session_resume.py:150

The disk readers do fall back — worktree paths at sessions.py:640-657 and :1172-1179, and a prefix scan for long paths at :176-183. The store readers have neither.

So for a session in a worktree of the repo you point at:

python
import_session_to_store(session_id, store, directory=REPO)   # resolver hops to the worktree, keys on "wt-7n30hczx"
list_sessions_from_store(store, directory=REPO)              # -> []
list_sessions_from_store(store, directory=WORKTREE)          # -> [('1111...', 'worktree session')]
materialize_resume_session(resume=SID, cwd=REPO, store=store)  # -> None

list_sessions(REPO) on the disk path finds the same session, so the store looks emptier than the disk it was fed from. materialize_resume_session returning None is documented as "store has no entries" (session_resume.py:135-139), so ClaudeAgentOptions(session_store=..., resume=sid, cwd=REPO) starts a fresh session with no error — the resumed history is simply gone.

import_session_to_store's docstring says directory has "the same semantics as list_sessions" (session_import.py:57-59), and list_sessions documents worktree inclusion (sessions.py:692-694), while list_sessions_from_store documents the opposite for reads: "include_worktrees is not honored on the store path" (:1703-1705). The write side honours neither.

Two ways to close it, and I'd rather pick with you:

  1. Read side: resolve the key the way the disk readers do — try _sanitize_path(directory), then the worktree keys, then a verified prefix scan. Keeps the on-disk key (which is what the mirror batcher can compute from a file path) and matches what #1170 is already doing for the disk prefix fallback.
  2. Write side: key on project_key_for_directory(directory) everywhere, so writes and reads agree by construction. Smaller, but it changes the key the mirror batcher derives from file_path, and the comment at session_import.py:76-79 says the on-disk name was chosen deliberately.

Happy to do either, with a regression test for the worktree case. Long project paths (>200 chars sanitized, where the CLI's Bun hash differs from the SDK's djb2) hit the same wall; that part overlaps with #1170, so I'd leave it to that PR unless you'd rather fold it in.

Related: #1170 (long-path prefix collisions), #1225 (worktree paths decoded as UTF-8 on Windows) — both in sessions.py, neither covering the store key divergence.

Source: anthropics/claude-agent-sdk-python