Skip to content

fix(hooks): try GNU stat before BSD stat in orphan-sweep throttle#1611

Open
timothyfroehlich wants to merge 1 commit into
mainfrom
worktree-orphan-sweep-stat-fix
Open

fix(hooks): try GNU stat before BSD stat in orphan-sweep throttle#1611
timothyfroehlich wants to merge 1 commit into
mainfrom
worktree-orphan-sweep-stat-fix

Conversation

@timothyfroehlich

Copy link
Copy Markdown
Owner

Summary

  • The session-start-orphan-sweep.sh SessionStart hook was failing with line 37: File: unbound variable on Bazzite/Linux (GNU coreutils), while working fine on macOS.
  • Root cause: the throttle check tried stat -f %m first, assuming it would fail on Linux and fall through to stat -c %Y. But GNU stat's -f flag is a valid (different) flag — filesystem-status mode — so it "succeeds" and dumps unrelated verbose filesystem text instead of erroring on the invalid %m directive. That garbage became $last, and $((now - last)) then tried to evaluate the leading word "File" as a variable, tripping set -u.
  • Fix: reorder to try GNU-style stat -c %Y first, then BSD-style stat -f %m as fallback. Safe on both platforms — each flavor fails cleanly when its syntax doesn't match, so the ordering only matters for which one is tried first and succeeds correctly.

Test plan

  • Reproduced the crash locally on Bazzite: bash -x .claude/hooks/session-start-orphan-sweep.sh showed last getting set to a multi-line stat -f filesystem dump, then failing on the arithmetic line.
  • Confirmed fix: same trace now shows stat -c %Y succeeding cleanly, age computed correctly, script exits 0.
  • shellcheck clean on the modified script.
  • Grepped repo for other stat -f/stat -c cross-platform usages — this was the only occurrence.
  • pnpm run check (pre-commit hooks) passed clean.

🤖 Generated with Claude Code

On Linux, GNU stat's `-f` flag is valid (filesystem-status mode) and
silently "succeeds" with unrelated verbose text instead of erroring on
the invalid %m directive, poisoning the throttle timestamp and
tripping `set -u` on the arithmetic check. BSD stat has no `-c`, so
trying GNU syntax first fails cleanly there, making this order safe on
both macOS and Linux (e.g. Bazzite).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 6, 2026 11:55
@vercel

vercel Bot commented Jul 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
pin-point Ready Ready Preview, Comment Jul 6, 2026 11:56am

Request Review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a cross-platform throttling bug in the Claude Code SessionStart orphan-sweep hook by ensuring the script reads the throttle file’s mtime correctly on GNU/Linux as well as macOS/BSD.

Changes:

  • Reorders the stat invocation to try GNU stat -c %Y before BSD stat -f %m, avoiding GNU stat -f’s filesystem-status output from poisoning the timestamp value.
  • Adds in-script commentary explaining the platform-specific stat behavior and why the ordering is safe.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants