Skip to content

Commit 172b285

Browse files
committed
fix: auto-correct prefix conflicts in create-new-feature.sh
When --number is passed with a prefix that already exists in the specs/ directory (e.g., an AI agent passes --number 1 but specs/001-* already exists), the script now auto-corrects to the next globally available number instead of silently creating a duplicate. This prevents duplicate spec directory prefixes that cause errors in downstream tooling (e.g., check-prerequisites.sh emitting "ERROR: Multiple spec directories found with prefix '001'"). The --number flag becomes a validated suggestion: if it conflicts, the script recalculates from the true global maximum across all spec directories and git branches, then warns on stderr: ⚠️ Requested number 001 conflicts with existing spec directory. Auto-corrected to 030. The workflow never fails due to numbering — it self-heals.
1 parent 7562664 commit 172b285

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

scripts/bash/create-new-feature.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,30 @@ fi
255255

256256
# Force base-10 interpretation to prevent octal conversion (e.g., 010 → 8 in octal, but should be 10 in decimal)
257257
FEATURE_NUM=$(printf "%03d" "$((10#$BRANCH_NUMBER))")
258+
259+
# ── Guardrail: auto-correct if the chosen prefix already exists ──────────────
260+
# If a specs directory with this numeric prefix already exists, the requested
261+
# number is stale or wrong (e.g., an agent passed --number with a conflicting
262+
# value). Auto-correct to the global max + 1 so the workflow never fails due
263+
# to numbering collisions.
264+
if compgen -G "$SPECS_DIR/${FEATURE_NUM}-*" > /dev/null 2>&1; then
265+
REQUESTED_NUM="$FEATURE_NUM"
266+
# Recalculate from the true global max across specs + branches
267+
if [ "$HAS_GIT" = true ]; then
268+
GLOBAL_MAX_BRANCH=$(get_highest_from_branches)
269+
else
270+
GLOBAL_MAX_BRANCH=0
271+
fi
272+
GLOBAL_MAX_SPEC=$(get_highest_from_specs "$SPECS_DIR")
273+
GLOBAL_MAX=$GLOBAL_MAX_BRANCH
274+
if [ "$GLOBAL_MAX_SPEC" -gt "$GLOBAL_MAX" ]; then
275+
GLOBAL_MAX=$GLOBAL_MAX_SPEC
276+
fi
277+
BRANCH_NUMBER=$((GLOBAL_MAX + 1))
278+
FEATURE_NUM=$(printf "%03d" "$((10#$BRANCH_NUMBER))")
279+
>&2 echo "⚠️ Requested number $REQUESTED_NUM conflicts with existing spec directory. Auto-corrected to $FEATURE_NUM."
280+
fi
281+
258282
BRANCH_NAME="${FEATURE_NUM}-${BRANCH_SUFFIX}"
259283

260284
# GitHub enforces a 244-byte limit on branch names

0 commit comments

Comments
 (0)