Shell
#!/bin/bash
# session-recovery-check.sh
#
# Hook: user-prompt-submit (runs before processing user input)
# Purpose: Check for crashed/incomplete sessions and prompt recovery
#
# This hook runs at the start of each interaction to:
# 1. Detect if there are incomplete sessions from previous crashes
# 2. Alert the user about recovery options
# 3. Provide context for resuming work
SESSION_AGENTS_DIR="$HOME/Development/session-resilience-agents"
SESSION_STATE="$SESSION_AGENTS_DIR/state/sessions/current-session.json"
# Check if session agents project exists
if [ ! -d "$SESSION_AGENTS_DIR" ]; then
exit 0
fi
# Check if there's a current session
if [ ! -f "$SESSION_STATE" ]; then
exit 0
fi
# Read session status
SESSION_STATUS=$(node --input-type=module -e "
import fs from 'fs';
try {
const data = JSON.parse(fs.readFileSync('$SESSION_STATE', 'utf-8'));
const lastActivity = new Date(data.recovery.lastActivity);
const now = new Date();
const ageMinutes = (now - lastActivity) / (1000 * 60);
// Check if session is stale (>30 min with active status)
if (data.status === 'active' && ageMinutes > 30) {
console.log('STALE');
console.log(data.projectName);
console.log(data.sessionId.substring(0, 8));
console.log(Math.round(ageMinutes));
} else if (data.status === 'crashed') {
console.log('CRASHED');
console.log(data.projectName);
console.log(data.sessionId.substring(0, 8));
} else if (data.status === 'active') {
console.log('ACTIVE');
console.log(data.projectName);
}
} catch (e) {
// Silent fail
}
" 2>/dev/null)
# Parse the output
STATUS=$(echo "$SESSION_STATUS" | head -1)
PROJECT=$(echo "$SESSION_STATUS" | sed -n '2p')
SESSION_ID=$(echo "$SESSION_STATUS" | sed -n '3p')
AGE=$(echo "$SESSION_STATUS" | sed -n '4p')
# Handle different states
case "$STATUS" in
"STALE")
echo ""
echo "========================================"
echo " SESSION RECOVERY AVAILABLE"
echo "========================================"
echo ""
echo " Found stale session: $PROJECT ($SESSION_ID...)"
echo " Last active: $AGE minutes ago"
echo ""
echo " Options:"
echo " /resume - Continue from last state"
echo " /status - View session details"
echo ""
echo "========================================"
echo ""
;;
"CRASHED")
echo ""
echo "========================================"
echo " CRASHED SESSION DETECTED"
echo "========================================"
echo ""
echo " Project: $PROJECT"
echo " Session: $SESSION_ID..."
echo ""
echo " Run /resume to recover your work"
echo ""
echo "========================================"
echo ""
;;
"ACTIVE")
# Active session - no message needed, just silent
;;
esac
exit 0