#!/usr/bin/env bash
# spawn-with-retry.sh
# Wraps subagent spawning with retry logic + spacing to reduce "Subagent main failed" errors.
#
# Usage (from shell scripts):
#   This script is a REFERENCE — actual subagent spawning happens via the sessions_spawn tool.
#   Apply this LOGIC when spawning subagents in agent code (see PIPELINES.md).
#
# Retry policy:
#   - Max retries: 3
#   - Delays: 5s → 15s → 30s (exponential backoff)
#   - Parallel spawn spacing: 3s between each spawn

RETRY_DELAYS=(5 15 30)
PARALLEL_SPAWN_GAP=3  # seconds between parallel spawns

echo "=== spawn-with-retry policy ==="
echo "Max retries: 3"
echo "Backoff delays: ${RETRY_DELAYS[*]} seconds"
echo "Parallel spawn gap: ${PARALLEL_SPAWN_GAP}s"
echo ""
echo "Apply this logic in agent code when using sessions_spawn:"
echo ""
echo "PSEUDOCODE:"
echo "  for attempt in 1 2 3:"
echo "    result = sessions_spawn(task=...)"
echo "    if result.success: break"
echo "    if attempt < 3:"
echo "      wait RETRY_DELAYS[attempt] seconds"
echo "      log 'Retrying subagent spawn (attempt N/3)...'"
echo "    else:"
echo "      log 'Subagent failed after 3 attempts, escalate or skip'"
echo ""
echo "  for each parallel spawn:"
echo "    sessions_spawn(task=...)"
echo "    wait ${PARALLEL_SPAWN_GAP}s before next spawn"
