diff --git a/AGENTS.md b/AGENTS.md index 4f90665..9e28876 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,7 +16,8 @@ See `README.md` for the full architecture and `BOOTSTRAP.md` for setup. disinto/ ├── dev/ dev-poll.sh, dev-agent.sh, phase-handler.sh — issue implementation ├── review/ review-poll.sh, review-pr.sh — PR review -├── gardener/ gardener-poll.sh, gardener-agent.sh — backlog grooming +├── gardener/ gardener-run.sh — files action issue for run-gardener formula +│ gardener-poll.sh, gardener-agent.sh — recipe engine + grooming ├── planner/ planner-poll.sh — files action issue for run-planner formula │ prediction-poll.sh, prediction-agent.sh — evidence-based predictions ├── supervisor/ supervisor-poll.sh — health monitoring @@ -111,12 +112,16 @@ spawns `review-pr.sh `. criteria, oversized issues, stale issues, and circular dependencies. Invoke Claude to fix or escalate to a human via Matrix. -**Trigger**: `gardener-poll.sh` runs daily (or 2x/day) via cron. Accepts an -optional project TOML argument. +**Trigger**: `gardener-run.sh` runs 2x/day via cron. It files an `action` +issue referencing `formulas/run-gardener.toml`; the [action-agent](#action-action) +picks it up and executes the gardener steps in an interactive Claude tmux session. +Accepts an optional project TOML argument. **Key files**: -- `gardener/gardener-poll.sh` — Cron wrapper: lock, escalation-reply injection for dev sessions, calls `gardener-agent.sh`, then processes dev-agent CI escalations via recipe engine +- `gardener/gardener-run.sh` — Cron wrapper: lock, memory guard, dedup check, files action issue +- `gardener/gardener-poll.sh` — Recipe engine: escalation-reply injection for dev sessions, processes dev-agent CI escalations via recipe engine (invoked by formula step ci-escalation-recipes) - `gardener/gardener-agent.sh` — Orchestrator: bash pre-analysis, creates tmux session (`gardener-{project}`) with interactive `claude`, monitors phase file, parses result file (ACTION:/DUST:/ESCALATE), handles dust bundling +- `formulas/run-gardener.toml` — Execution spec: preflight, grooming, blocked-review, CI escalation recipes, agents-update, commit-and-pr **Environment variables consumed**: - `CODEBERG_TOKEN`, `CODEBERG_REPO`, `CODEBERG_API`, `PROJECT_NAME`, `PROJECT_REPO_ROOT` diff --git a/gardener/gardener-run.sh b/gardener/gardener-run.sh new file mode 100755 index 0000000..2147123 --- /dev/null +++ b/gardener/gardener-run.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +# ============================================================================= +# gardener-run.sh — Cron wrapper: files action issue for run-gardener formula +# +# Runs 2x/day (or on-demand). Guards against concurrent runs and low memory. +# Files an action issue referencing formulas/run-gardener.toml; the action-agent +# picks it up and executes the gardener steps in an interactive Claude session. +# ============================================================================= +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +FACTORY_ROOT="$(dirname "$SCRIPT_DIR")" + +# Load shared environment (with optional project TOML override) +# Usage: gardener-run.sh [projects/harb.toml] +export PROJECT_TOML="${1:-}" +# shellcheck source=../lib/env.sh +source "$FACTORY_ROOT/lib/env.sh" + +LOG_FILE="$SCRIPT_DIR/gardener.log" +LOCK_FILE="/tmp/gardener-run.lock" + +log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%S)Z] $*" >> "$LOG_FILE"; } + +# ── Lock ────────────────────────────────────────────────────────────────── +if [ -f "$LOCK_FILE" ]; then + LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null || true) + if [ -n "$LOCK_PID" ] && kill -0 "$LOCK_PID" 2>/dev/null; then + log "poll: gardener-run running (PID $LOCK_PID)" + exit 0 + fi + rm -f "$LOCK_FILE" +fi +echo $$ > "$LOCK_FILE" +trap 'rm -f "$LOCK_FILE"' EXIT + +# ── Memory guard ────────────────────────────────────────────────────────── +AVAIL_MB=$(free -m | awk '/Mem:/{print $7}') +if [ "${AVAIL_MB:-0}" -lt 2000 ]; then + log "poll: skipping — only ${AVAIL_MB}MB available (need 2000)" + exit 0 +fi + +log "--- Gardener run start ---" + +# ── Dedup: skip if an open run-gardener action issue already exists ─────── +OPEN_ACTIONS=$(codeberg_api GET "/issues?state=open&type=issues&labels=action&limit=50" 2>/dev/null || true) +if [ -n "$OPEN_ACTIONS" ] && [ "$OPEN_ACTIONS" != "null" ]; then + EXISTING=$(printf '%s' "$OPEN_ACTIONS" | \ + jq '[.[] | select(.title | test("run-gardener"))] | length' 2>/dev/null || echo 0) + if [ "${EXISTING:-0}" -gt 0 ]; then + log "poll: open run-gardener action issue already exists — skipping" + log "--- Gardener run done ---" + exit 0 + fi +fi + +# ── Fetch 'action' label ID ────────────────────────────────────────────── +ACTION_LABEL_ID=$(codeberg_api GET "/labels" 2>/dev/null | \ + jq -r '.[] | select(.name == "action") | .id' 2>/dev/null || true) + +if [ -z "$ACTION_LABEL_ID" ]; then + log "ERROR: 'action' label not found — cannot file gardener issue" + exit 1 +fi + +# ── File action issue ───────────────────────────────────────────────────── +ISSUE_BODY="--- +formula: run-gardener +model: opus +--- + +Periodic gardener housekeeping run. The action-agent reads \`formulas/run-gardener.toml\` +and executes the steps: preflight, grooming, blocked-review, CI escalation recipes, +AGENTS.md update, and commit-and-pr. + +Filed automatically by \`gardener-run.sh\`." + +PAYLOAD=$(jq -nc \ + --arg title "action: run-gardener — periodic housekeeping" \ + --arg body "$ISSUE_BODY" \ + --argjson labels "[$ACTION_LABEL_ID]" \ + '{title: $title, body: $body, labels: $labels}') + +RESULT=$(codeberg_api POST "/issues" -d "$PAYLOAD" 2>/dev/null || true) +ISSUE_NUM=$(printf '%s' "$RESULT" | jq -r '.number // empty' 2>/dev/null || true) + +if [ -z "$ISSUE_NUM" ]; then + log "ERROR: failed to create action issue for run-gardener" + exit 1 +fi + +log "Filed action issue #${ISSUE_NUM} for run-gardener formula" +matrix_send "gardener" "Filed action #${ISSUE_NUM}: run-gardener — periodic housekeeping" 2>/dev/null || true + +log "--- Gardener run done ---"