()
| 166 | * Returns 0 on any error (conservative). |
| 167 | */ |
| 168 | export async function countConcurrentSessions(): Promise<number> { |
| 169 | const dir = getSessionsDir() |
| 170 | let files: string[] |
| 171 | try { |
| 172 | files = await readdir(dir) |
| 173 | } catch (e) { |
| 174 | if (!isFsInaccessible(e)) { |
| 175 | logForDebugging(`[concurrentSessions] readdir failed: ${errorMessage(e)}`) |
| 176 | } |
| 177 | return 0 |
| 178 | } |
| 179 | |
| 180 | let count = 0 |
| 181 | for (const file of files) { |
| 182 | // Strict filename guard: only `<pid>.json` is a candidate. parseInt's |
| 183 | // lenient prefix-parsing means `2026-03-14_notes.md` would otherwise |
| 184 | // parse as PID 2026 and get swept as stale — silent user data loss. |
| 185 | // See anthropics/claude-code#34210. |
| 186 | if (!/^\d+\.json$/.test(file)) continue |
| 187 | const pid = parseInt(file.slice(0, -5), 10) |
| 188 | if (pid === process.pid) { |
| 189 | count++ |
| 190 | continue |
| 191 | } |
| 192 | if (isProcessRunning(pid)) { |
| 193 | count++ |
| 194 | } else if (getPlatform() !== 'wsl') { |
| 195 | // Stale file from a crashed session — sweep it. Skip on WSL: if |
| 196 | // ~/.claude/sessions/ is shared with Windows-native Claude (symlink |
| 197 | // or CLAUDE_CONFIG_DIR), a Windows PID won't be probeable from WSL |
| 198 | // and we'd falsely delete a live session's file. This is just |
| 199 | // telemetry so conservative undercount is acceptable. |
| 200 | void unlink(join(dir, file)).catch(() => {}) |
| 201 | } |
| 202 | } |
| 203 | return count |
| 204 | } |
| 205 |
no test coverage detected