#!/usr/bin/env bash
# Background poller entry point for the agent coordination bulletin.
#
#   ./poll-cron
#
# Wraps `fleet.bulletin.poll` in the fleet uv environment. This is what a
# systemd user timer (see settings/systemd/user/fleet-bulletin-poll.*.example)
# calls every 5 minutes. It snapshots ~/ai/bulletin.md into a scratch git repo
# (~/.ai/bulletin-history/) and runs symbolic overlap checks, invoking a DSPy
# agent via the gateway only when an ambiguous conflict needs assessment.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FLEET="$(cd "$HERE/../.." && pwd)"

# No secrets-manager wrapper. The poller needs exactly one secret — a gateway
# key — and only on the rare tick that escalates an ambiguous conflict to
# DSPy. Deployments that use systemd-creds can load it with
# `LoadCredentialEncrypted=` (a *virtual key* scoped to the poller's one
# model, never the gateway master key) and export it here as LITELLM_MASTER_KEY
# (the env var `fleet.bulletin.poll` reads; the name is historical). Without
# it, the poller degrades to the deterministic overlap alerts.
if [ -f "${CREDENTIALS_DIRECTORY:-}/litellm_bulletin_key" ]; then
  export LITELLM_MASTER_KEY="$(cat "$CREDENTIALS_DIRECTORY/litellm_bulletin_key")"
fi

exec uv run --project "$FLEET" python -m fleet.bulletin.poll "$@"
