Shell
#!/bin/bash
# session-persist.sh
#
# Hook: PostToolUse (Write, Edit, MultiEdit)
# Purpose: Track file changes and trigger checkpoints
#
# This hook runs after code modifications to:
# 1. Record the file change in session state
# 2. Increment the tool call counter
# 3. Create checkpoint if threshold reached (every 5 changes)
SESSION_AGENTS_DIR="$HOME/Development/session-resilience-agents"
SESSION_STATE="$SESSION_AGENTS_DIR/state/sessions/current-session.json"
# Check if session state exists
if [ ! -f "$SESSION_STATE" ]; then
exit 0
fi
# Check if the session-resilience-agents project is set up
if [ ! -d "$SESSION_AGENTS_DIR" ]; then
exit 0
fi
# Get the modified file path from environment (if available)
MODIFIED_FILE="${CLAUDE_TOOL_RESULT_FILE:-}"
# Update session state by running the checkpoint tracker
# This is a lightweight operation - just updates the JSON
cd "$SESSION_AGENTS_DIR" 2>/dev/null || exit 0
# Use node to update the session state inline (faster than full tsx)
node --input-type=module -e "
import fs from 'fs';
const statePath = '$SESSION_STATE';
try {
const data = JSON.parse(fs.readFileSync(statePath, 'utf-8'));
data.recovery.toolCallCount = (data.recovery.toolCallCount || 0) + 1;
data.recovery.lastActivity = new Date().toISOString();
data.updatedAt = new Date().toISOString();
// Track modified file if provided
const modFile = '$MODIFIED_FILE';
if (modFile && !data.context.filesModified.includes(modFile)) {
data.context.filesModified.push(modFile);
}
fs.writeFileSync(statePath, JSON.stringify(data, null, 2));
// Check if checkpoint threshold reached
if (data.recovery.toolCallCount >= 5) {
console.log('CHECKPOINT_NEEDED');
}
} catch (e) {
// Silent fail - don't interrupt workflow
}
" 2>/dev/null
# Exit successfully
exit 0