Skip to content
Shell
4.2 KB
Raw Blame
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════════
# ID8Pipeline Activity Tracker
# ═══════════════════════════════════════════════════════════════════════════════
# Runs as a post-tool hook to automatically document work done.
# Detects significant changes and updates PIPELINE_STATUS.md accordingly.
#
# Hook triggers: Edit, Write (file operations)
# ═══════════════════════════════════════════════════════════════════════════════

set -euo pipefail

# Get the file that was modified from stdin or argument
MODIFIED_FILE="${1:-}"
TOOL_NAME="${CLAUDE_TOOL_NAME:-unknown}"
PIPELINE_BRAIN="$HOME/.claude/hooks/pipeline/pipeline-brain.sh"

# Find PIPELINE_STATUS.md
find_pipeline_status() {
    local dir="${PWD}"
    while [[ "$dir" != "/" ]]; do
        if [[ -f "$dir/PIPELINE_STATUS.md" ]]; then
            echo "$dir/PIPELINE_STATUS.md"
            return 0
        fi
        dir=$(dirname "$dir")
    done
    return 1
}

# Detect what type of work was done based on file modified
detect_work_type() {
    local file="$1"

    case "$file" in
        *.test.* | *.spec.* | */__tests__/* | */tests/*)
            echo "testing"
            ;;
        *.md | *.mdx | */docs/*)
            echo "documentation"
            ;;
        */components/* | */ui/* | *.tsx | *.jsx)
            echo "ui"
            ;;
        */api/* | */routes/* | */server/*)
            echo "backend"
            ;;
        */migrations/* | *.sql | */schema/*)
            echo "database"
            ;;
        package.json | *.config.* | tsconfig.json | next.config.*)
            echo "configuration"
            ;;
        .env* | */env/*)
            echo "environment"
            ;;
        *)
            echo "general"
            ;;
    esac
}

# Update the activity log in PIPELINE_STATUS.md
update_activity_log() {
    local status_file="$1"
    local work_type="$2"
    local file_changed="$3"

    local timestamp=$(date "+%Y-%m-%d %H:%M")
    local relative_file=$(basename "$file_changed")

    # Check if Activity Log section exists
    if ! grep -q "## Activity Log" "$status_file"; then
        # Add Activity Log section before Override Log
        sed -i.bak '/## Override Log/i\
## Activity Log\
\
| Time | Type | File | Notes |\
|------|------|------|-------|\
\
---\
' "$status_file"
        rm -f "${status_file}.bak"
    fi

    # Add new activity entry (insert after the header row)
    # This is a bit tricky with sed, so we use a temp approach
    local activity_line="| $timestamp | $work_type | \`$relative_file\` | Auto-logged |"

    # Use awk to insert after the Activity Log table header
    awk -v line="$activity_line" '
        /## Activity Log/ { found=1 }
        found && /^\|---/ { print; print line; found=0; next }
        { print }
    ' "$status_file" > "${status_file}.tmp" && mv "${status_file}.tmp" "$status_file"
}

# Touch the PIPELINE_STATUS.md to update modification time
touch_pipeline() {
    local status_file="$1"
    touch "$status_file"
}

# Main logic
main() {
    # Skip if no file was modified
    if [[ -z "$MODIFIED_FILE" ]]; then
        exit 0
    fi

    # Find pipeline status file
    local status_file
    if ! status_file=$(find_pipeline_status); then
        # No pipeline, nothing to do
        exit 0
    fi

    # Detect work type
    local work_type=$(detect_work_type "$MODIFIED_FILE")

    # Update activity log
    update_activity_log "$status_file" "$work_type" "$MODIFIED_FILE"

    # Touch to reset staleness timer
    touch_pipeline "$status_file"

    # Log to pipeline brain
    if [[ -x "$PIPELINE_BRAIN" ]]; then
        "$PIPELINE_BRAIN" log "Activity: $work_type on $MODIFIED_FILE" 2>/dev/null || true
    fi

    # Output for Claude to see
    echo "Pipeline: Logged $work_type activity"
}

main "$@"