Skip to content

Conversation

@ryancnelson
Copy link
Contributor

@ryancnelson ryancnelson commented Oct 12, 2025

Problem

The session-start.sh hook fails silently when executed by Claude Code with error "⏺ Plugin hook error:" preventing skills context from loading.

Root Cause

  1. BASH_SOURCE[0] is unbound in Claude Code's execution context (fails with set -u)
  2. Empty grep results return exit code 1 (fails with set -e)

Solution

  1. Use ${BASH_SOURCE[0]:-$0} to fallback to $0 when BASH_SOURCE unavailable
  2. Add || true to handle empty grep results gracefully

Testing

✅ Direct execution produces valid JSON
✅ Claude Code plugin execution now works
✅ No breaking changes to existing functionality

Changes

  • hooks/session-start.sh: 2 lines changed
    • Line 10: Add ${BASH_SOURCE[0]:-$0} fallback
    • Line 18: Add || true for empty grep results

Summary by CodeRabbit

  • Bug Fixes

    • Ensured reliable startup by correctly resolving the script location across environments, even when path variables are unset.
    • Prevented errors during initialization when clearing update/status flags that may not exist, allowing sessions to proceed smoothly.
  • Chores

    • Hardened session initialization for improved robustness and resilience.

… grep results

The hook was failing with 'Plugin hook error:' on startup due to two issues:

1. BASH_SOURCE[0] is unbound when the script is executed in certain contexts
   (like Claude Code plugin execution), causing failure with set -u
   Fix: Use /bin/bash to fallback to $0

2. When filtering status flags, grep returns exit code 1 when all lines
   are filtered out, causing script to exit with set -e
   Fix: Add '|| true' to allow empty result without failing

These changes allow the hook to run successfully in all execution contexts.
@coderabbitai
Copy link

coderabbitai bot commented Oct 12, 2025

Walkthrough

The session-start hook script updates how it resolves its directory by adding a fallback when BASH_SOURCE[0] is unset and makes the SKILLS_UPDATED/BEHIND flag cleanup tolerant to no-output cases by appending || true.

Changes

Cohort / File(s) Summary
Session start hook hardening
hooks/session-start.sh
- SCRIPT_DIR uses ${BASH_SOURCE[0]:-$0} for fallback when BASH_SOURCE[0] is unset
- Flag cleanup (SKILLS_UPDATED/BEHIND) pipeline now appends `

Sequence Diagram(s)

sequenceDiagram
    participant U as Invoker (hook)
    participant S as session-start.sh
    participant FS as Filesystem

    U->>S: Execute hook
    rect rgba(200,230,255,0.3)
      note over S: Resolve script directory
      S->>S: SCRIPT_DIR = dir("${BASH_SOURCE[0]:-$0}")
    end

    rect rgba(220,255,220,0.3)
      note over S,FS: Initialize/clean output flags
      S->>FS: Remove SKILLS_UPDATED/BEHIND markers
      FS-->>S: May return no results
      S->>S: Continue on empty via "|| true"
    end

    S-->>U: Proceed with remaining startup
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I hop to hooks at break of day,
Path found even when shells stray—hurray!
Flags that flicker, now behave,
If none appear, we still stay brave.
With bashy paws, I press on through,
Session starts fresh—carrots anew! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly and concisely summarizes the primary change of fixing the session-start hook's behavior in a plugin context by highlighting the core issue and solution without extraneous details.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ryancnelson
Copy link
Contributor Author

this PR is all ai-generated, btw.... i just asked claude/superpowers to help debug the issue.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
hooks/session-start.sh (2)

18-18: Simplify flag stripping; avoid masking real errors.

Use a single grep (or sed) to drop both lines without needing || true, reducing processes and keeping failures visible.

Apply one of the following:

-init_output=$(echo "$init_output" | grep -v "SKILLS_UPDATED=true" | grep -v "SKILLS_BEHIND=true" || true)
+init_output=$(echo "$init_output" | grep -Ev 'SKILLS_UPDATED=true|SKILLS_BEHIND=true' || true)

Or with sed (no need for || true):

-init_output=$(echo "$init_output" | grep -v "SKILLS_UPDATED=true" | grep -v "SKILLS_BEHIND=true" || true)
+init_output=$(echo "$init_output" | sed -E '/SKILLS_UPDATED=true|SKILLS_BEHIND=true/d')

15-16: skills_updated appears unused (SC2034).

Either use it for messaging or remove to avoid confusion.

Option A — remove it:

-skills_updated=$(echo "$init_output" | grep "SKILLS_UPDATED=true" || echo "")
 skills_behind=$(echo "$init_output" | grep "SKILLS_BEHIND=true" || echo "")

Option B — surface a success message:

 skills_behind=$(echo "$init_output" | grep "SKILLS_BEHIND=true" || echo "")
 # Build status messages that go at the end
 status_message=""
+if [ -n "$skills_updated" ]; then
+    status_message="\n\n✅ Skills updated from upstream."${status_message}
+fi
 if [ -n "$skills_behind" ]; then
     status_message="\n\n⚠️ New skills available from upstream. Ask me to use the pulling-updates-from-skills-repository skill."
 fi
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bde691c and b731542.

📒 Files selected for processing (1)
  • hooks/session-start.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.11.0)
hooks/session-start.sh

[warning] 15-15: skills_updated appears unused. Verify use (or export if used externally).

(SC2034)

🔇 Additional comments (1)
hooks/session-start.sh (1)

10-10: Good fix for set -u contexts.

${BASH_SOURCE[0]:-$0} safely handles unbound BASH_SOURCE and keeps path resolution robust when sourced or executed.

@ericphanson
Copy link

I'm also hitting the empty grep results issue here when using a fork of the skills directory. As summarized by Claude code:

When a user is working on a fork with local commits ahead of the tracking branch, initialize-skills.sh outputs only
SKILLS_BEHIND=true with no user-facing messages. The session-start hook then filters out status flags with grep -v,
which leaves empty output. Without || echo "", grep exits with code 1 when all lines are filtered out, causing the
script to fail due to set -euo pipefail. This prevents the hook from executing successfully.

(This PR uses || true instead of || echo "")

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.

3 participants