Skip to content

Commit 82c29bd

Browse files
committed
fix: persist debugging concepts from hook errors
1 parent 9c8635f commit 82c29bd

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

scripts/track-command.sh

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,35 @@ if command -v jq &> /dev/null; then
4747
# Log the command
4848
echo "{\"timestamp\":\"$TIMESTAMP\",\"command\":\"$(echo "$COMMAND" | head -c 200)\",\"concept\":\"$CONCEPT\"}" >> "$COMMANDS_LOG"
4949

50-
# Track concept in session and lifetime if new and meaningful
51-
IS_FIRST_EVER="false"
52-
if [ -n "$CONCEPT" ] && [ -f "$PROFILE_FILE" ]; then
53-
ALREADY_SEEN=$(jq --arg c "$CONCEPT" '.session_concepts | index($c)' "$PROFILE_FILE")
54-
if [ "$ALREADY_SEEN" = "null" ]; then
55-
UPDATED=$(jq --arg c "$CONCEPT" '.session_concepts += [$c]' "$PROFILE_FILE")
56-
echo "$UPDATED" > "$PROFILE_FILE"
50+
record_profile_concept() {
51+
local concept="$1"
52+
local first_flag_var="$2"
53+
54+
if [ -z "$concept" ] || [ ! -f "$PROFILE_FILE" ]; then
55+
return
56+
fi
57+
58+
local already_in_session
59+
local already_in_lifetime
60+
local updated
61+
62+
already_in_session=$(jq --arg c "$concept" '.session_concepts | index($c)' "$PROFILE_FILE")
63+
if [ "$already_in_session" = "null" ]; then
64+
updated=$(jq --arg c "$concept" '.session_concepts += [$c]' "$PROFILE_FILE")
65+
echo "$updated" > "$PROFILE_FILE"
5766
fi
5867

59-
LIFETIME_SEEN=$(jq --arg c "$CONCEPT" '.concepts_seen | index($c)' "$PROFILE_FILE")
60-
if [ "$LIFETIME_SEEN" = "null" ]; then
61-
UPDATED=$(jq --arg c "$CONCEPT" '.concepts_seen += [$c]' "$PROFILE_FILE")
62-
echo "$UPDATED" > "$PROFILE_FILE"
63-
IS_FIRST_EVER="true"
68+
already_in_lifetime=$(jq --arg c "$concept" '.concepts_seen | index($c)' "$PROFILE_FILE")
69+
if [ "$already_in_lifetime" = "null" ]; then
70+
updated=$(jq --arg c "$concept" '.concepts_seen += [$c]' "$PROFILE_FILE")
71+
echo "$updated" > "$PROFILE_FILE"
72+
printf -v "$first_flag_var" '%s' "true"
6473
fi
65-
fi
74+
}
75+
76+
# Track concept in session and lifetime if new and meaningful
77+
IS_FIRST_EVER="false"
78+
record_profile_concept "$CONCEPT" IS_FIRST_EVER
6679

6780
# Always inject teaching context after commands
6881
BELT=$(jq -r '.belt // "white"' "$PROFILE_FILE" 2>/dev/null || echo "white")
@@ -79,7 +92,8 @@ if command -v jq &> /dev/null; then
7992
mocha\ *|"mocha"|\
8093
"npm test"*|"npm run test"*|\
8194
"yarn test"*|"yarn run test"*|\
82-
"npx jest"*|"npx vitest"*)
95+
"pnpm test"*|"pnpm run test"*|"pnpm vitest"*|\
96+
"npx jest"*|"npx vitest"*|"pnpm exec jest"*|"pnpm exec vitest"*)
8397
IS_TEST_RUNNER="true"
8498
;;
8599
esac
@@ -117,12 +131,17 @@ if command -v jq &> /dev/null; then
117131
esac
118132
fi
119133

120-
if [ "$IS_FIRST_EVER" = "true" ] && [ -n "$CONCEPT" ]; then
121-
# First-time encounter: micro-lesson about the concept
122-
CONTEXT="🥋 CodeSensei micro-lesson trigger: The user just encountered '$CONCEPT' for the FIRST TIME (command: $SAFE_CMD). Their belt level is '$BELT'. Provide a brief 2-sentence explanation of what $CONCEPT means and why it matters. Adapt language to their belt level. Keep it concise and non-intrusive."
134+
ERROR_IS_FIRST_EVER="false"
135+
record_profile_concept "$ERROR_CONCEPT" ERROR_IS_FIRST_EVER
136+
137+
if [ "$ERROR_IS_FIRST_EVER" = "true" ] && [ -n "$ERROR_CONCEPT" ]; then
138+
CONTEXT="🥋 CodeSensei micro-lesson trigger: The user just encountered '$ERROR_CONCEPT' for the FIRST TIME while reading command output ($SAFE_CMD). Their belt level is '$BELT'. Provide a brief 2-sentence explanation of how to read this kind of error and why it matters. Adapt language to their belt level. Keep it supportive and practical."
123139
elif [ -n "$ERROR_CONCEPT" ]; then
124-
# Error detected in command output: teach debugging
140+
# Error detected in command output: teach debugging first
125141
CONTEXT="🥋 CodeSensei inline insight: An error appeared in the command output ($SAFE_CMD). The user's belt level is '$BELT'. This is a great moment to teach '$ERROR_CONCEPT' — briefly explain how to read and interpret this type of error in 1-2 sentences, adapted to their belt level. Keep it supportive and practical."
142+
elif [ "$IS_FIRST_EVER" = "true" ] && [ -n "$CONCEPT" ]; then
143+
# First-time encounter: micro-lesson about the concept
144+
CONTEXT="🥋 CodeSensei micro-lesson trigger: The user just encountered '$CONCEPT' for the FIRST TIME (command: $SAFE_CMD). Their belt level is '$BELT'. Provide a brief 2-sentence explanation of what $CONCEPT means and why it matters. Adapt language to their belt level. Keep it concise and non-intrusive."
126145
elif [ -n "$CONCEPT" ]; then
127146
# Already-seen concept: brief inline insight about this specific command
128147
CONTEXT="🥋 CodeSensei inline insight: Claude just ran a '$CONCEPT' command ($SAFE_CMD). The user's belt level is '$BELT'. Provide a brief 1-sentence explanation of what this command does, adapted to their belt level. Keep it natural and non-intrusive."

0 commit comments

Comments
 (0)