<?xml version="1.0" encoding="UTF-8"?>
<!--
  launchd keep-alive for the shared `fak serve` gateway — the always-on sibling
  of com.fleet.dispatch-supervisor.plist. Where THAT plist polls a watchdog every
  5 minutes, THIS one is a true 24/7 daemon: KeepAlive=true means launchd respawns
  `fak serve` the instant it crashes or exits, and RunAtLoad=true starts it at
  load (and at every login / boot). One local gateway on 127.0.0.1:8080 is the
  shared inference seam every dispatch worker on this node dials, so it must
  outlive any single session.

  Sleep prevention: the M-series mac node sleeps ~every 25 minutes. This plist
  routes fak serve through tools/fak-serve-caffeinate.sh, which asserts -is
  (idle+system) sleep prevention via `caffeinate -is -w $$` then execs fak serve.
  After exec, launchd's direct child IS fak serve (KeepAlive tracks the right
  process) and fak serve's stdio flows to StandardOutPath/StandardErrorPath.
  For OFF-LAN reach, install with `--bind-all` (see below) and keep both machines
  on the same Tailscale network.

  This is a TEMPLATE: replace the __PLACEHOLDER__ tokens with absolute paths
  for your node, then install it as a per-user agent. Use the one-command
  installer (preferred) or the manual sed path below.

  ONE COMMAND (preferred — handles all three units + caffeinate):
      ./tools/install-mac-node.sh
      # Off-host access (0.0.0.0 + bearer key):
      ./tools/install-mac-node.sh --bind-all

  MANUAL (single unit):
      sed -e "s#__FAK__#$(command -v fak)#" \
          -e "s#__REPO__#$(cd "$(dirname "$0")/.." && pwd)#" \
          -e "s#__ADDR__#127.0.0.1:8080#" \
          tools/com.fak.serve-gateway.plist \
        > ~/Library/LaunchAgents/com.fak.serve-gateway.plist
      launchctl load -w ~/Library/LaunchAgents/com.fak.serve-gateway.plist

  Set the upstream credential in your login environment (NOT in this template):

      launchctl setenv ANTHROPIC_API_KEY "sk-ant-..."

  Tail it, or unload it, with:

      tail -f tools/_watchdog/launchd_serve.log
      launchctl unload -w ~/Library/LaunchAgents/com.fak.serve-gateway.plist
-->
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.fak.serve-gateway</string>

    <key>ProgramArguments</key>
    <array>
      <!-- fak-serve-caffeinate.sh asserts -is (idle+system) sleep prevention via
           `caffeinate -is -w $$`, then execs fak serve. exec keeps launchd's direct
           child as fak serve (KeepAlive tracks the right process) and preserves stdio
           so StandardOutPath / StandardErrorPath actually receive fak serve's output.
           Using caffeinate as the top-level command directly loses all stdio. -->
      <string>__REPO__/tools/fak-serve-caffeinate.sh</string>
      <string>__FAK__</string>
      <string>serve</string>
      <string>--provider</string>
      <string>anthropic</string>
      <string>--base-url</string>
      <string>https://api.anthropic.com</string>
      <string>--addr</string>
      <string>__ADDR__</string>
      <string>--policy</string>
      <string>__REPO__/examples/dogfood-claude-policy.json</string>
    </array>

    <key>EnvironmentVariables</key>
    <dict>
      <key>FAK_AUDIT_JOURNAL</key>
      <string>__REPO__/tools/_watchdog/serve_audit.jsonl</string>
    </dict>

    <key>WorkingDirectory</key>
    <string>__REPO__</string>

    <key>KeepAlive</key>
    <true/>

    <key>RunAtLoad</key>
    <true/>

    <key>StandardOutPath</key>
    <string>__REPO__/tools/_watchdog/launchd_serve.log</string>
    <key>StandardErrorPath</key>
    <string>__REPO__/tools/_watchdog/launchd_serve.err</string>
  </dict>
</plist>
