summaryrefslogtreecommitdiff
path: root/hooks/session-start
blob: fc71eb88720dd529499e5af177636bd5d7e36ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
# SessionStart hook for superpowers plugin

set -eu

# Determine plugin root directory.
# Splits $0 on its last / or \ (run-hook.cmd's cmd half passes a Windows
# path), with no separator meaning the current directory. This avoids
# dirname because a SessionStart:startup hook can run with a broken/empty
# PATH (anthropics/claude-code#43127) before any PATH-repairing sibling hook
# in the same startup batch takes effect.
#
# No command substitution or pipe runs under bash: each one forks, and a
# fork costs tens of milliseconds under Git Bash on Windows. The paths stay
# relative to the current directory, which is all the file read below needs.
case "$0" in
  */*|*\\*) SCRIPT_DIR="${0%[/\\]*}" ;;
  *) SCRIPT_DIR="." ;;
esac
PLUGIN_ROOT="${SCRIPT_DIR}/.."

nl='
'
tab='	'
if [ -n "${BASH_VERSION:-}" ]; then
  # shellcheck disable=SC3045 # bash builtin avoids a subshell fork (#2081)
  printf -v cr '\r'
else
  cr=$(printf '\r' 2>/dev/null || true)
fi

# Read using-superpowers content with the read builtin (no cat, for the same
# PATH-independence reason, and no $(< file) subshell). read returns 1 at end
# of file, hence || [ -n "$line" ]; the loop drops trailing newlines as
# $(< file) did.
skill_md="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
using_superpowers_content="Error reading using-superpowers skill"
if [ -r "$skill_md" ]; then
  using_superpowers_content=""
  first_line=1
  while IFS= read -r line || [ -n "$line" ]; do
    if [ "$first_line" -eq 1 ]; then
      using_superpowers_content=$line
      first_line=0
    else
      using_superpowers_content="${using_superpowers_content}${nl}${line}"
    fi
  done < "$skill_md"
  while :; do
    case "$using_superpowers_content" in
      *"$nl") using_superpowers_content=${using_superpowers_content%"$nl"} ;;
      *) break ;;
    esac
  done
fi

# Escape string for JSON embedding using POSIX parameter substitution
# (no subshell, works in sh/ash/dash/bash/zsh).
replace_all() {
  rest=$1
  out=""
  while :; do
    case "$rest" in
      *"$2"*)
        out="${out}${rest%%"$2"*}$3"
        rest=${rest#*"$2"}
        ;;
      *)
        escaped="${out}${rest}"
        break
        ;;
    esac
  done
}

escape_for_json() {
  escaped=$1
  replace_all "$escaped" '\' '\\'
  replace_all "$escaped" '"' '\"'
  [ -z "$cr" ] || replace_all "$escaped" "$cr" '\r'
  replace_all "$escaped" "$nl" '\n'
  replace_all "$escaped" "$tab" '\t'
  using_superpowers_escaped=$escaped
}

escape_for_json "$using_superpowers_content"
session_context="<EXTREMELY_IMPORTANT>\nYou have superpowers.\n\n**Below is the full content of your 'superpowers:using-superpowers' skill - your introduction to using skills. For all other skills, use the 'Skill' tool:**\n\n${using_superpowers_escaped}\n</EXTREMELY_IMPORTANT>"

# Output context injection as JSON.
# Cursor hooks expect additional_context (snake_case).
# Claude Code hooks expect hookSpecificOutput.additionalContext (nested).
# Copilot CLI (v1.0.11+) and others expect additionalContext (top-level, SDK standard).
# Claude Code reads BOTH additional_context and hookSpecificOutput without
# deduplication, so we must emit only the field the current platform consumes.
# Muse sets MUSE_PLUGIN_ROOT and gets the nested hookSpecificOutput form.
#
# Uses printf instead of heredoc to work around bash 5.1+ heredoc hang.
# See: https://github.com/obra/superpowers/issues/571
#
# emit_json absorbs EPIPE without a pipe through `cat`. On Windows + Git
# Bash, the harness can close the hook's stdout before printf finishes, and
# Git Bash's printf reports that EPIPE as "printf: write error: Permission
# denied", which `set -eu` would let escape as a startup diagnostic
# every session (#1612). SIGPIPE is ignored so other platforms see the same
# EPIPE error instead of a signal; the error message is dropped and the
# status ignored. No external command is involved, so a broken PATH (#2310)
# changes nothing.
emit_json() {
  trap '' PIPE
  # shellcheck disable=SC2059 # every caller passes a literal format string
  printf "$@" 2>/dev/null || true
}

if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
  # Cursor sets CURSOR_PLUGIN_ROOT (may also set CLAUDE_PLUGIN_ROOT)
  emit_json '{\n  "additional_context": "%s"\n}\n' "$session_context"
elif [ -n "${CLAUDE_PLUGIN_ROOT:-}" ] && [ -z "${COPILOT_CLI:-}" ] && [ -z "${MUSE_PLUGIN_ROOT:-}" ]; then
  # Claude Code sets CLAUDE_PLUGIN_ROOT without COPILOT_CLI/MUSE_PLUGIN_ROOT
  emit_json '{\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$session_context"
elif [ -n "${MUSE_PLUGIN_ROOT:-}" ]; then
  # Muse sets MUSE_PLUGIN_ROOT — try Claude-style nested output for Muse Spark
  emit_json '{\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$session_context"
else
  # Copilot CLI (sets COPILOT_CLI=1) or unknown platform — SDK standard format
  emit_json '{\n  "additionalContext": "%s"\n}\n' "$session_context"
fi

exit 0