#!/usr/bin/env bash
# Human-in-the-loop reproduction loop.
# Copy this file, edit the steps below, and run it.
# The agent runs the script; the user follows prompts in their terminal.
#
# Usage:
#   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, captured values are printed as KEY=VALUE for the agent to parse.
#
# `capture` prints its value back to the terminal, where the agent reads it — so
# capture observations, and leave signing in to the user as a `step`.

set -euo pipefail

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

capture() {
  local var="$1" question="$2" answer redacted_answer
  printf '\n>>> %s\n' "$question"
  read -r -p "    > " answer
  redacted_answer=$(printf '%s' "$answer" |
    sed -E \
      -e 's/\\(["])/\1/g' \
      -e 's/(([Aa][Uu][Tt][Hh][Oo][Rr][Ii][Zz][Aa][Tt][Ii][Oo][Nn])[[:space:]]*[:=][[:space:]]*)("[^"]*"|\047[^\047]*\047|[^[:space:]].*)/\1<REDACTED>/g' \
      -e 's/(([Cc][Oo][Oo][Kk][Ii][Ee])[[:space:]]*[:=][[:space:]]*)("[^"]*"|\047[^\047]*\047|[^[:space:]].*)/\1<REDACTED>/g' \
      -e 's/("[^"]*([Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt]|[Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Pp][Aa][Ss][Ss][Ww][Dd]|[Aa][Uu][Tt][Hh][Oo][Rr][Ii][Zz][Aa][Tt][Ii][Oo][Nn]|[Cc][Oo][Oo][Kk][Ii][Ee]|[Dd][Ss][Nn]|[Aa][Pp][Ii][^"]*[Kk][Ee][Yy])[^"]*"[[:space:]]*:[[:space:]]*")[^"]*"/\1<REDACTED>"/g' \
      -e 's/(([[:alnum:]_-]*([Aa][Pp][Ii][-_]?[Kk][Ee][Yy]|[Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt]|[Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Pp][Aa][Ss][Ss][Ww][Dd]|[Dd][Ss][Nn]|[Oo][Aa][Uu][Tt][Hh][-_]?[Tt][Oo][Kk][Ee][Nn]|[Aa][Uu][Tt][Hh][Oo][Rr][Ii][Zz][Aa][Tt][Ii][Oo][Nn]|[Cc][Oo][Oo][Kk][Ii][Ee])[[:alnum:]_-]*)[[:space:]]*[:=][[:space:]]*)("[^"]*"|\047[^\047]*\047|[^[:space:]]+)/\1<REDACTED>/g' \
      -e 's/(https?:\/\/)[^[:space:]\/:]+:[^[:space:]@]+@/\1<REDACTED>@/g')
  printf -v "$var" '%s' "$redacted_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 a redacted error message (replace secrets with <REDACTED>, or 'none'):"

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

printf '\n--- Captured ---\n'
printf 'ERRORED=%s\n' "$ERRORED"
printf 'ERROR_MSG=%s\n' "$ERROR_MSG"
