#!/bin/sh
root="/Users/jessegall/projects/agent-journal/.journal"
if { read -r at url < "$root/runtime/heartbeat"; } 2>/dev/null && [ $(( $(date +%s) - at )) -le 5 ]; then
session="$JOURNAL_SESSION"; [ -z "$session" ] && [ -n "$JOURNAL_SESSION_VARIABLE" ] && eval "session=\${$JOURNAL_SESSION_VARIABLE:-}"
reply=$(printf '%s\0' "$@" | curl -s -m 20 -w '\n%{http_code}' -H 'Content-Type: text/plain' --url-query "actor=$JOURNAL_ACTOR" --url-query "env=$JOURNAL_ENV" --url-query "cwd=$PWD" --url-query "plugin=$JOURNAL_PLUGIN" --url-query "session=$session" --data-binary @- "${url}api/run")
[ $? -ne 2 ] || reply=$(printf '%s\0' "$@" | curl -s -m 20 -w '\n%{http_code}' -H 'Content-Type: text/plain' --data-binary @- "$(curl -Gso /dev/null -w '%{url_effective}' --data-urlencode "actor=$JOURNAL_ACTOR" --data-urlencode "env=$JOURNAL_ENV" --data-urlencode "cwd=$PWD" --data-urlencode "plugin=$JOURNAL_PLUGIN" --data-urlencode "session=$session" "${url}api/run")")
said=${reply##*
}
body=${reply%
*}
case "$said" in
200) printf '%s' "$body"; exit 0 ;;
404|409|000|"") ;;
*) [ -n "$body" ] && printf '%s' "$body" >&2 || echo "! the journal server answered $said and said nothing" >&2; exit 1 ;;
esac
fi
[ -x "/opt/homebrew/opt/python@3.14/bin/python3.14" ] || { echo "journal: Python at /opt/homebrew/opt/python@3.14/bin/python3.14 is gone (moved or upgraded), so reinstall: re-run install.sh" >&2; exit 1; }
exec "/opt/homebrew/opt/python@3.14/bin/python3.14" "/Users/jessegall/projects/agent-journal/.journal/journal.py" --root "$root" "$@"
