@@ -343,3 +343,208 @@ jobs:
343343 name : pr-review-feedback
344344 path : feedback/
345345 retention-days : 90
346+
347+ # ==========================================================================
348+ # REPLY TO FEEDBACK
349+ # Responds directly in the PR review thread when a user replies to an agent
350+ # comment. Runs in parallel with capture-feedback — this job handles the
351+ # synchronous conversational reply, while capture-feedback saves the artifact
352+ # for async learning on the next review run (resilient fallback).
353+ # ==========================================================================
354+ reply-to-feedback :
355+ if : |
356+ github.event_name == 'pull_request_review_comment' &&
357+ github.event.comment.in_reply_to_id &&
358+ github.event.comment.user.type != 'Bot'
359+ runs-on : ubuntu-latest
360+ env :
361+ HAS_APP_SECRETS : ${{ secrets.CAGENT_REVIEWER_APP_ID != '' }}
362+
363+ steps :
364+ - name : Check if reply is to agent comment
365+ id : check
366+ shell : bash
367+ env :
368+ GH_TOKEN : ${{ github.token }}
369+ PARENT_ID : ${{ github.event.comment.in_reply_to_id }}
370+ REPO : ${{ github.repository }}
371+ run : |
372+ if [ -z "$PARENT_ID" ]; then
373+ echo "is_agent=false" >> $GITHUB_OUTPUT
374+ echo "⏭️ Not a reply comment, skipping"
375+ exit 0
376+ fi
377+
378+ parent=$(gh api "repos/$REPO/pulls/comments/$PARENT_ID") || {
379+ echo "::warning::Failed to fetch parent comment $PARENT_ID" >&2
380+ echo "is_agent=false" >> $GITHUB_OUTPUT
381+ exit 0
382+ }
383+ # Validate required fields exist before extracting
384+ if ! echo "$parent" | jq -e '.user.type and .body' > /dev/null 2>&1; then
385+ echo "::warning::Parent comment has unexpected structure" >&2
386+ echo "is_agent=false" >> $GITHUB_OUTPUT
387+ exit 0
388+ fi
389+ body=$(echo "$parent" | jq -r '.body')
390+ parent_user_type=$(echo "$parent" | jq -r '.user.type')
391+
392+ # Defense-in-depth: verify the root comment was posted by a Bot (agent) AND
393+ # contains the review marker but NOT the reply marker (substring overlap).
394+ # The user.type check prevents matching human comments that happen to contain
395+ # the marker text (e.g., in discussions about the review system).
396+ if [ "$parent_user_type" = "Bot" ] && \
397+ echo "$body" | grep -q "<!-- cagent-review -->" && \
398+ ! echo "$body" | grep -q "<!-- cagent-review-reply -->"; then
399+ echo "is_agent=true" >> $GITHUB_OUTPUT
400+ echo "root_comment_id=$PARENT_ID" >> $GITHUB_OUTPUT
401+
402+ # Extract file path and line from the root comment for context
403+ echo "file_path=$(echo "$parent" | jq -r '.path // ""')" >> $GITHUB_OUTPUT
404+ echo "line=$(echo "$parent" | jq -r '.line // .original_line // ""')" >> $GITHUB_OUTPUT
405+ echo "✅ Reply is to an agent review comment"
406+ else
407+ echo "is_agent=false" >> $GITHUB_OUTPUT
408+ echo "⏭️ Not a reply to agent comment, skipping"
409+ fi
410+
411+ - name : Check authorization
412+ if : steps.check.outputs.is_agent == 'true'
413+ id : auth
414+ shell : bash
415+ env :
416+ AUTHOR_ASSOCIATION : ${{ github.event.comment.author_association }}
417+ run : |
418+ case "$AUTHOR_ASSOCIATION" in
419+ OWNER|MEMBER|COLLABORATOR)
420+ echo "authorized=true" >> $GITHUB_OUTPUT
421+ echo "✅ Author is $AUTHOR_ASSOCIATION — authorized to trigger reply"
422+ ;;
423+ *)
424+ echo "authorized=false" >> $GITHUB_OUTPUT
425+ echo "⏭️ Author is $AUTHOR_ASSOCIATION — not authorized for reply"
426+ ;;
427+ esac
428+
429+ - name : Build thread context
430+ if : steps.check.outputs.is_agent == 'true' && steps.auth.outputs.authorized == 'true'
431+ id : thread
432+ shell : bash
433+ env :
434+ GH_TOKEN : ${{ github.token }}
435+ ROOT_ID : ${{ steps.check.outputs.root_comment_id }}
436+ PR_NUMBER : ${{ github.event.pull_request.number }}
437+ REPO : ${{ github.repository }}
438+ FILE_PATH : ${{ steps.check.outputs.file_path }}
439+ LINE : ${{ steps.check.outputs.line }}
440+ # The triggering comment from the webhook payload — guaranteed fresh,
441+ # unlike the API which may have eventual consistency lag.
442+ TRIGGER_COMMENT_BODY : ${{ github.event.comment.body }}
443+ TRIGGER_COMMENT_AUTHOR : ${{ github.event.comment.user.login }}
444+ TRIGGER_COMMENT_ID : ${{ github.event.comment.id }}
445+ run : |
446+ # Fetch the root comment (fail early if the API call errors)
447+ root=$(gh api "repos/$REPO/pulls/comments/$ROOT_ID") || {
448+ echo "::error::Failed to fetch root comment $ROOT_ID" >&2
449+ exit 1
450+ }
451+ root_body=$(echo "$root" | jq -r '.body // ""')
452+
453+ # Fetch all review comments on this PR and filter to this thread.
454+ # Uses --paginate to handle PRs with >100 review comments.
455+ # Each page is processed by jq independently, then merged with jq -s.
456+ # Note: the triggering comment may not appear here due to eventual
457+ # consistency, so we append it from the webhook payload below.
458+ all_comments=$(gh api --paginate "repos/$REPO/pulls/$PR_NUMBER/comments" \
459+ --jq "[.[] | select(.in_reply_to_id == $ROOT_ID)]" | jq -s 'add // [] | sort_by(.created_at)') || {
460+ echo "::error::Failed to fetch thread comments for PR $PR_NUMBER" >&2
461+ exit 1
462+ }
463+
464+ # Build the thread context and save as step output.
465+ # Use a randomized delimiter to prevent comment body content from
466+ # colliding with the GITHUB_OUTPUT heredoc terminator.
467+ DELIM="THREAD_CONTEXT_$(openssl rand -hex 8)"
468+
469+ {
470+ echo "prompt<<$DELIM"
471+ echo "A developer replied to your review comment. Read the thread context below and respond"
472+ echo "in the same thread."
473+ echo ""
474+ echo "---"
475+ echo "REPO=$REPO"
476+ echo "PR_NUMBER=$PR_NUMBER"
477+ echo "ROOT_COMMENT_ID=$ROOT_ID"
478+ echo "FILE_PATH=$FILE_PATH"
479+ echo "LINE=$LINE"
480+ echo ""
481+ echo "[ORIGINAL REVIEW COMMENT]"
482+ echo "$root_body"
483+ echo ""
484+
485+ # Add earlier replies from the API (excludes the triggering comment
486+ # to avoid duplication if the API already has it)
487+ reply_count=$(echo "$all_comments" | jq 'length')
488+ if [ "$reply_count" -gt 0 ]; then
489+ for i in $(seq 0 $((reply_count - 1))); do
490+ comment_id=$(echo "$all_comments" | jq -r ".[$i].id") || continue
491+ # Skip the triggering comment — we append it from the payload below
492+ if [ "$comment_id" = "$TRIGGER_COMMENT_ID" ]; then
493+ continue
494+ fi
495+ # Skip bot replies to avoid the agent responding to its own previous replies
496+ user_type=$(echo "$all_comments" | jq -r ".[$i].user.type") || continue
497+ if [ "$user_type" = "Bot" ]; then
498+ continue
499+ fi
500+ author=$(echo "$all_comments" | jq -r ".[$i].user.login") || continue
501+ body=$(echo "$all_comments" | jq -r ".[$i].body") || continue
502+ echo "[REPLY by @$author]"
503+ echo "$body"
504+ echo ""
505+ done
506+ fi
507+
508+ # Always append the triggering comment last — sourced directly from
509+ # the webhook payload so it's guaranteed to be present.
510+ echo "[REPLY by @$TRIGGER_COMMENT_AUTHOR] ← this is the reply you are responding to"
511+ echo "$TRIGGER_COMMENT_BODY"
512+ echo ""
513+ echo "$DELIM"
514+ } >> $GITHUB_OUTPUT
515+
516+ echo "✅ Built thread context with replies (triggering comment from webhook payload)"
517+
518+ # Safe to checkout PR head because the reply agent only READS files (no code execution)
519+ - name : Checkout PR head
520+ if : steps.check.outputs.is_agent == 'true' && steps.auth.outputs.authorized == 'true'
521+ uses : actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
522+ with :
523+ fetch-depth : 0
524+ ref : refs/pull/${{ github.event.pull_request.number }}/head
525+
526+ # Generate GitHub App token for custom app identity (optional - falls back to github.token)
527+ - name : Generate GitHub App token
528+ if : steps.check.outputs.is_agent == 'true' && steps.auth.outputs.authorized == 'true' && env.HAS_APP_SECRETS == 'true'
529+ id : app-token
530+ continue-on-error : true
531+ uses : tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # v2
532+ with :
533+ app_id : ${{ secrets.CAGENT_REVIEWER_APP_ID }}
534+ private_key : ${{ secrets.CAGENT_REVIEWER_APP_PRIVATE_KEY }}
535+
536+ - name : Run reply
537+ if : steps.check.outputs.is_agent == 'true' && steps.auth.outputs.authorized == 'true'
538+ continue-on-error : true
539+ uses : docker/cagent-action/review-pr/reply@latest
540+ with :
541+ thread-context : ${{ steps.thread.outputs.prompt }}
542+ comment-id : ${{ github.event.comment.id }}
543+ anthropic-api-key : ${{ secrets.ANTHROPIC_API_KEY }}
544+ openai-api-key : ${{ secrets.OPENAI_API_KEY }}
545+ google-api-key : ${{ secrets.GOOGLE_API_KEY }}
546+ aws-bearer-token-bedrock : ${{ secrets.AWS_BEARER_TOKEN_BEDROCK }}
547+ xai-api-key : ${{ secrets.XAI_API_KEY }}
548+ nebius-api-key : ${{ secrets.NEBIUS_API_KEY }}
549+ mistral-api-key : ${{ secrets.MISTRAL_API_KEY }}
550+ github-token : ${{ steps.app-token.outputs.token || github.token }}
0 commit comments