Skip to content
Shell
12.2 KB
Raw Blame
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════════
# ID8Pipeline Progress Updater
# ═══════════════════════════════════════════════════════════════════════════════
# Handles automatic updates to PIPELINE_STATUS.md including:
# - Advancing to next stage
# - Marking checkpoints as cleared
# - Logging decisions and overrides
#
# Usage:
#   ./pipeline-progress-updater.sh advance        - Move to next stage
#   ./pipeline-progress-updater.sh checkpoint     - Mark current checkpoint cleared
#   ./pipeline-progress-updater.sh decision "..." - Log a decision
#   ./pipeline-progress-updater.sh override X "reason" - Log a stage skip
# ═══════════════════════════════════════════════════════════════════════════════

set -euo pipefail

PIPELINE_BRAIN="$HOME/.claude/hooks/pipeline/pipeline-brain.sh"
PIPELINE_MEMORY_DIR="$HOME/.claude/pipeline-memory"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

# 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
}

get_current_stage() {
    local status_file="$1"
    grep -oE "Current Stage: ([0-9]+)" "$status_file" 2>/dev/null | grep -oE "[0-9]+" | head -1 || echo "0"
}

get_stage_name() {
    local stage_num="$1"
    case $stage_num in
        1) echo "Concept Lock" ;;
        2) echo "Scope Fence" ;;
        3) echo "Architecture Sketch" ;;
        4) echo "Foundation Pour" ;;
        5) echo "Feature Blocks" ;;
        6) echo "Integration Pass" ;;
        7) echo "Polish & Harden" ;;
        8) echo "Launch Prep" ;;
        9) echo "Ship" ;;
        10) echo "Listen & Iterate" ;;
        *) echo "Unknown" ;;
    esac
}

get_checkpoint_question() {
    local stage_num="$1"
    case $stage_num in
        1) echo "What's the one-liner?" ;;
        2) echo "What are we NOT building?" ;;
        3) echo "Draw me the boxes and arrows." ;;
        4) echo "Can we deploy an empty shell?" ;;
        5) echo "Does this feature work completely, right now?" ;;
        6) echo "Do all the pieces talk to each other?" ;;
        7) echo "What breaks if I do something stupid?" ;;
        8) echo "Could a stranger use this without asking me questions?" ;;
        9) echo "Is it live and are people using it?" ;;
        10) echo "What did we learn?" ;;
        *) echo "No checkpoint defined" ;;
    esac
}

# ─────────────────────────────────────────────────────────────────────────────
# Command: Advance to next stage
# ─────────────────────────────────────────────────────────────────────────────
cmd_advance() {
    local status_file
    if ! status_file=$(find_pipeline_status); then
        echo "No PIPELINE_STATUS.md found"
        return 1
    fi

    local current_stage=$(get_current_stage "$status_file")
    local next_stage=$((current_stage + 1))

    if [[ $next_stage -gt 10 ]]; then
        echo "Already at final stage (10)"
        return 0
    fi

    local current_name=$(get_stage_name "$current_stage")
    local next_name=$(get_stage_name "$next_stage")
    local next_checkpoint=$(get_checkpoint_question "$next_stage")
    local today=$(date "+%Y-%m-%d")

    echo ""
    echo -e "${CYAN}Advancing Pipeline${NC}"
    echo "  From: Stage $current_stage - $current_name"
    echo "  To:   Stage $next_stage - $next_name"
    echo ""

    # Update current stage line
    sed -i.bak "s/Current Stage: $current_stage.*/Current Stage: $next_stage - $next_name/" "$status_file"

    # Update next checkpoint question
    sed -i.bak "s/\*\*Next Checkpoint Question:\*\* .*/\*\*Next Checkpoint Question:\*\* \"$next_checkpoint\"/" "$status_file"

    # Update progress tracker table - mark previous stage as cleared
    # Change: | X. Name | 🔄 In Progress | - |  to  | X. Name | ✅ Cleared | DATE |
    sed -i.bak "s/| $current_stage\. .* | 🔄 In Progress |/| $current_stage. $current_name | ✅ Cleared | $today |/" "$status_file"

    # Mark new stage as in progress
    sed -i.bak "s/| $next_stage\. .* | ⬜ Pending |/| $next_stage. $next_name | 🔄 In Progress | - |/" "$status_file"

    # Update Last Updated date
    sed -i.bak "s/^> Last Updated: .*/> Last Updated: $today/" "$status_file"

    # Clean up backup files
    rm -f "${status_file}.bak"

    # Log the advancement
    if [[ -x "$PIPELINE_BRAIN" ]]; then
        "$PIPELINE_BRAIN" log "Advanced from Stage $current_stage to Stage $next_stage" 2>/dev/null || true
    fi

    echo -e "${GREEN}✓ Pipeline advanced to Stage $next_stage${NC}"
    echo ""
    echo "New checkpoint: \"$next_checkpoint\""
}

# ─────────────────────────────────────────────────────────────────────────────
# Command: Mark checkpoint cleared (same as advance)
# ─────────────────────────────────────────────────────────────────────────────
cmd_checkpoint() {
    cmd_advance
}

