// Orchestration script builder — bash worktree execution scripts
/**
 * Build the parallel spec execution section for the orchestration script.
 * Implements validate_specs / create_worktree / run_parallel / cleanup / main.
 */
function buildParallelSpecSection() {
    return [
        '',
        '# ---------------------------------------------------------------------------',
        '# Parallel Spec Execution (3-Layer Automation)',
        '# ---------------------------------------------------------------------------',
        '',
        'validate_specs() {',
        '  echo "Validating spec integrity..."',
        '  if [ -f "scripts/check-spec-integrity.sh" ]; then',
        '    bash scripts/check-spec-integrity.sh || {',
        '      echo "ERROR: Spec integrity check failed. Fix issues before running parallel specs."',
        '      exit 1',
        '    }',
        '  fi',
        '  echo "✓ Spec integrity OK"',
        '}',
        '',
        'create_worktree() {',
        '  local spec_id="$1"',
        '  local branch="feat/${spec_id}"',
        '  echo "Creating worktree for $spec_id on branch $branch..."',
        '  git worktree add "$WORKTREE_BASE/$spec_id" -b "$branch" 2>/dev/null || {',
        '    echo "Branch $branch already exists — reusing"',
        '    git worktree add "$WORKTREE_BASE/$spec_id" "$branch"',
        '  }',
        '  echo "✓ Worktree ready: $WORKTREE_BASE/$spec_id"',
        '}',
        '',
        'run_parallel() {',
        '  local spec_id="$1"',
        '  echo "Launching agent for $spec_id..."',
        '  env -u CLAUDECODE claude -p "implement spec $spec_id" \\',
        '    --project-dir "$WORKTREE_BASE/$spec_id" \\',
        '    > "$LOG_DIR/$spec_id.log" 2>&1 &',
        '  echo "$!" > "$LOG_DIR/$spec_id.pid"',
        '  echo "Agent PID: $! (logs: $LOG_DIR/$spec_id.log)"',
        '}',
        '',
        'cleanup() {',
        '  local spec_id="$1"',
        '  echo "Removing worktree for $spec_id..."',
        '  git worktree remove --force "$WORKTREE_BASE/$spec_id" 2>/dev/null || true',
        '  echo "✓ Worktree $spec_id removed"',
        '}',
        '',
        'run_parallel_specs() {',
        '  if [ $# -eq 0 ]; then',
        '    echo "Usage: bash <script> spec SPEC-001 SPEC-002 ..."',
        '    return 0',
        '  fi',
        '  validate_specs',
        '  mkdir -p "$WORKTREE_BASE" "$LOG_DIR"',
        '  local pids=() spec_ids=("$@")',
        '  for spec_id in "${spec_ids[@]}"; do',
        '    create_worktree "$spec_id"',
        '    run_parallel "$spec_id"',
        '    pids+=("$!")',
        '  done',
        '  echo "Waiting for all agents to complete..."',
        '  local failed=0',
        '  for i in "${!pids[@]}"; do',
        '    wait "${pids[$i]}" || failed=$((failed + 1))',
        '  done',
        '  echo ""',
        '  echo "=== Parallel Results ==="',
        '  for spec_id in "${spec_ids[@]}"; do',
        '    echo "  $spec_id: $(cat $LOG_DIR/$spec_id.log | tail -1)"',
        '    cleanup "$spec_id"',
        '  done',
        '  if [ $failed -gt 0 ]; then',
        '    echo "✗ $failed spec(s) failed — check logs in $LOG_DIR"',
        '    exit 1',
        '  fi',
        '  echo "✓ All specs completed successfully"',
        '}',
    ].join('\n');
}
/**
 * Generate a bash orchestration script for parallel worktree execution.
 * Implements the 3-layer automation system: validate → worktrees → parallel → report → cleanup.
 */
