#!/usr/bin/env ruby
# frozen_string_literal: true
# ag-run — the zero-infrastructure driver. Runs `/ag-build` in a fresh
# `claude -p` process per item, so a long unattended drain never lets one
# session's context fill up across many items. Stops cleanly at the first
# pause a human needs to answer — a detached process can't answer a
# checkpoint; answer it in Agentile Projects (or with `printf 'reply' |
# ag-store checkpoint_answer <id> --by <you>`) and re-run with the same
# AGENTILE_RUNNER_ID. For parallel workers, chosen models, and the app's
# dashboards, see docs/agentile-factory.md.
#
# Usage: ag-run [--limit N] [-- <claude flags>]
#   --limit N   stop after N items even if more are ready (default: unlimited —
#               runs until idle, paused, or failed)
#   -- ...      everything after -- is passed through to `claude` verbatim,
#               e.g. `--permission-mode acceptEdits`. ag-run sets no
#               permission mode itself; a headless run needs one from the
#               project's own settings or a flag here, since nothing here can
#               answer a permission prompt.
#
#   Recommended invocation:
#     ag-run -- --permission-mode acceptEdits --permission-prompts none --allowedTools "Bash(ag-store:*)" "Bash(git:*)"
#   Add this project's gates.json commands to that --allowedTools list too.
#
# AGENTILE_RUNNER_ID, if already set in the environment, names this driver's
# claim identity (so a crashed run can be resumed by re-running with the same
# id — see skills/ag-build/SKILL.md, "Identity"). If unset, ag-run picks
# one and prints it, stable for the life of this process across every item it
# drains.
require "open3"
require "socket"

args = ARGV.dup
limit = nil
claude_args = []
if (i = args.index("--"))
  claude_args = args[(i + 1)..] || []
  args = args[0...i]
end
while (flag = args.shift)
  case flag
  when "--limit"
    limit = args.shift.to_i
  else
    abort "ag-run: unknown argument #{flag.inspect} (usage: ag-run [--limit N] [-- <claude flags>])"
  end
end

runner_id = ENV["AGENTILE_RUNNER_ID"]
if runner_id.to_s.empty?
  runner_id = "ag-run@#{Socket.gethostname}/#{Process.pid}"
  ENV["AGENTILE_RUNNER_ID"] = runner_id
end
puts "[ag-run] runner id: #{runner_id}"

STATUS_LINE = /^AG_(?:BUILD|LOOP):\s*(\S+)(?:\s+(\S+))?(?:\s+(.*))?$/

def tail(text, n = 4000)
  text.length > n ? text[-n..] : text
end

item = 0
loop do
  item += 1
  if limit && item > limit
    puts "[ag-run] reached --limit #{limit}, stopping."
    exit 0
  end

  puts "[ag-run] item #{item}: running claude -p \"/ag-build\"..."
  out, err, status = Open3.capture3("claude", "-p", "/ag-build", *claude_args)

  unless status.success?
    warn "[ag-run] claude exited #{status.exitstatus}"
    warn err unless err.strip.empty?
    warn tail(out)
    exit 1
  end

  # A model sometimes wraps the status line in backticks or bold despite the
  # skill's instruction; strip such decoration before matching.
  line = out.lines.map { |l| l.strip.sub(/\A[`*_]+/, "").sub(/[`*_]+\z/, "") }
            .reverse.find { |l| l.start_with?("AG_BUILD:", "AG_LOOP:") }
  unless line
    warn "[ag-run] no AG_BUILD status line found in output — showing the tail:"
    warn tail(out)
    exit 1
  end

  m = STATUS_LINE.match(line)
  outcome, arg2, arg3 = m[1], m[2], m[3]
  puts "[ag-run] item #{item}: #{line}"

  case outcome
  when "shipped"
    next
  when "idle"
    puts "[ag-run] backlog idle (#{arg2}) — nothing more to do. Stopping."
    exit 0
  when "paused"
    reason, checkpoint = arg3.to_s.split(/\s+/, 2)
    puts "[ag-run] paused on #{arg2} (#{reason}) — a human checkpoint needs an answer."
    puts "[ag-run] checkpoint: #{checkpoint}" unless checkpoint.to_s.empty?
    puts "[ag-run] to resume: answer it in Agentile Projects (or: printf 'reply' | ag-store checkpoint_answer #{checkpoint} --by you),"
    puts "[ag-run] then re-run ag-run with the same AGENTILE_RUNNER_ID=#{runner_id}."
    exit 0
  when "failed"
    warn "[ag-run] failed on #{arg2} (#{arg3}). Stopping."
    exit 1
  else
    warn "[ag-run] unrecognised AG_BUILD status: #{line.inspect}"
    exit 1
  end
end