# ─────────────────────────────────────────────────────────────────────────────
# Command: Log a decision
# ─────────────────────────────────────────────────────────────────────────────
cmd_decision() {
    local decision_text="$1"

    local status_file
    if ! status_file=$(find_pipeline_status); then
        echo "No PIPELINE_STATUS.md found"
        return 1
    fi

    local today=$(date "+%Y-%m-%d")
    local current_stage=$(get_current_stage "$status_file")

    # Find Decision Log table and add entry
    # The table format is: | Date | Decision | Reasoning | Stage |

    # Create a decision entry line
    local decision_line="| $today | $decision_text |  | $current_stage |"

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

    echo -e "${GREEN}✓ Decision logged${NC}"

    # Log to pipeline brain
    if [[ -x "$PIPELINE_BRAIN" ]]; then
        "$PIPELINE_BRAIN" log "Decision: $decision_text" 2>/dev/null || true
    fi
}

# ─────────────────────────────────────────────────────────────────────────────
# Command: Log an override (stage skip)
# ─────────────────────────────────────────────────────────────────────────────
cmd_override() {
    local skipped_stage="$1"
    local reason="${2:-No reason provided}"

    local status_file
    if ! status_file=$(find_pipeline_status); then
        echo "No PIPELINE_STATUS.md found"
        return 1
    fi

    local today=$(date "+%Y-%m-%d")

    # Create an override entry line
    local override_line="| $today | Stage $skipped_stage | $reason | Yes |"

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

    # Also mark the stage as skipped in progress tracker
    local stage_name=$(get_stage_name "$skipped_stage")
    sed -i.bak "s/| $skipped_stage\. .* | ⬜ Pending |/| $skipped_stage. $stage_name | ⏭️ Skipped | $today |/" "$status_file"
    rm -f "${status_file}.bak"

    echo -e "${YELLOW}⏭️ Override logged: Stage $skipped_stage skipped${NC}"
    echo "Reason: $reason"

    # Log to pipeline brain
    if [[ -x "$PIPELINE_BRAIN" ]]; then
        "$PIPELINE_BRAIN" log "Override: Skipped stage $skipped_stage - $reason" 2>/dev/null || true
    fi
}

# ─────────────────────────────────────────────────────────────────────────────
# Command: Initialize a new pipeline status file
# ─────────────────────────────────────────────────────────────────────────────
cmd_init() {
    local project_name="${1:-$(basename "$PWD")}"
    local status_file="./PIPELINE_STATUS.md"
    local template="$HOME/.claude/PIPELINE_STATUS_TEMPLATE.md"

    if [[ -f "$status_file" ]]; then
        echo "PIPELINE_STATUS.md already exists"
        return 1
    fi

    if [[ ! -f "$template" ]]; then
        echo "Template not found at $template"
        return 1
    fi

    # Copy template
    cp "$template" "$status_file"

    # Fill in project name and dates
    local today=$(date "+%Y-%m-%d")
    sed -i.bak "s/\[PROJECT NAME\]/$project_name/" "$status_file"
    sed -i.bak "s/\[DATE\]/$today/g" "$status_file"
    sed -i.bak "s/\[STAGE NUMBER\]/1/" "$status_file"
    sed -i.bak "s/\[STAGE NAME\]/Concept Lock/" "$status_file"
    sed -i.bak "s/\[QUESTION\]/What's the one-liner?/" "$status_file"

    # Mark stage 1 as in progress
    sed -i.bak "s/| 1\. Concept Lock | ⬜ Pending |/| 1. Concept Lock | 🔄 In Progress |/" "$status_file"

    rm -f "${status_file}.bak"

    echo -e "${GREEN}✓ Pipeline initialized for: $project_name${NC}"
    echo "Starting at Stage 1: Concept Lock"
    echo ""
    echo "First checkpoint: \"What's the one-liner?\""

    # Log to pipeline brain
    if [[ -x "$PIPELINE_BRAIN" ]]; then
        "$PIPELINE_BRAIN" log "Pipeline initialized for $project_name" 2>/dev/null || true
    fi
}

# ─────────────────────────────────────────────────────────────────────────────
# Entry Point
# ─────────────────────────────────────────────────────────────────────────────
case "${1:-help}" in
    advance)
        cmd_advance
        ;;
    checkpoint)
        cmd_checkpoint
        ;;
    decision)
        cmd_decision "${2:-}"
        ;;
    override)
        cmd_override "${2:-0}" "${3:-}"
        ;;
    init)
        cmd_init "${2:-}"
        ;;
    *)
        echo "Usage: pipeline-progress-updater.sh {advance|checkpoint|decision|override|init}"
        echo ""
        echo "Commands:"
        echo "  advance           - Move to the next pipeline stage"
        echo "  checkpoint        - Mark current checkpoint as cleared"
        echo "  decision \"text\"   - Log a decision to the Decision Log"
        echo "  override N \"why\"  - Log skipping stage N with reason"
        echo "  init [name]       - Initialize new PIPELINE_STATUS.md"
        exit 1
        ;;
esac