This repository was archived by the owner on Oct 27, 2025. It is now read-only.
-
Couldn't load subscription status.
- Fork 90
Add context-aware skills with additionalDirectories support #14
Open
dvic
wants to merge
1
commit into
obra:main
Choose a base branch
from
qdentity:feature/context-aware-skills
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,114 @@ | ||
| #!/usr/bin/env bash | ||
| # find-skills - Find and list skills with when_to_use guidance | ||
| # Shows all skills by default, filters by pattern if provided | ||
| # Searches current dir + additionalDirectories from settings, then global skills | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Determine directories | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| SKILLS_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" | ||
|
|
||
| # Function to get additional directories from Claude Code settings | ||
| # Outputs: "path|settings_dir" for each additionalDirectory found | ||
| get_additional_dirs() { | ||
| local git_root | ||
| git_root=$(git rev-parse --show-toplevel 2>/dev/null) || return | ||
|
|
||
| local current_dir | ||
| current_dir=$(pwd) | ||
|
|
||
| # Walk up from current dir to git root, checking for settings files | ||
| local check_dir="$current_dir" | ||
| while true; do | ||
| local settings_files=( | ||
| "$check_dir/.claude/settings.json" | ||
| "$check_dir/.claude/settings.local.json" | ||
| ) | ||
|
|
||
| for settings_file in "${settings_files[@]}"; do | ||
| if [[ -f "$settings_file" ]]; then | ||
| # Extract paths from additionalDirectories array only | ||
| # Output format: "path|settings_dir" so we can resolve relative paths correctly | ||
| awk -v settings_dir="$check_dir" ' | ||
| /"additionalDirectories"/ { in_section=1; next } | ||
| in_section && /\]/ { in_section=0; next } | ||
| in_section && /^[[:space:]]*"/ { | ||
| gsub(/^[[:space:]]*"|"[[:space:]]*,?[[:space:]]*$/, "") | ||
| print $0 "|" settings_dir | ||
| } | ||
| ' "$settings_file" 2>/dev/null | ||
| fi | ||
| done | ||
|
|
||
| # Stop if we've reached git root | ||
| if [[ "$(cd "$check_dir" && pwd)" == "$(cd "$git_root" && pwd)" ]]; then | ||
| break | ||
| fi | ||
|
|
||
| # Move up one directory | ||
| check_dir=$(dirname "$check_dir") | ||
|
|
||
| # Safety check | ||
| if [[ "$check_dir" == "/" ]]; then | ||
| break | ||
| fi | ||
| done | ||
| } | ||
|
|
||
| # Collect additional directories from settings (excluding current dir) | ||
| CURRENT_DIR=$(pwd) | ||
| ADDITIONAL_DIRS=() | ||
|
|
||
| if git rev-parse --show-toplevel >/dev/null 2>&1; then | ||
| while IFS='|' read -r dir settings_dir; do | ||
| [[ -z "$dir" ]] && continue | ||
|
|
||
| # Resolve relative paths from settings_dir, absolute paths as-is | ||
| if [[ "$dir" = /* ]]; then | ||
| # Absolute path - use as is | ||
| dir_normalized=$(cd "$dir" 2>/dev/null && pwd) || continue | ||
| else | ||
| # Relative path - resolve from where settings file is | ||
| dir_normalized=$(cd "$settings_dir" && cd "$dir" 2>/dev/null && pwd) || continue | ||
| fi | ||
|
|
||
| current_normalized=$(cd "$CURRENT_DIR" 2>/dev/null && pwd) | ||
|
|
||
| # Skip if it's the current directory | ||
| if [[ "$dir_normalized" == "$current_normalized" ]]; then | ||
| continue | ||
| fi | ||
|
|
||
| # Deduplicate: only add if not already in array | ||
| already_added=false | ||
| for existing_dir in "${ADDITIONAL_DIRS[@]}"; do | ||
| if [[ "$existing_dir" == "$dir_normalized" ]]; then | ||
| already_added=true | ||
| break | ||
| fi | ||
| done | ||
|
|
||
| if ! $already_added; then | ||
| ADDITIONAL_DIRS+=("$dir_normalized") | ||
| fi | ||
| done < <(get_additional_dirs) | ||
| fi | ||
|
|
||
| # Project skills: current dir .claude/skills | ||
| PROJECT_SKILLS_DIR="" | ||
| if [[ -d "$CURRENT_DIR/.claude/skills" ]]; then | ||
| PROJECT_SKILLS_DIR="$CURRENT_DIR/.claude/skills" | ||
| fi | ||
|
|
||
| # Additional context skills: .claude/skills in each additionalDirectory | ||
| ADDITIONAL_SKILLS_DIRS=() | ||
| for dir in "${ADDITIONAL_DIRS[@]}"; do | ||
| if [[ -d "$dir/.claude/skills" ]]; then | ||
| ADDITIONAL_SKILLS_DIRS+=("$dir/.claude/skills") | ||
| fi | ||
| done | ||
|
|
||
| SUPERPOWERS_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/superpowers" | ||
| LOG_FILE="${SUPERPOWERS_DIR}/search-log.jsonl" | ||
|
|
||
|
|
@@ -28,11 +129,15 @@ EXAMPLES: | |
|
|
||
| OUTPUT: | ||
| Each line shows: Use skill-path/SKILL.md when [trigger] | ||
| Skills from additionalDirectories show location: Use skill-path (dirname) when [trigger] | ||
| Paths include /SKILL.md for direct use with Read tool | ||
| Current dir project skills shadow additional directory skills when paths match | ||
|
|
||
| SEARCH: | ||
| Searches both skill content AND path names. | ||
| Skills location: ~/.config/superpowers/skills/ | ||
| Current dir: .claude/skills/ in current working directory | ||
| Additional context: .claude/skills/ in each additionalDirectories from .claude/settings*.json | ||
| Global skills: ~/.config/superpowers/skills/ | ||
| EOF | ||
| exit 0 | ||
| fi | ||
|
|
@@ -54,7 +159,8 @@ get_skill_path() { | |
| echo "$rel_path" | ||
| } | ||
|
|
||
| # Collect all matching skills | ||
| # Collect all matching skills (track seen skills to handle shadowing) | ||
| seen_skills_list="" | ||
| results=() | ||
|
|
||
| # If pattern provided, log the search | ||
|
|
@@ -63,23 +169,86 @@ if [[ -n "$PATTERN" ]]; then | |
| echo "{\"timestamp\":\"$timestamp\",\"query\":\"$PATTERN\"}" >> "$LOG_FILE" 2>/dev/null || true | ||
| fi | ||
|
|
||
| # Search skills directory | ||
| # Search current directory project skills (no location tag) | ||
| if [[ -n "$PROJECT_SKILLS_DIR" ]]; then | ||
| while IFS= read -r file; do | ||
| [[ -z "$file" ]] && continue | ||
|
|
||
| # Get path relative to .claude/skills/, then prepend "skills/" for consistency | ||
| rel_path="${file#$PROJECT_SKILLS_DIR/}" | ||
| skill_path="skills/${rel_path}" | ||
|
|
||
| when_to_use=$(get_when_to_use "$file") | ||
| seen_skills_list="${seen_skills_list}${skill_path}"$'\n' | ||
| results+=("$skill_path||$when_to_use") # Empty location tag | ||
| done < <( | ||
| if [[ -n "$PATTERN" ]]; then | ||
| # Pattern mode: search content and paths | ||
| { | ||
| grep -E -r "$PATTERN" "$PROJECT_SKILLS_DIR/" --include="SKILL.md" -l 2>/dev/null || true | ||
| find "$PROJECT_SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null | grep -E "$PATTERN" 2>/dev/null || true | ||
| } | sort -u | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else | ||
| # Show all | ||
| find "$PROJECT_SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null || true | ||
| fi | ||
| ) | ||
| fi | ||
|
|
||
| # Search additional context directories (with location tags) | ||
| for ADDITIONAL_SKILLS_DIR in "${ADDITIONAL_SKILLS_DIRS[@]}"; do | ||
| # Get project directory name for location tag (parent of .claude/skills) | ||
| # /path/to/project/.claude/skills -> /path/to/project | ||
| project_dir=$(dirname "$(dirname "$ADDITIONAL_SKILLS_DIR")") | ||
| location_tag=$(basename "$project_dir") | ||
|
|
||
| while IFS= read -r file; do | ||
| [[ -z "$file" ]] && continue | ||
|
|
||
| rel_path="${file#$ADDITIONAL_SKILLS_DIR/}" | ||
| skill_path="skills/${rel_path}" | ||
|
|
||
| # Skip if already seen (shadowed by current dir) | ||
| if echo "$seen_skills_list" | grep -q "^${skill_path}$"; then | ||
| continue | ||
| fi | ||
|
Comment on lines
+212
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use fixed-string, exact-line matching for shadowing checks. Regex matching can misidentify paths containing regex metacharacters, breaking shadowing. Apply: - if echo "$seen_skills_list" | grep -q "^${skill_path}$"; then
+ if echo "$seen_skills_list" | grep -F -x -q "${skill_path}"; then
continue
fi- echo "$seen_skills_list" | grep -q "^${skill_path}$" && continue
+ echo "$seen_skills_list" | grep -F -x -q "${skill_path}" && continueAlso applies to: 238-239 🤖 Prompt for AI Agents |
||
|
|
||
| when_to_use=$(get_when_to_use "$file") | ||
| seen_skills_list="${seen_skills_list}${skill_path}"$'\n' | ||
| results+=("$skill_path|$location_tag|$when_to_use") | ||
| done < <( | ||
| if [[ -n "$PATTERN" ]]; then | ||
| { | ||
| grep -E -r "$PATTERN" "$ADDITIONAL_SKILLS_DIR/" --include="SKILL.md" -l 2>/dev/null || true | ||
| find "$ADDITIONAL_SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null | grep -E "$PATTERN" 2>/dev/null || true | ||
| } | sort -u | ||
| else | ||
| find "$ADDITIONAL_SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null || true | ||
| fi | ||
| ) | ||
| done | ||
|
|
||
| # Search global skills directory (skip if shadowed by project skills) | ||
| while IFS= read -r file; do | ||
| [[ -z "$file" ]] && continue | ||
|
|
||
| skill_path=$(get_skill_path "$file" "$SKILLS_DIR") | ||
|
|
||
| # Skip if shadowed by project skill | ||
| echo "$seen_skills_list" | grep -q "^${skill_path}$" && continue | ||
|
|
||
| when_to_use=$(get_when_to_use "$file") | ||
| results+=("$skill_path|$when_to_use") | ||
| results+=("$skill_path||$when_to_use") # Empty location tag | ||
| done < <( | ||
| if [[ -n "$PATTERN" ]]; then | ||
| # Pattern mode: search content and paths | ||
| # Pattern mode: search content and paths (exclude .claude directories) | ||
| { | ||
| grep -E -r -- "$PATTERN" "$SKILLS_DIR/" --include="SKILL.md" -l 2>/dev/null || true | ||
| find "$SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null | grep -E -- "$PATTERN" 2>/dev/null || true | ||
| grep -E -r -- "$PATTERN" "$SKILLS_DIR/" --include="SKILL.md" --exclude-dir=".claude" -l 2>/dev/null || true | ||
| find "$SKILLS_DIR/" -name "SKILL.md" -type f -not -path "*/.claude/*" 2>/dev/null | grep -E -- "$PATTERN" 2>/dev/null || true | ||
| } | sort -u | ||
| else | ||
| # Show all | ||
| find "$SKILLS_DIR/" -name "SKILL.md" -type f 2>/dev/null || true | ||
| # Show all (exclude .claude directories) | ||
| find "$SKILLS_DIR/" -name "SKILL.md" -type f -not -path "*/.claude/*" 2>/dev/null || true | ||
| fi | ||
| ) | ||
|
|
||
|
|
@@ -96,11 +265,21 @@ if [[ ${#results[@]} -eq 0 ]]; then | |
| fi | ||
|
|
||
| # Sort and display results | ||
| printf "%s\n" "${results[@]}" | sort | while IFS='|' read -r skill_path when_to_use; do | ||
| if [[ -n "$when_to_use" ]]; then | ||
| echo "Use $skill_path $when_to_use" | ||
| printf "%s\n" "${results[@]}" | sort | while IFS='|' read -r skill_path location_tag when_to_use; do | ||
| if [[ -n "$location_tag" ]]; then | ||
| # Additional directory skill - show with location tag | ||
| if [[ -n "$when_to_use" ]]; then | ||
| echo "Use $skill_path ($location_tag) $when_to_use" | ||
| else | ||
| echo "$skill_path ($location_tag)" | ||
| fi | ||
| else | ||
| echo "$skill_path" | ||
| # Current dir or global skill - no location tag | ||
| if [[ -n "$when_to_use" ]]; then | ||
| echo "Use $skill_path $when_to_use" | ||
| else | ||
| echo "$skill_path" | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Verify JSON parsing handles edge cases correctly.
The awk-based JSON parser should handle most simple cases of
additionalDirectoriesarrays. However, it may not correctly handle:"path/with\"quote")Run the following script to test edge cases:
🏁 Script executed:
Length of output: 110
🏁 Script executed:
Length of output: 98
Replace AWK-based JSON parsing with jq
AWK fails on escaped quotes, inline comments and other JSON edge cases; a dedicated JSON parser (e.g. jq) guarantees correct extraction of
additionalDirectories.🤖 Prompt for AI Agents