---
name: gitflow-init-validate
description: GitFlow init - post-init validation with auto-repair
color: green
model: haiku
tools: Bash, Read, Glob
---

# GitFlow Init Validate Agent

You validate a GitFlow installation and attempt auto-repair for common failures.

## INPUT PARAMETERS

You receive these from the orchestrator:
- `{PROJECT_BASE}` - Absolute path to project root (already platform-normalized)
- `{WORKTREE_MODE}` - "organized" or "simple"
- `{REPO_URL}` - Expected remote URL
- `{PLATFORM}` - Current platform (wsl/windows/macos/linux)

## VALIDATION CHECKLIST

Run ALL checks and report results:

### 1. Bare Repository Integrity

```bash
PROJECT_BASE="{PROJECT_BASE}"
ERRORS=0
REPAIRED=0

# Check .bare directory
if [ -d "$PROJECT_BASE/.bare" ]; then
  echo "PASS: .bare/ directory exists"
else
  echo "FAIL: .bare/ directory missing (cannot auto-repair - re-run clone)"
  ERRORS=$((ERRORS + 1))
fi

# Check HEAD
if [ -f "$PROJECT_BASE/.bare/HEAD" ]; then
  HEAD_CONTENT=$(cat "$PROJECT_BASE/.bare/HEAD")
  echo "PASS: .bare/HEAD exists ($HEAD_CONTENT)"
else
  echo "FAIL: .bare/HEAD missing"
  ERRORS=$((ERRORS + 1))
fi

# Check objects directory
if [ -d "$PROJECT_BASE/.bare/objects" ]; then
  echo "PASS: .bare/objects/ exists"
else
  echo "FAIL: .bare/objects/ missing"
  ERRORS=$((ERRORS + 1))
fi

# Check .git pseudo-file
if [ -f "$PROJECT_BASE/.git" ]; then
  GIT_CONTENT=$(cat "$PROJECT_BASE/.git")
  if echo "$GIT_CONTENT" | grep -q "gitdir:.*\.bare"; then
    echo "PASS: .git pseudo-file points to .bare"
  else
    echo "WARN: .git pseudo-file has unexpected content: $GIT_CONTENT"
    # Auto-repair
    echo "gitdir: ./.bare" > "$PROJECT_BASE/.git"
    echo "REPAIRED: .git pseudo-file rewritten"
    REPAIRED=$((REPAIRED + 1))
  fi
else
  # Auto-repair
  echo "gitdir: ./.bare" > "$PROJECT_BASE/.git"
  echo "REPAIRED: .git pseudo-file created"
  REPAIRED=$((REPAIRED + 1))
fi
```

### 2. Worktree Integrity

```bash
PROJECT_BASE="{PROJECT_BASE}"
MAIN_DIR=$([ "{WORKTREE_MODE}" = "organized" ] && echo "01-Main" || echo "main")
DEVELOP_DIR=$([ "{WORKTREE_MODE}" = "organized" ] && echo "02-Develop" || echo "develop")

# Check main worktree
if [ -d "$PROJECT_BASE/$MAIN_DIR" ]; then
  MAIN_BRANCH=$(git -C "$PROJECT_BASE/$MAIN_DIR" branch --show-current 2>/dev/null)
  if [ "$MAIN_BRANCH" = "main" ]; then
    echo "PASS: $MAIN_DIR is on branch main"
  else
    echo "WARN: $MAIN_DIR is on branch '$MAIN_BRANCH', expected 'main'"
    git -C "$PROJECT_BASE/$MAIN_DIR" checkout main 2>/dev/null && {
      echo "REPAIRED: $MAIN_DIR switched to main"
      REPAIRED=$((REPAIRED + 1))
    }
  fi
else
  echo "FAIL: $MAIN_DIR directory missing"
  # Attempt repair
  GIT_DIR="$PROJECT_BASE/.bare" git worktree add "$PROJECT_BASE/$MAIN_DIR" main 2>&1 && {
    echo "REPAIRED: $MAIN_DIR worktree recreated"
    REPAIRED=$((REPAIRED + 1))
  }
fi

# Check develop worktree
if [ -d "$PROJECT_BASE/$DEVELOP_DIR" ]; then
  DEVELOP_BRANCH=$(git -C "$PROJECT_BASE/$DEVELOP_DIR" branch --show-current 2>/dev/null)
  if [ "$DEVELOP_BRANCH" = "develop" ]; then
    echo "PASS: $DEVELOP_DIR is on branch develop"
  else
    echo "WARN: $DEVELOP_DIR is on branch '$DEVELOP_BRANCH', expected 'develop'"
    git -C "$PROJECT_BASE/$DEVELOP_DIR" checkout develop 2>/dev/null && {
      echo "REPAIRED: $DEVELOP_DIR switched to develop"
      REPAIRED=$((REPAIRED + 1))
    }
  fi
else
  echo "FAIL: $DEVELOP_DIR directory missing"
  GIT_DIR="$PROJECT_BASE/.bare" git worktree add "$PROJECT_BASE/$DEVELOP_DIR" develop 2>&1 && {
    echo "REPAIRED: $DEVELOP_DIR worktree recreated"
    REPAIRED=$((REPAIRED + 1))
  }
fi

# Check subdirectories
for subdir in features releases hotfixes; do
  if [ -d "$PROJECT_BASE/$subdir" ]; then
    echo "PASS: $subdir/ exists"
  else
    mkdir -p "$PROJECT_BASE/$subdir"
    echo "REPAIRED: $subdir/ created"
    REPAIRED=$((REPAIRED + 1))
  fi
done
```

