Your Claude Code sessions outlive the work. Who closes them?
A usage report said most of my sessions had been open for more than eight hours. The fix was a small hourly job, and its first version had a timezone bug that would have closed nothing.
How to do it
- Read what /usage is telling you before guessing. Mine showed 94% of sessions asking for /clear and 65% open for 8 hours or more, with 10 sessions alive and 9 editor tabs between 15 and 17 hours old.
- Check the suspect before blaming it. I assumed Remote Control was keeping the tabs alive, but only 2 of the 5 old idle tabs had a bridge at all. The real cause was simpler: an editor tab keeps its process alive until the tab closes, and Claude Code never closes an idle session by itself.
- Make the reaper narrow on purpose. Mine closes only editor sessions that are idle past a threshold and have no shell or Claude child process. It never touches launchd services, CLI, SDK or memory sessions, and /resume brings any closed session back, so the worst case is a reopen.
- Give it a --dry-run flag that prints every session with its keep or close reason, plus an --hours flag for the threshold. Run the dry run until each reason reads right, and only then let it close anything.
- Compare timestamps in one timezone. The session file records the process start in UTC, while ps -o lstart prints local time. Run ps with TZ=UTC. My first version missed this and would never have closed a single session.
- Schedule it with launchd, once an hour with a 4 hour threshold. Log only the sessions it closed, and send errors to a separate file so the log stays readable.
Write a Python script that lists my Claude Code sessions from ~/.claude/sessions/*.json and closes only editor sessions that have been idle longer than --hours (default 4) and have no shell or claude child process. Add a --dry-run flag that prints every session with a keep or close reason. The session files record procStart in UTC, so compare against ps run with TZ=UTC. Never touch launchd services, CLI or SDK sessions. Log closed sessions to one file and errors to another. Then write a launchd plist that runs the script every hour.