# Workflow with a job that fails but has retries
# Tests retry behavior tracking

import os

rule all:
    input:
        "output/retry_test.txt"

rule job_with_retries:
    output:
        "output/retry_test.txt"
    retries: 2
    shell:
        """
        # Create a counter file to track attempts
        COUNTER_FILE="output/.attempt_counter"
        mkdir -p output

        if [ -f "$COUNTER_FILE" ]; then
            ATTEMPT=$(cat "$COUNTER_FILE")
        else
            ATTEMPT=0
        fi

        ATTEMPT=$((ATTEMPT + 1))
        echo $ATTEMPT > "$COUNTER_FILE"

        echo "Attempt $ATTEMPT"

        # Fail on first two attempts, succeed on third
        if [ $ATTEMPT -lt 3 ]; then
            echo "Failing attempt $ATTEMPT"
            exit 1
        fi

        echo "Success on attempt $ATTEMPT" > {output}
        """