function buildOrchestrationScriptContent(skillName, slug) {
    return [
        '#!/usr/bin/env bash',
        `# Orchestration script for skill: ${skillName}`,
        '# Generated by Planu SDD MCP Server',
        `# Usage: bash scripts/orchestrate-${slug}.sh`,
        '# 3-Layer Automation cross-references:',
        `#   Skill    : .claude/skills/${slug}.md  (or skills/${slug}.md)`,
        `#   SubAgent : agents/${slug}-agent.ts     (generate with sdd_generate_sub_agent)`,
        '# Skill=commit/tracking, sub-agent=subtask delegation, this script=worktree orchestration.',
        '',
        'set -euo pipefail',
        '',
        'WORKTREE_BASE=".worktrees"',
        'BRANCH_PREFIX="feat"',
        'LOG_DIR="logs/orchestration"',
        `SKILL_NAME="${slug}"`,
        '',
        `echo "=== Orchestration: ${skillName} ==="`,
        'echo ""',
        '',
        '# 1. Validate clean working tree',
        'echo "Checking working tree..."',
        'if git status --porcelain | grep -q .; then',
        '  echo "ERROR: Working tree is dirty. Commit or stash changes first."',
        '  exit 1',
        'fi',
        'echo "✓ Working tree is clean"',
        '',
        '# 2. Create worktree and branch',
        'mkdir -p "$WORKTREE_BASE" "$LOG_DIR"',
        'TIMESTAMP=$(date +%Y%m%d-%H%M%S)',
        'BRANCH="${BRANCH_PREFIX}/${SKILL_NAME}-${TIMESTAMP}"',
        'echo "Creating worktree: $BRANCH"',
        'git worktree add "$WORKTREE_BASE/$SKILL_NAME" -b "$BRANCH"',
        'echo "✓ Worktree created at $WORKTREE_BASE/$SKILL_NAME"',
        '',
        '# 3. Execute agent in worktree (claude -p for non-interactive mode)',
        'echo ""',
        'echo "Launching agent..."',
        `env -u CLAUDECODE claude -p "Execute skill: ${skillName}" \\`,
        '  --project-dir "$WORKTREE_BASE/$SKILL_NAME" \\',
        '  > "$LOG_DIR/$SKILL_NAME.log" 2>&1 &',
        'AGENT_PID=$!',
        'echo "Agent PID: $AGENT_PID (logs: $LOG_DIR/$SKILL_NAME.log)"',
        '',
        '# 4. Wait and report results',
        'wait $AGENT_PID',
        'EXIT_CODE=$?',
        '',
        'echo ""',
        'echo "=== Results ==="',
        'if [ $EXIT_CODE -eq 0 ]; then',
        '  echo "✓ Agent completed successfully"',
        '  echo "Log: $LOG_DIR/$SKILL_NAME.log"',
        'else',
        '  echo "✗ Agent failed (exit code: $EXIT_CODE)"',
        '  echo "Check log: $LOG_DIR/$SKILL_NAME.log"',
        '  git worktree remove --force "$WORKTREE_BASE/$SKILL_NAME" 2>/dev/null || true',
        '  exit $EXIT_CODE',
        'fi',
        '',
        '# 5. Merge instructions (manual — review before merging)',
        'echo ""',
        'echo "=== Next Steps ==="',
        'echo "1. Review changes in the worktree:"',
        'echo "   git diff main..$BRANCH"',
        'echo ""',
        'echo "2. Merge when satisfied:"',
        'echo "   git checkout main"',
        'echo "   git merge --no-ff $BRANCH"',
        'echo ""',
        'echo "3. Cleanup worktree and branch:"',
        'echo "   git worktree remove $WORKTREE_BASE/$SKILL_NAME"',
        'echo "   git branch -d $BRANCH"',
        buildParallelSpecSection(),
    ].join('\n');
}
export function generateOrchestrationScript(skillName, _knowledge) {
    const slug = skillName.toLowerCase().replace(/\s+/g, '-');
    return {
        path: `scripts/orchestrate-${slug}.sh`,
        content: buildOrchestrationScriptContent(skillName, slug),
        format: 'markdown',
    };
}
//# sourceMappingURL=skills-orchestration.js.map