Skip to main content

Module claude_session

Module claude_session 

Source
Expand description

Hard-tier in-use signal: detects active Claude Code sessions in a worktree by inspecting ~/.claude/projects/<encoded>/*.jsonl event tails.

Encoding rule mirrors Claude Code’s own: replace / and . with -, drop trailing slash. Verified empirically against ~/.claude/projects/ contents during design.

Structs§

ActiveSession
Information about one active Claude Code session in a worktree.

Functions§

encode_project_dir
Encode an absolute filesystem path to the directory name Claude Code uses under ~/.claude/projects/. / and . become -. Trailing path separators are trimmed.
find_active_sessions
Return all sessions in project_dir whose newest event timestamp is within threshold of now AND whose newest event cwd (if present) matches worktree. Missing/unreadable directories return an empty vec — the caller treats this as “Claude not in use here.”
newest_event_cwd
Companion: extract the cwd field from the same newest event. Used in Task 4 for path-encoding-collision defense. Returns None if not present.
newest_event_timestamp
Read up to ~64 KiB from the end of path, find the newest line that parses as JSON with a timestamp field, and return that timestamp. Returns None for empty files, files containing only metadata events without timestamp, or unreadable / unparseable files.
project_dir_for
Resolve the per-worktree Claude projects directory, e.g. ~/.claude/projects/-Users-dave-Projects-foo. Returns None if $HOME is not set / cannot be resolved.