#!/bin/bash
# Workflow: conductor
# Creates a conductor agent, primes it with instructions, and exits.
# The conductor agent handles the rest autonomously.
set -euo pipefail

SESSION_NAME="$OLLIE_SESSION_NAME"
CWD="$OLLIE_CWD"

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

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

# Prime the conductor with instructions.
# The agent receives OLLIE_SESSION_ID in its environment.
PROMPT_PATH="session/$SESSION_NAME/agent/$AGENT_ID/prompt"
cat <<'EOF' | ollie-9p write "$PROMPT_PATH"
Read the goal from session/$OLLIE_SESSION_ID/goal.

Analyze and understand the goal. If anything is ambiguous, investigate the codebase to clarify.

Break it down into concrete, actionable steps. Rewrite the goal file with your structured plan — use a checklist format so progress can be tracked.

Delegate each step to sub-agents using subagent_spawn. As steps complete, update the goal file to mark progress.

When all steps are complete, write "complete" to session/$OLLIE_SESSION_ID/goalstatus.
If you get stuck and need human input, write "blocked" to session/$OLLIE_SESSION_ID/goalstatus.
EOF
