#!/usr/bin/env bash
# arq-worker — operator entrypoint for substrate-routed ARQERA workloads.
#
# The substrate runs the workload. The substrate signs the evidence. The
# substrate emits the act. GitHub is mirrored only at the end, if asked.
#
# Usage:
#   scripts/arq-worker <workload> [worker-specific flags]
#
# Workloads (current):
#   frontend-dependency-audit    — pnpm audit --audit-level=high in frontend/
#   exception-governor           — scan bypass markers for bonded-exception compliance
#   policy-approver              — classify open PRs against the approval rule set
#
# Registry (substrate is the truth, not a JSON file):
#   registry register-all        — publish every worker's WORKER_SPEC as
#                                  arq://body/worker/<name>
#   registry list                — list registered workers from substrate
#   registry show <name>         — fetch one worker's spec from substrate
#   registry verbs               — list all verbs across in-tree workers
#   registry who-provides <verb> — find which worker(s) declare a verb
#   registry cost                — show declared cost-class per worker
#   registry freshness [name]    — derive last-invocation age from substrate
#   registry health [name]       — derive health verdict from substrate evidence
#
# Examples:
#   scripts/arq-worker frontend-dependency-audit
#   scripts/arq-worker frontend-dependency-audit --mirror-to-github
#   scripts/arq-worker frontend-dependency-audit --json | jq .
#   scripts/arq-worker exception-governor
#   scripts/arq-worker exception-governor --mode all --json
#   scripts/arq-worker policy-approver
#   scripts/arq-worker policy-approver --json | jq .
#   scripts/arq-worker registry register-all
#   scripts/arq-worker registry list
#   scripts/arq-worker registry show policy-approver
#   scripts/arq-worker registry verbs
#   scripts/arq-worker registry who-provides pr.classify
#   scripts/arq-worker registry cost
#   scripts/arq-worker registry freshness
#   scripts/arq-worker registry health policy-approver

set -euo pipefail

# Resolve symlinks so ROOT_DIR points at the real ARQERA repo whether
# this script is invoked directly (scripts/arq-worker) or via a symlink
# in ~/.local/bin/arq-worker. Per arq://doc/twin_memory/installer-skips-real-file-at-symlink-path
# (2026-05-14), CLI installers + the symlink layer have repeatedly
# tripped on resolving `${BASH_SOURCE[0]}` through symlinks — without
# this loop, a ~/.local/bin symlink computes ROOT_DIR as ~/.local
# (wrong) instead of the real ARQERA repo (correct).
SOURCE="${BASH_SOURCE[0]}"
while [[ -L "$SOURCE" ]]; do
  TARGET="$(readlink "$SOURCE")"
  [[ "$TARGET" = /* ]] || TARGET="$(dirname "$SOURCE")/$TARGET"
  SOURCE="$TARGET"
done
ROOT_DIR="$(cd "$(dirname "$SOURCE")/.." && pwd)"
WORKLOAD="${1:-}"

if [[ -z "$WORKLOAD" ]]; then
  echo "Usage: scripts/arq-worker <workload> [flags]" >&2
  echo "Workloads:" >&2
  echo "  frontend-dependency-audit    pnpm audit --audit-level=high (substrate-routed)" >&2
  echo "  exception-governor           scan bypass markers for bonded-exception compliance" >&2
  echo "  policy-approver              classify open PRs against the approval rule set" >&2
  echo "Registry:" >&2
  echo "  registry register-all        publish every WORKER_SPEC to substrate" >&2
  echo "  registry list                list registered workers from substrate" >&2
  echo "  registry show <name>         fetch one worker's spec from substrate" >&2
  echo "  registry verbs               list all verbs across in-tree workers" >&2
  echo "  registry who-provides <verb> find which worker(s) declare a verb" >&2
  echo "  registry cost                show declared cost-class per worker" >&2
  echo "  registry freshness [name]    derive last-invocation age from substrate" >&2
  echo "  registry health [name]       derive health verdict from substrate evidence" >&2
  exit 2
fi

shift || true

case "$WORKLOAD" in
  frontend-dependency-audit|frontend-audit|fea)
    exec python3 "$ROOT_DIR/workers/frontend_dependency_audit/run.py" "$@"
    ;;
  exception-governor|exc-gov|eg)
    exec python3 "$ROOT_DIR/workers/exception_governor/run.py" "$@"
    ;;
  policy-approver|policy|pa)
    exec python3 "$ROOT_DIR/workers/policy_approver/run.py" "$@"
    ;;
  registry|reg)
    exec python3 "$ROOT_DIR/workers/_common/registry_cli.py" "$@"
    ;;
  *)
    echo "arq-worker: unknown workload: $WORKLOAD" >&2
    echo "Known: frontend-dependency-audit, exception-governor, policy-approver, registry" >&2
    exit 2
    ;;
esac