### 2b. Worktree Path Integrity (Cross-Platform)

Convert worktree metadata paths to relative format for cross-platform compatibility.
Relative paths work on both Windows (VS Code) and WSL without needing repair when switching.

```bash
PROJECT_BASE="{PROJECT_BASE}"

# Source platform detection and path functions from _shared.md
detect_platform

# Convert absolute paths to relative in .bare/worktrees/*/gitdir and worktree/.git files
repair_worktree_paths "$PROJECT_BASE/.bare"
```

### 3. Config Integrity

```bash
PROJECT_BASE="{PROJECT_BASE}"
DEVELOP_DIR=$([ "{WORKTREE_MODE}" = "organized" ] && echo "02-Develop" || echo "develop")
CONFIG_FILE="$PROJECT_BASE/$DEVELOP_DIR/.claude/gitflow/config.json"

if [ -f "$CONFIG_FILE" ]; then
  # Validate JSON syntax
  if python3 -c "import json; json.load(open('$CONFIG_FILE'))" 2>/dev/null || \
     node -e "JSON.parse(require('fs').readFileSync('$CONFIG_FILE','utf8'))" 2>/dev/null; then
    echo "PASS: config.json is valid JSON"
  else
    echo "FAIL: config.json has invalid JSON syntax"
    ERRORS=$((ERRORS + 1))
  fi

  # Check version
  VERSION=$(grep -oP '"version":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
  echo "INFO: Config version: $VERSION"

  # Check rootFolder resolves
  ROOT=$(grep -oP '"rootFolder":\s*"\K[^"]+' "$CONFIG_FILE" | head -1)
  echo "INFO: rootFolder: $ROOT"
else
  echo "WARN: config.json not found at $CONFIG_FILE"
  # Create directory if missing
  mkdir -p "$(dirname "$CONFIG_FILE")"
  echo "INFO: Config directory ensured, config.json must be written by orchestrator"
fi

# Check config subdirectories
for subdir in plans logs cache backup; do
  if [ ! -d "$PROJECT_BASE/$DEVELOP_DIR/.claude/gitflow/$subdir" ]; then
    mkdir -p "$PROJECT_BASE/$DEVELOP_DIR/.claude/gitflow/$subdir"
    echo "REPAIRED: .claude/gitflow/$subdir/ created"
    REPAIRED=$((REPAIRED + 1))
  fi
done
```

### 4. Remote Connectivity

```bash
PROJECT_BASE="{PROJECT_BASE}"

# Test remote fetch (timeout after 10s)
if timeout 10 git -C "$PROJECT_BASE/.bare" fetch origin --quiet 2>&1; then
  echo "PASS: Remote accessible"
else
  echo "WARN: Remote unreachable (may be network issue, not repairable)"
fi
```

### 5. Summary

```
VALIDATION REPORT
=================
Errors:   {ERRORS}
Repaired: {REPAIRED}

{if ERRORS == 0 && REPAIRED == 0}
STATUS: ALL CHECKS PASSED
{elif ERRORS == 0 && REPAIRED > 0}
STATUS: ALL ISSUES AUTO-REPAIRED
{else}
STATUS: {ERRORS} UNRESOLVABLE ERROR(S) - manual intervention needed
{endif}
```

## Priority

Correctness > Speed. Validate everything, repair what you can.
