#!/usr/bin/env bash
# Human-in-the-loop reproduction loop.
# Generated by the diagnosing-bugs skill (/skill:diagnosing-bugs).
# The agent copies this file and edits the steps below; the human runs it.
# The human runs the script in their own terminal and follows its prompts.
# The agent never runs it, in whole or in part: it only checks it with
# `bash -n` or `shellcheck`, and reads the Captured block the human pastes back.
#
# Usage (the human, in their own terminal):
#   bash hitl-loop.template.sh
#
# Two helpers:
#   step "<instruction>"          → show instruction, wait for Enter
#   capture VAR "<question>"      → show question, read response into VAR
#
# At the end, every value `capture` read is printed as KEY=VALUE, whatever its
# name; paste that block back to the agent. Record every observation with
# `capture`, so the block carries it; edit only between the markers.
#
# `capture` prints its value back to the terminal, and the pasted block reaches
# the agent, so capture observations, never a password, token or other secret,
# and leave signing in to the user as a `step`.

set -euo pipefail

# The names `capture` filled and their values, in order, for the Captured block.
# Values are kept here too, so a VAR named like a helper's local still prints.
CAPTURED=()
CAPTURED_VALUES=()

step() {
  printf '\n>>> %s\n' "$1"
  read -r -p "    [Enter when done] " _
}

capture() {
  local _hitl_var="$1" _hitl_question="$2" _hitl_answer
  case "$_hitl_var" in
    _hitl_* | CAPTURED | CAPTURED_VALUES | [!A-Za-z_]* | *[!A-Za-z0-9_]*)
      printf 'capture: %s is not a usable variable name\n' "$_hitl_var" >&2
      exit 2
      ;;
  esac
  printf '\n>>> %s\n' "$_hitl_question"
  read -r -p "    > " _hitl_answer
  printf -v "$_hitl_var" '%s' "$_hitl_answer"
  CAPTURED+=("$_hitl_var")
  CAPTURED_VALUES+=("$_hitl_answer")
}

# --- edit below ---------------------------------------------------------

step "Open the app at http://localhost:3000 and sign in."

capture ERRORED "Click the 'Export' button. Did it throw an error? (y/n)"

capture ERROR_MSG "Paste the error message (or 'none'):"

# --- edit above ---------------------------------------------------------

printf '\n--- Captured ---\n'
# The +-expansion keeps an empty list safe under `set -u` in bash 3.2 (macOS).
_hitl_i=0
while [ "$_hitl_i" -lt "${#CAPTURED[@]}" ]; do
  printf '%s=%s\n' "${CAPTURED[$_hitl_i]}" "${CAPTURED_VALUES[$_hitl_i]}"
  _hitl_i=$((_hitl_i + 1))
done
