Batches the remaining comment-noise cleanups on top of the activity-log fix:
- Resume comment no longer re-posts the full PR description (## Summary/## Changes) — the PR body and
diff already carry it; the comment just links the PR (the reasoning trail shows what the run did).
- Strip a leading self-header the model sometimes emits ("🤖 **@pm**" on its own line, sometimes
twice) before we prepend our own, so headers aren't doubled.
- Also drop the BEGIN_PR_DESCRIPTION block from the plain reply text, and simplify the activity
block's summary (it repeated "🤖 **@name** — activity log").
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
193 lines
11 KiB
Bash
Executable File
193 lines
11 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Publish — PR (dev agents) or comment (pm/qa), always reply in the issue.
|
|
#
|
|
# Required env (provided by the workflow step):
|
|
# GT AGENT_TOKEN TOKEN_PM TOKEN_SENIOR TOKEN_JUNIOR TOKEN_LEAD TOKEN_QA
|
|
# NAME MODE NUM TITLE BRANCH NEW GITHUB_SERVER_URL GITHUB_REPOSITORY
|
|
set +e # publish is best-effort: a grep-no-match / curl non-zero must NOT kill the step
|
|
# Post/PR as the agent's OWN Gitea user when its token is configured; else the built-in bot.
|
|
case "$NAME" in
|
|
pm) TOK="$TOKEN_PM";; senior) TOK="$TOKEN_SENIOR";; junior) TOK="$TOKEN_JUNIOR";;
|
|
lead) TOK="$TOKEN_LEAD";; qa) TOK="$TOKEN_QA";; *) TOK="";;
|
|
esac
|
|
[ -z "$TOK" ] && TOK="$GT"
|
|
git config user.name "$NAME"
|
|
git config user.email "$NAME@ffaerber.duckdns.org"
|
|
API="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}"
|
|
hdr=(-H "Authorization: token $TOK" -H "Content-Type: application/json")
|
|
post() { curl -sS -w 'comment -> HTTP %{http_code}\n' -X POST "${hdr[@]}" \
|
|
"$API/issues/$NUM/comments" -d "$(jq -nc --arg b "$1" '{body:$b}')"; }
|
|
|
|
# drop machine-readable markers: DELEGATE / CLOSE_ISSUE, and the BEGIN_SUBTASKS..END_SUBTASKS and
|
|
# BEGIN_PR_DESCRIPTION..END_PR_DESCRIPTION blocks (the PR description is published separately).
|
|
reply=$(awk '
|
|
/^[[:space:]]*BEGIN_SUBTASKS/{s=1}
|
|
/^[[:space:]]*BEGIN_PR_DESCRIPTION/{p=1}
|
|
/^[[:space:]]*DELEGATE:[[:space:]]*@/{next}
|
|
/^[[:space:]]*CLOSE_ISSUE[[:space:]]*$/{next}
|
|
s{ if(/^[[:space:]]*END_SUBTASKS/){s=0}; next }
|
|
p{ if(/^[[:space:]]*END_PR_DESCRIPTION/){p=0}; next }
|
|
{print}
|
|
' /tmp/agent_out.md 2>/dev/null)
|
|
# Strip a leading self-header the model sometimes emits ("🤖 **@pm**" on its own line) so we don't
|
|
# double it when we prepend our own. Removes a leading run of such header lines and blank lines.
|
|
reply=$(printf '%s' "$reply" | awk '
|
|
BEGIN{s=1}
|
|
s && /^[^A-Za-z0-9]*\*\*@[A-Za-z]+\*\*[[:space:]]*$/ {next}
|
|
s && /^[[:space:]]*$/ {next}
|
|
{s=0; print}
|
|
')
|
|
[ -z "$reply" ] && reply="_(Made changes without a text summary — see the diff below.)_"
|
|
# Prefer the agent's clean delimited PR description; fall back to the whole reply.
|
|
prdesc=$(awk '/BEGIN_PR_DESCRIPTION/{f=1;next} /END_PR_DESCRIPTION/{f=0} f' /tmp/agent_out.md)
|
|
[ -z "$prdesc" ] && prdesc="$reply"
|
|
|
|
# comment-only roles (pm/qa): never change files
|
|
if [ "$MODE" != "pr" ]; then
|
|
git checkout -- . 2>/dev/null || true
|
|
git clean -fd 2>/dev/null || true
|
|
target=$(grep -oiE 'DELEGATE:[[:space:]]*@(junior|senior|lead|qa)' /tmp/agent_out.md 2>/dev/null | head -1 | grep -oiE '(junior|senior|lead|qa)' | tr '[:upper:]' '[:lower:]')
|
|
# Visible comment: the reply text, or a sensible line if the agent only emitted a marker.
|
|
msg="$reply"
|
|
case "$msg" in ""|"_(Made changes"*) msg=$([ -n "$target" ] && echo "Handing off to @$target." || echo "_(no further comment)_") ;; esac
|
|
# Close the issue if the agent flagged it (maintainer said it's not needed / duplicate).
|
|
if grep -qiE '^[[:space:]]*CLOSE_ISSUE[[:space:]]*$' /tmp/agent_out.md; then
|
|
echo "closing issue #$NUM"
|
|
curl -sS -X PATCH "${hdr[@]}" "$API/issues/$NUM" \
|
|
-d '{"state":"closed"}' -w '\nclose -> HTTP %{http_code}\n' || true
|
|
fi
|
|
# BREAKDOWN: from a BEGIN_SUBTASKS block, create a milestone + one sub-issue per line
|
|
# (linked to this issue). Sub-issues are NOT auto-started — maintainer mentions agents later.
|
|
# Process subtasks first so we can append the created-issues list to the SAME comment as
|
|
# the reply (issue #38 — one comment per run).
|
|
subtext=""
|
|
if grep -qiE '^[[:space:]]*BEGIN_SUBTASKS' /tmp/agent_out.md; then
|
|
block=$(awk '/^[[:space:]]*BEGIN_SUBTASKS/{f=1;next} /^[[:space:]]*END_SUBTASKS/{f=0} f' /tmp/agent_out.md)
|
|
ms=$(printf '%s\n' "$block" | sed -nE 's/^[[:space:]]*milestone:[[:space:]]*//Ip' | head -1)
|
|
msid=""
|
|
if [ -n "$ms" ]; then
|
|
msid=$(curl -sS "${hdr[@]}" "$API/milestones?state=open&limit=100" | jq -r --arg t "$ms" 'if type=="array" then ([.[]|select(.title==$t)][0].id // empty) else empty end')
|
|
[ -z "$msid" ] && msid=$(curl -sS -X POST "${hdr[@]}" "$API/milestones" -d "$(jq -nc --arg t "$ms" '{title:$t}')" | jq -r '.id // empty')
|
|
echo "milestone '$ms' -> id ${msid:-?}"
|
|
fi
|
|
printf '%s\n' "$block" | grep -E '^[[:space:]]*-[[:space:]]' > /tmp/subtasks.txt || true
|
|
links=""
|
|
while IFS= read -r line; do
|
|
item=$(printf '%s' "$line" | sed -E 's/^[[:space:]]*-[[:space:]]*//')
|
|
title=${item%%::*}; body=${item#*::}; [ "$body" = "$item" ] && body=""
|
|
title=$(printf '%s' "$title" | sed -E 's/[[:space:]]*$//')
|
|
body=$(printf '%s' "$body" | sed -E 's/^[[:space:]]*//')
|
|
[ -z "$title" ] && continue
|
|
ibody=$(printf 'Part of #%s\n\n%s' "$NUM" "$body")
|
|
if [ -n "$msid" ]; then
|
|
payload=$(jq -nc --arg t "$title" --arg b "$ibody" --argjson m "$msid" '{title:$t,body:$b,milestone:$m}')
|
|
else
|
|
payload=$(jq -nc --arg t "$title" --arg b "$ibody" '{title:$t,body:$b}')
|
|
fi
|
|
n=$(curl -sS -X POST "${hdr[@]}" "$API/issues" -d "$payload" | jq -r '.number // empty')
|
|
echo "created sub-issue #${n:-?}: $title"
|
|
[ -n "$n" ] && links="$links\n- #$n — $title"
|
|
done < /tmp/subtasks.txt
|
|
subtext=$(printf '\n\n---\n🤖 **@%s** — created sub-issues%s (mention an agent on each when ready):%b' "$NAME" "${ms:+ under milestone **$ms**}" "$links")
|
|
fi
|
|
post "$(printf '🤖 **@%s**\n\n%s%s' "$NAME" "$msg" "$subtext")"
|
|
# Auto-delegate: if the plan names a teammate, trigger them via AGENT_TOKEN (a PAT, so it
|
|
# fires a new workflow run — the built-in token cannot). Never targets @pm or self, so the
|
|
# chain always terminates at a dev. The '🤖' guard on the trigger stops status-comment loops.
|
|
if [ -n "$AGENT_TOKEN" ]; then
|
|
# Only delegate on an explicit "DELEGATE: @<agent>" line — never on a prose mention,
|
|
# so an agent that is asking the maintainer a question does not hand off prematurely.
|
|
target=$(grep -oiE 'DELEGATE:[[:space:]]*@(junior|senior|lead|qa)' /tmp/agent_out.md 2>/dev/null \
|
|
| head -1 | grep -oiE '(junior|senior|lead|qa)' | tr '[:upper:]' '[:lower:]')
|
|
if [ -n "$target" ] && [ "$target" != "$NAME" ]; then
|
|
echo "auto-delegating to @$target"
|
|
curl -sS -X POST -H "Authorization: token $AGENT_TOKEN" -H "Content-Type: application/json" \
|
|
"$API/issues/$NUM/comments" \
|
|
-d "$(jq -nc --arg b "@$target please proceed with issue #$NUM per the plan above (delegated by $NAME)." '{body:$b}')" \
|
|
-w '\ndelegate -> HTTP %{http_code}\n' || true
|
|
else
|
|
echo "no DELEGATE marker — not delegating (agent is asking or finished)"
|
|
fi
|
|
fi
|
|
exit 0
|
|
fi
|
|
|
|
# Scrub the runtime scripts checkout (.agents-workflow) from the tree so it never lands in a
|
|
# commit/PR and never confuses the git ops below (issue #33). The scripts we run live outside the
|
|
# workspace ($SCRIPTS -> runner.temp), so removing this in-tree copy is always safe. Handle every
|
|
# way an agent might have left it: untracked dir, tracked files, or a committed gitlink/submodule.
|
|
if git ls-files --error-unmatch .agents-workflow >/dev/null 2>&1 || \
|
|
[ -n "$(git ls-files .agents-workflow 2>/dev/null)" ]; then
|
|
git rm -r --cached --quiet --ignore-unmatch .agents-workflow 2>/dev/null || true
|
|
fi
|
|
git config -f .gitmodules --remove-section submodule..agents-workflow 2>/dev/null || true
|
|
[ -s .gitmodules ] || rm -f .gitmodules 2>/dev/null || true
|
|
rm -rf .agents-workflow 2>/dev/null || true
|
|
|
|
# The agent may have committed on the starting branch AND/OR created extra
|
|
# ai/issue-N-<slug> branches. Commit any leftover on the current branch, push it, then
|
|
# open a PR for EVERY ai/issue-N* branch that has commits beyond main.
|
|
if [ -n "$(git status --porcelain)" ]; then
|
|
git add -A
|
|
git commit -m "@$NAME: issue #$NUM"
|
|
fi
|
|
git push origin "HEAD:$BRANCH" || true
|
|
git fetch -q origin 2>/dev/null || true
|
|
|
|
prbody=$(printf '%s\n\n---\nResolves #%s · 🤖 @%s' "$prdesc" "$NUM" "$NAME")
|
|
owner=${GITHUB_REPOSITORY%%/*}
|
|
|
|
# Post the agent's activity trail (tool calls + reasoning) inline in the same comment so
|
|
# each run produces exactly ONE comment (issue #38). Computed once here so every dev-agent
|
|
# exit path (no-changes, PR-open-failed, normal) appends it to the single reply comment.
|
|
activity=""
|
|
if [ -s /tmp/activity_log.md ]; then
|
|
entries=$(wc -l < /tmp/activity_log.md 2>/dev/null || echo 0)
|
|
log=$(cat /tmp/activity_log.md)
|
|
activity=$(printf '\n\n<details>\n<summary>🔧 activity — %s tool calls & reasoning</summary>\n\n%s\n\n</details>' "$entries" "$log")
|
|
fi
|
|
|
|
# One PR per run: publish ONLY this run's own branch ($BRANCH), never sibling
|
|
# ai/issue-N-* branches. This removes the multi-PR ambiguity that left the
|
|
# activity log stranded on the triggering issue instead of the PR thread.
|
|
br="$BRANCH"
|
|
ahead=$(git rev-list --count "origin/main..origin/$br" 2>/dev/null || echo 0)
|
|
if [ "${ahead:-0}" -eq 0 ]; then
|
|
# No changes on this branch — a plan / questions / analysis only.
|
|
post "$(printf '🤖 **@%s**\n\n%s%s' "$NAME" "$reply" "$activity")"
|
|
exit 0
|
|
fi
|
|
|
|
# NOTE: Gitea ignores the ?head= filter, so match the head branch client-side.
|
|
resp=$(curl -sS "${hdr[@]}" "$API/pulls?state=open&limit=50" \
|
|
| jq -r --arg br "$br" 'if type=="array" then (map(select(.head.ref==$br)) | .[0] // empty) else empty end' 2>/dev/null)
|
|
url=$(printf '%s' "$resp" | jq -r '.html_url // empty' 2>/dev/null)
|
|
prnum=$(printf '%s' "$resp" | jq -r '.number // empty' 2>/dev/null)
|
|
if [ -z "$url" ]; then
|
|
title="@$NAME: $TITLE"
|
|
resp=$(curl -sS -X POST "${hdr[@]}" "$API/pulls" \
|
|
-d "$(jq -nc --arg t "$title" --arg h "$br" --arg b "$prbody" \
|
|
'{title:$t, head:$h, base:"main", body:$b}')")
|
|
echo "PR create ($br): $resp"
|
|
url=$(printf '%s' "$resp" | jq -r '.html_url // empty' 2>/dev/null)
|
|
prnum=$(printf '%s' "$resp" | jq -r '.number // empty' 2>/dev/null)
|
|
fi
|
|
[ -z "$url" ] && { echo "PR open/lookup failed for $br — posting reply on issue instead"; post "$(printf '🤖 **@%s**\n\n%s%s' "$NAME" "$reply" "$activity")"; exit 0; }
|
|
|
|
# Posts to the PR thread when we have a PR number, else to the origin issue ($NUM).
|
|
prpost() {
|
|
local n="$1"; shift; local t="$NUM"
|
|
[ -n "$n" ] && [ "$n" != "$NUM" ] && t="$n"
|
|
echo "posting to #$t"
|
|
curl -sS -w 'comment -> HTTP %{http_code}\n' -X POST "${hdr[@]}" \
|
|
"$API/issues/$t/comments" -d "$(jq -nc --arg b "$1" '{body:$b}')"
|
|
}
|
|
|
|
if [ "$NEW" = "true" ]; then
|
|
prpost "$prnum" "$(printf '🤖 **@%s** — ✅ PR ready for review — @ffaerber please review & merge:\n- %s%s' "$NAME" "$url" "$activity")"
|
|
else
|
|
# Resume: just link the PR — its body and the diff already carry the description, so we don't
|
|
# repeat the full write-up in the comment (the reasoning trail below shows what this run did).
|
|
prpost "$prnum" "$(printf '🤖 **@%s** — pushed an update to the PR:\n- %s%s' "$NAME" "$url" "$activity")"
|
|
fi
|