#!/bin/bash
# Periodic worker health check: orphan bead recovery, idle worker nudging, auto-mount.
# Usage: anvillm-supervisor [--orphans] [--nudge] [--auto-mount <workdir>]
set -euo pipefail

LOG_PREFIX="[supervisor $(date '+%Y-%m-%d %H:%M:%S')]"
log() { echo "$LOG_PREFIX $*"; }

DO_ORPHANS=0
DO_NUDGE=0
AUTO_MOUNT_DIR=""

while [[ $# -gt 0 ]]; do
    case "$1" in
        --orphans) DO_ORPHANS=1; shift ;;
        --nudge)   DO_NUDGE=1;   shift ;;
        --auto-mount) AUTO_MOUNT_DIR="$2"; shift 2 ;;
        *) echo "unknown argument: $1" >&2; exit 1 ;;
    esac
done

if [ "$DO_ORPHANS" -eq 0 ] && [ "$DO_NUDGE" -eq 0 ] && [ -z "$AUTO_MOUNT_DIR" ]; then
    echo "usage: anvillm-supervisor [--orphans] [--nudge] [--auto-mount <workdir>]" >&2
    exit 1
fi

# --- Auto-mount beads for workdir ---
if [ -n "$AUTO_MOUNT_DIR" ]; then
    MTAB=$(9p read anvillm/beads/mtab 2>/dev/null || true)
    EXISTING=$(echo "$MTAB" | awk -F'\t' -v dir="$AUTO_MOUNT_DIR" '$2 == dir {print $1; exit}')
    if [ -n "$EXISTING" ]; then
        echo "error: already mounted as $EXISTING" >&2
        exit 1
    fi
    echo "mount $AUTO_MOUNT_DIR" | 9p write anvillm/beads/ctl
    # Read back the mount name from mtab
    9p read anvillm/beads/mtab 2>/dev/null | awk -F'\t' -v dir="$AUTO_MOUNT_DIR" '$2 == dir {print $1; exit}'
    exit 0
fi

SESSIONS=$(9p read anvillm/list 2>/dev/null || true)

# --- Orphan recovery ---
if [ "$DO_ORPHANS" -eq 1 ]; then
    LIVE_SESSIONS=$(echo "$SESSIONS" | awk -F'\t' '{print $1}' || true)

    MTAB=$(9p read anvillm/beads/mtab 2>/dev/null || true)
    if [ -z "$MTAB" ]; then
        log "orphans: no mounts, nothing to do"
    else
        while IFS=$'\t' read -r MOUNT CWD; do
            [ -z "$MOUNT" ] && continue

            IN_PROGRESS=$(9p read "anvillm/beads/$MOUNT/list" 2>/dev/null \
                | jq -r '.[] | select(.status == "in_progress") | [.id, (.assignee // "")] | @tsv' \
                2>/dev/null || true)

            [ -z "$IN_PROGRESS" ] && continue

            while IFS=$'\t' read -r BEAD_ID ASSIGNEE; do
                [ -z "$BEAD_ID" ] && continue
                [ "$ASSIGNEE" = "user" ] && continue
                if [ -n "$ASSIGNEE" ] && echo "$LIVE_SESSIONS" | grep -qx "$ASSIGNEE"; then
                    continue
                fi
                log "orphan: $BEAD_ID (was: ${ASSIGNEE:-unassigned}) mount=$MOUNT — unclaiming"
                echo "unclaim $BEAD_ID" | 9p write "anvillm/beads/$MOUNT/ctl" 2>/dev/null || \
                    log "WARNING: unclaim failed for $BEAD_ID"
            done <<< "$IN_PROGRESS"
        done <<< "$MTAB"
    fi
fi

# --- Nudge idle workers ---
if [ "$DO_NUDGE" -eq 1 ]; then
    [ -z "$SESSIONS" ] && { log "nudge: no sessions"; exit 0; }

    BODY='Use the execute_code tool to run wait_for_bead.sh and check for available work.'

    while IFS=$'\t' read -r ID BACKEND STATE ALIAS ROLE CWD; do
        [ -z "$ID" ] && continue
        [ "$STATE" != "idle" ] && continue
        [ "$ROLE" = "-" ] || [ -z "$ROLE" ] && continue

        ROLE_CONTENT=$(9p read "anvillm/roles/${ROLE}.md" 2>/dev/null || true)
        echo "$ROLE_CONTENT" | grep -q '^worker: true' || continue

        log "nudge: $ID (role=$ROLE)"
        AGENT_ID=user bash <(9p read anvillm/tools/send_message.sh) \
            --to "$ID" \
            --type PROMPT_REQUEST \
            --subject work \
            --body "$BODY" || \
            log "WARNING: nudge failed for $ID"
    done <<< "$SESSIONS"
fi
