validate-gen-plan-io.sh 5.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
# validate-gen-plan-io.sh
# Validates input and output paths for the gen-plan command
# Exit codes:
#   0 - Success, all validations passed
#   1 - Input file does not exist
#   2 - Input file is empty
#   3 - Output directory does not exist
#   4 - Output file already exists
#   5 - No write permission to output directory
#   6 - Invalid arguments
#   7 - Plan template file not found (plugin configuration error)

set -e

usage() {
    echo "Usage: $0 --input <path/to/draft.md> --output <path/to/plan.md> [--auto-start-rlcr-if-converged] [--discussion|--direct]"
    echo ""
    echo "Options:"
    echo "  --input   Path to the input draft file (required)"
    echo "  --output  Path to the output plan file (required)"
    echo "  --auto-start-rlcr-if-converged  Enable direct RLCR start after converged planning (discussion mode only)"
    echo "  --discussion  Use discussion mode (iterative Claude/Codex convergence rounds)"
    echo "  --direct      Use direct mode (skip convergence rounds, proceed immediately to plan)"
    echo "  -h, --help  Show this help message"
    exit 6
}

INPUT_FILE=""
OUTPUT_FILE=""
AUTO_START_RLCR_IF_CONVERGED="false"
GEN_PLAN_MODE_DISCUSSION="false"
GEN_PLAN_MODE_DIRECT="false"

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --input)
            if [[ $# -lt 2 || "$2" == --* ]]; then
                echo "ERROR: --input requires a value"
                usage
            fi
            INPUT_FILE="$2"
            shift 2
            ;;
        --output)
            if [[ $# -lt 2 || "$2" == --* ]]; then
                echo "ERROR: --output requires a value"
                usage
            fi
            OUTPUT_FILE="$2"
            shift 2
            ;;
        --auto-start-rlcr-if-converged)
            AUTO_START_RLCR_IF_CONVERGED="true"
            shift
            ;;
        --discussion)
            GEN_PLAN_MODE_DISCUSSION="true"
            shift
            ;;
        --direct)
            GEN_PLAN_MODE_DIRECT="true"
            shift
            ;;
        -h|--help)
            usage
            ;;
        *)
            echo "ERROR: Unknown option: $1"
            usage
            ;;
    esac
done

# Validate mutually exclusive flags
if [[ "$GEN_PLAN_MODE_DISCUSSION" == "true" && "$GEN_PLAN_MODE_DIRECT" == "true" ]]; then
    echo "Error: --discussion and --direct are mutually exclusive"
    exit 6
fi

# Validate required arguments
if [[ -z "$INPUT_FILE" ]]; then
    echo "ERROR: --input is required"
    usage
fi

if [[ -z "$OUTPUT_FILE" ]]; then
    echo "ERROR: --output is required"
    usage
fi

# Note on auto-start behavior in direct mode
if [[ "$GEN_PLAN_MODE_DIRECT" == "true" && "$AUTO_START_RLCR_IF_CONVERGED" == "true" ]]; then
    echo "NOTE: --auto-start-rlcr-if-converged only triggers in --discussion mode; in --direct mode the plan is not considered converged and auto-start will be skipped."
fi

# Get absolute paths
INPUT_FILE=$(realpath -m "$INPUT_FILE" 2>/dev/null || echo "$INPUT_FILE")
OUTPUT_FILE=$(realpath -m "$OUTPUT_FILE" 2>/dev/null || echo "$OUTPUT_FILE")
OUTPUT_DIR=$(dirname "$OUTPUT_FILE")

echo "=== gen-plan IO Validation ==="
echo "Input file: $INPUT_FILE"
echo "Output file: $OUTPUT_FILE"
echo "Output directory: $OUTPUT_DIR"

# Check 1: Input file exists
if [[ ! -f "$INPUT_FILE" ]]; then
    echo "VALIDATION_ERROR: INPUT_NOT_FOUND"
    echo "The input file does not exist: $INPUT_FILE"
    echo "Please ensure the draft file exists before running gen-plan."
    exit 1
fi

# Check 2: Input file is not empty
if [[ ! -s "$INPUT_FILE" ]]; then
    echo "VALIDATION_ERROR: INPUT_EMPTY"
    echo "The input file is empty: $INPUT_FILE"
    echo "Please add content to your draft file before running gen-plan."
    exit 2
fi

# Check 3: Output directory exists
if [[ ! -d "$OUTPUT_DIR" ]]; then
    echo "VALIDATION_ERROR: OUTPUT_DIR_NOT_FOUND"
    echo "The output directory does not exist: $OUTPUT_DIR"
    echo "Please create the directory: mkdir -p $OUTPUT_DIR"
    exit 3
fi

# Check 4: Output path must not exist (must be a new file path)
if [[ -d "$OUTPUT_FILE" ]]; then
    echo "VALIDATION_ERROR: OUTPUT_IS_DIRECTORY"
    echo "The output path is a directory: $OUTPUT_FILE"
    echo "Please specify a file path, not a directory (e.g., $OUTPUT_FILE/plan.md)."
    exit 4
fi

if [[ -e "$OUTPUT_FILE" ]]; then
    echo "VALIDATION_ERROR: OUTPUT_EXISTS"
    echo "The output path already exists: $OUTPUT_FILE"
    echo "Please choose a different output path or remove the existing file."
    exit 4
fi

# Check 5: Write permission to output directory
if [[ ! -w "$OUTPUT_DIR" ]]; then
    echo "VALIDATION_ERROR: NO_WRITE_PERMISSION"
    echo "No write permission for the output directory: $OUTPUT_DIR"
    echo "Please check directory permissions."
    exit 5
fi

# All checks passed
INPUT_LINE_COUNT=$(wc -l < "$INPUT_FILE" | tr -d ' ')
echo "VALIDATION_SUCCESS"
echo "Input file: $INPUT_FILE ($INPUT_LINE_COUNT lines)"
echo "Output target: $OUTPUT_FILE"
echo "IO validation passed."

# Locate template file using CLAUDE_PLUGIN_ROOT (set by Claude Code plugin system)
# Fallback to script-relative path if environment variable not set
if [[ -n "${CLAUDE_PLUGIN_ROOT:-}" ]]; then
    TEMPLATE_FILE="$CLAUDE_PLUGIN_ROOT/prompt-template/plan/gen-plan-template.md"
else
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
    TEMPLATE_FILE="$SCRIPT_DIR/../prompt-template/plan/gen-plan-template.md"
fi

if [[ ! -f "$TEMPLATE_FILE" ]]; then
    echo "ERROR: Plan template file not found: $TEMPLATE_FILE"
    echo "This is a plugin configuration error. Please reinstall the plugin."
    exit 7
fi

echo "TEMPLATE_FILE: $TEMPLATE_FILE"
echo "Proceeding with draft analysis..."
exit 0