#!/bin/bash
# Workflow: consensus
# Creates a foreman + N panelists, links them as peers, and primes each.
# Panelists independently analyze the goal from different angles.
# They report back to the foreman, who synthesizes a consensus.
set -euo pipefail

SESSION_NAME="$OLLIE_SESSION_NAME"
CWD="$OLLIE_CWD"
N=${OLLIE_CONSENSUS_N:-3}

# Set status to running.
echo "running" | ollie-9p write "session/$SESSION_NAME/goalstatus"

# Create the foreman agent.
FOREMAN=$(printf 'profile=foreman\ncwd=%s\nname=foreman\n' "$CWD" \
  | ollie-9p rdwr "session/$SESSION_NAME/agent/new")
FOREMAN=$(echo "$FOREMAN" | tr -d '\n')

# Create panelist agents and establish peer links.
PANELISTS=()
ANGLES=("correctness and completeness" "simplicity and maintainability" "edge cases and failure modes")
for i in $(seq 1 "$N"); do
  NAME="panelist_${i}"
  ID=$(printf 'profile=panelist\ncwd=%s\nname=%s\n' "$CWD" "$NAME" \
    | ollie-9p rdwr "session/$SESSION_NAME/agent/new")
  ID=$(echo "$ID" | tr -d '\n')
  PANELISTS+=("$NAME")

  # Establish bidirectional peer link (foreman ↔ panelist).
  echo "peeradd $NAME" | ollie-9p rdwr "session/$SESSION_NAME/agent/$FOREMAN/ctl"
done

# Prime the foreman — it waits for panelist reports.
cat <<PROMPT | ollie-9p write "session/$SESSION_NAME/agent/$FOREMAN/prompt"
You are the foreman of a consensus panel. You have $N panelists: ${PANELISTS[*]}.

Each panelist will independently analyze the session goal and send you their findings via peer messaging. You will receive their reports as prompts — one per panelist.

**Your job:**
1. Wait for all $N panelist reports. After each report, acknowledge it briefly and note how many remain.
2. Once all $N reports are in, synthesize:
   - Identify points of agreement (consensus).
   - Identify disagreements and resolve them by evaluating the strength of each argument.
   - Produce a clear, actionable verdict.
3. Write "complete" to session/\$OLLIE_SESSION_ID/goalstatus.

Your synthesis IS your final chat response — that's where the consensus lives. Be thorough but concise.

If panelists give contradictory recommendations, reason through the conflict and pick the strongest position. Explain why.
PROMPT

# Prime each panelist with the goal and their analysis angle.
GOAL=$(ollie-9p read "session/$SESSION_NAME/goal")
for i in $(seq 1 "$N"); do
  NAME="panelist_${i}"
  IDX=$((i - 1))
  ANGLE="${ANGLES[$IDX]:-general analysis}"

  cat <<PROMPT | ollie-9p write "session/$SESSION_NAME/agent/$NAME/prompt"
You are $NAME, a panelist in a consensus review. Your analysis angle: **$ANGLE**.

## Goal to analyze

$GOAL

## Instructions

1. Analyze the goal from your assigned angle. Be thorough — read relevant code, explore the codebase, understand the context.
2. Produce a clear, structured analysis with specific findings and recommendations.
3. When done, send your complete analysis to the foreman:

\`\`\`bash
echo "<your analysis>" | ollie-9p write session/\$OLLIE_SESSION_ID/agent/\$OLLIE_UNAME/peer/foreman
\`\`\`

Be direct. No filler. Focus on your angle. Cite specific code locations when relevant.
PROMPT
done
