# Advanced Parallel Execution (Tier 7)
#
# Demonstrates join strategies and failure policies
# for parallel blocks.

agent researcher:
  model: haiku
  prompt: "You are a research assistant. Provide concise information."

# 1. Race Pattern: First to Complete Wins
# ----------------------------------------
# Use parallel ("first") when you want the fastest result
# and don't need all branches to complete.

parallel ("first"):
  session: researcher
    prompt: "Find information via approach A"
  session: researcher
    prompt: "Find information via approach B"
  session: researcher
    prompt: "Find information via approach C"

session "Summarize: only the fastest approach completed"

# 2. Any-N Pattern: Get Multiple Quick Results
# --------------------------------------------
# Use parallel ("any", count: N) when you need N results
# but not necessarily all of them.

parallel ("any", count: 2):
  a = session "Generate a creative headline for a tech blog"
  b = session "Generate a catchy headline for a tech blog"
  c = session "Generate an engaging headline for a tech blog"
  d = session "Generate a viral headline for a tech blog"

session "Choose the best from the 2 headlines that finished first"
  context: { a, b, c, d }

# 3. Continue on Failure: Gather All Results
# ------------------------------------------
# Use on-fail: "continue" when you want all branches
# to complete and handle failures afterwards.

parallel (on-fail: "continue"):
  session "Fetch data from primary API"
  session "Fetch data from secondary API"
  session "Fetch data from backup API"

session "Combine all available data, noting any failures"

# 4. Ignore Failures: Best-Effort Enrichment
# ------------------------------------------
# Use on-fail: "ignore" for optional enrichments
# where failures shouldn't block progress.

parallel (on-fail: "ignore"):
  session "Get optional metadata enrichment 1"
  session "Get optional metadata enrichment 2"
  session "Get optional metadata enrichment 3"

session "Continue with whatever enrichments succeeded"

# 5. Combined: Race with Resilience
# ---------------------------------
# Combine join strategies with failure policies.

parallel ("first", on-fail: "continue"):
  session "Fast but might fail"
  session "Slow but reliable"

session "Got the first result, even if it was a handled failure"
