Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:15:01 +08:00
commit 2fbdb7fc3d
23 changed files with 2851 additions and 0 deletions

34
hooks/create-session.sh Executable file
View File

@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# Create and initialize spec-dev session tracking file
set -euo pipefail
# Read JSON input from stdin
input=$(cat)
# Extract session_id and cwd from JSON
session_id=$(echo "$input" | jq -r '.session_id')
cwd=$(echo "$input" | jq -r '.cwd')
# Normalize CWD for filesystem (replace / with -)
normalized_cwd=$(echo "$cwd" | sed 's|^/||' | sed 's|/|-|g')
# Create cache directory path
cache_dir="$HOME/.local/cache/codethread-plugins/spec-dev/$normalized_cwd"
session_file="$cache_dir/$session_id.json"
# Create directory if it doesn't exist
mkdir -p "$cache_dir"
# Create initial session file
cat > "$session_file" <<EOF
{
"status": "enabled",
"compactions": 0,
"cwd": "$cwd",
"created": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
}
EOF
echo "✓ Session tracking initialized: $session_file"
exit 0

41
hooks/hooks.json Normal file
View File

@@ -0,0 +1,41 @@
{
"description": "Spec-dev session tracking and workflow resumption",
"hooks": {
"PreToolUse": [
{
"matcher": "Skill",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/skill-pretooluse-handler.sh",
"timeout": 10
}
]
}
],
"PreCompact": [
{
"matcher": "*",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/precompact-handler.sh",
"timeout": 10
}
]
}
],
"SessionStart": [
{
"matcher": "compact",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/sessionstart-handler.sh",
"timeout": 10
}
]
}
]
}
}

37
hooks/precompact-handler.sh Executable file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# Handle PreCompact event - increment compactions counter
set -euo pipefail
# Read JSON input from stdin
input=$(cat)
# Extract session_id and cwd from JSON
session_id=$(echo "$input" | jq -r '.session_id')
cwd=$(echo "$input" | jq -r '.cwd')
# Normalize CWD for filesystem (replace / with -)
normalized_cwd=$(echo "$cwd" | sed 's|^/||' | sed 's|/|-|g')
# Locate session file
cache_dir="$HOME/.local/cache/codethread-plugins/spec-dev/$normalized_cwd"
session_file="$cache_dir/$session_id.json"
# Check if session file exists
if [ ! -f "$session_file" ]; then
# Not a spec-dev session, exit silently
exit 0
fi
# Read current compactions count
current_compactions=$(jq -r '.compactions // 0' "$session_file")
# Increment counter
new_compactions=$((current_compactions + 1))
# Update the session file
jq --arg count "$new_compactions" '.compactions = ($count | tonumber)' "$session_file" > "$session_file.tmp"
mv "$session_file.tmp" "$session_file"
echo "✓ Compaction #$new_compactions tracked for spec-dev session"
exit 0

67
hooks/sessionstart-handler.sh Executable file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env bash
# Handle SessionStart event - inject context if spec-dev session is active
set -euo pipefail
# Read JSON input from stdin
input=$(cat)
# Extract session_id and cwd from JSON
session_id=$(echo "$input" | jq -r '.session_id')
cwd=$(echo "$input" | jq -r '.cwd')
source=$(echo "$input" | jq -r '.source')
# Normalize CWD for filesystem (replace / with -)
normalized_cwd=$(echo "$cwd" | sed 's|^/||' | sed 's|/|-|g')
# Locate session file
cache_dir="$HOME/.local/cache/codethread-plugins/spec-dev/$normalized_cwd"
session_file="$cache_dir/$session_id.json"
# Check if session file exists
if [ ! -f "$session_file" ]; then
# Not a spec-dev session, exit silently
exit 0
fi
# Read session status
status=$(jq -r '.status' "$session_file")
compactions=$(jq -r '.compactions // 0' "$session_file")
# Only inject context if status is enabled
if [ "$status" != "enabled" ]; then
exit 0
fi
# Get the most recent spec
latest_spec=$("${CLAUDE_PLUGIN_ROOT}/scripts/get-latest-spec.sh" "$cwd/specs" 2>/dev/null || echo "")
# Build additional context message
context_msg="━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔧 SPEC-DEV MODE ACTIVE
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This session is in spec-driven development mode.
**CRITICAL**: You MUST load the \`spec-dev:spec-architect\` skill immediately
**Workflow**: Follow the ITERATE workflow
**Compactions**: This session has been compacted $compactions time(s)
**Latest Spec**: $latest_spec
After loading the skill, follow the ITERATE workflow to assess the current state and route to either PLAN or BUILD workflow as appropriate.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Output JSON with additionalContext
cat <<EOF
{
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": $(echo "$context_msg" | jq -Rs .)
},
"suppressOutput": true
}
EOF
exit 0

View File

@@ -0,0 +1,49 @@
#!/usr/bin/env bash
# Handle PreToolUse for Skill tool - initialize session when spec-architect is loaded
set -euo pipefail
# Read JSON input from stdin
input=$(cat)
# Extract tool name and skill parameter
tool_name=$(echo "$input" | jq -r '.tool_name')
skill=$(echo "$input" | jq -r '.tool_input.skill // ""')
# Only proceed if this is a Skill tool call for spec-dev:spec-architect
if [ "$tool_name" != "Skill" ] || [ "$skill" != "spec-dev:spec-architect" ]; then
exit 0
fi
# Extract session_id and cwd
session_id=$(echo "$input" | jq -r '.session_id')
cwd=$(echo "$input" | jq -r '.cwd')
# Normalize CWD for filesystem (replace / with -)
normalized_cwd=$(echo "$cwd" | sed 's|^/||' | sed 's|/|-|g')
# Create cache directory path
cache_dir="$HOME/.local/cache/codethread-plugins/spec-dev/$normalized_cwd"
session_file="$cache_dir/$session_id.json"
# Check if session file already exists
if [ -f "$session_file" ]; then
# Session already initialized
exit 0
fi
# Create directory if it doesn't exist
mkdir -p "$cache_dir"
# Create initial session file
cat > "$session_file" <<EOF
{
"status": "enabled",
"compactions": 0,
"cwd": "$cwd",
"created": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
}
EOF
echo "✓ Spec-dev session tracking initialized"
exit 0