#!/bin/bash
# Workflow: review
# Creates an author + reviewer, links them as peers, and runs an iterative
# refinement loop. The reviewer either approves or sends feedback; the author
# revises. Stops when the reviewer approves or max rounds are reached.
set -euo pipefail

SESSION_NAME="$OLLIE_SESSION_NAME"
CWD="$OLLIE_CWD"
MAX_ROUNDS=${OLLIE_REFINE_MAX:-3}
AUTHOR_PROFILE=${AUTHOR_PROFILE:-author}
REVIEWER_PROFILE=${REVIEWER_PROFILE:-reviewer}

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

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

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

# Establish bidirectional peer link (author ↔ reviewer).
echo "peeradd reviewer" | ollie-9p rdwr "session/$SESSION_NAME/agent/$AUTHOR/ctl"

# Read the goal.
GOAL=$(ollie-9p read "session/$SESSION_NAME/goal")

# Prime the reviewer — it waits for the author's submissions.
cat <<PROMPT | ollie-9p write "session/$SESSION_NAME/agent/$REVIEWER/prompt"
You are the reviewer in an iterative refinement loop with a peer named "author".

## Goal being worked on

$GOAL

## Instructions

The author will send you their work via peer messaging. For each submission:

1. Evaluate the work against the goal. Is it correct, complete, and well-structured?
2. If the work is satisfactory, write "complete" to session/\$OLLIE_SESSION_ID/goalstatus.
3. If the work needs improvement, send specific, actionable feedback to the author:
   \`\`\`bash
   echo "<your feedback>" | ollie-9p write session/\$OLLIE_SESSION_ID/agent/\$OLLIE_UNAME/peer/author
   \`\`\`

## Convergence rule

This loop has a hard cap of $MAX_ROUNDS rounds. If you have sent feedback $((MAX_ROUNDS - 1)) times already (meaning the author is now on their final revision), you MUST approve the next submission regardless of remaining issues. Write "complete" to goalstatus.

Track how many rounds of feedback you have given. Do not exceed the cap.

## Feedback style

- Be specific: cite what's wrong, where, and what to change.
- Be concise: no padding, no praise sandwiches.
- Prioritize: focus on the most impactful issues first.
PROMPT

# Prime the author with the goal.
cat <<PROMPT | ollie-9p write "session/$SESSION_NAME/agent/$AUTHOR/prompt"
You are the author in an iterative refinement loop with a peer named "reviewer".

## Goal

$GOAL

## Instructions

1. Produce your best initial work to satisfy the goal above.
2. When done, send your complete output to the reviewer:
   \`\`\`bash
   echo "<your work>" | ollie-9p write session/\$OLLIE_SESSION_ID/agent/\$OLLIE_UNAME/peer/reviewer
   \`\`\`
3. The reviewer will either approve (ending the loop) or send feedback.
4. When you receive feedback, revise your work incorporating the feedback and send the complete updated version back to the reviewer.

## Rules

- Each submission must be complete and self-contained — not a diff or partial update.
- Incorporate all feedback points. If you disagree with a point, explain why in your submission.
- You have at most $MAX_ROUNDS rounds total. Make each one count.
PROMPT
