-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpr-loop-stop-hook.sh
More file actions
executable file
·1649 lines (1378 loc) · 68.4 KB
/
pr-loop-stop-hook.sh
File metadata and controls
executable file
·1649 lines (1378 loc) · 68.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Stop Hook for PR loop
#
# Intercepts Claude's exit attempts, polls for remote bot reviews,
# and uses local Codex to validate if bot concerns are addressed.
#
# Key features:
# - Polls until ALL active bots respond (per-bot tracking with 15min timeout each)
# - Checks PR state before polling (detects CLOSED/MERGED)
# - Uses APPROVE marker for Codex approval
# - Updates active_bots list based on per-bot approval
#
# State directory: .humanize/pr-loop/<timestamp>/
# State file: state.md (current_round, pr_number, active_bots as YAML list, etc.)
# Resolve file: round-N-pr-resolve.md (Claude's resolution summary)
# Comment file: round-N-pr-comment.md (Fetched PR comments)
# Check file: round-N-pr-check.md (Local Codex validation)
# Feedback file: round-N-pr-feedback.md (Feedback for next round)
#
set -euo pipefail
# ========================================
# Default Configuration
# ========================================
# Override effort before sourcing loop-common.sh (PR loop defaults to medium effort).
# codex_model is NOT pre-set here so that config-backed values from loop-common.sh apply.
DEFAULT_CODEX_EFFORT="medium"
DEFAULT_CODEX_TIMEOUT=900
DEFAULT_POLL_INTERVAL=30
DEFAULT_POLL_TIMEOUT=900 # 15 minutes per bot
# Note: Bot name mapping functions (map_bot_to_author, map_author_to_bot)
# and helper functions (build_yaml_list, build_bot_mention_string) are
# provided by loop-common.sh which is sourced below.
# ========================================
# Read Hook Input
# ========================================
HOOK_INPUT=$(cat)
# ========================================
# Find Active Loop
# ========================================
PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$(pwd)}"
LOOP_BASE_DIR="$PROJECT_ROOT/.humanize/pr-loop"
# Source shared loop functions
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
source "$SCRIPT_DIR/lib/loop-common.sh"
# Source portable timeout wrapper
PLUGIN_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
TEMPLATE_DIR="$PLUGIN_ROOT/prompt-template"
source "$PLUGIN_ROOT/scripts/portable-timeout.sh"
# Default timeout for git/gh operations
GIT_TIMEOUT=30
GH_TIMEOUT=60
# Use shared find_active_pr_loop function from loop-common.sh
LOOP_DIR=$(find_active_pr_loop "$LOOP_BASE_DIR")
# If no active PR loop, let other hooks handle
if [[ -z "$LOOP_DIR" ]]; then
exit 0
fi
STATE_FILE="$LOOP_DIR/state.md"
if [[ ! -f "$STATE_FILE" ]]; then
exit 0
fi
# ========================================
# Parse State File (YAML list format for active_bots)
# ========================================
# Declare arrays outside function for macOS Bash 3.2 compatibility
# (declare -g requires Bash 4.2+, which macOS doesn't have by default)
PR_CONFIGURED_BOTS_ARRAY=()
PR_ACTIVE_BOTS_ARRAY=()
parse_pr_loop_state() {
local state_file="$1"
STATE_FRONTMATTER=$(sed -n '/^---$/,/^---$/{ /^---$/d; p; }' "$state_file" 2>/dev/null || echo "")
PR_CURRENT_ROUND=$(echo "$STATE_FRONTMATTER" | grep "^current_round:" | sed "s/current_round: *//" | tr -d ' ' || true)
PR_MAX_ITERATIONS=$(echo "$STATE_FRONTMATTER" | grep "^max_iterations:" | sed "s/max_iterations: *//" | tr -d ' ' || true)
PR_NUMBER=$(echo "$STATE_FRONTMATTER" | grep "^pr_number:" | sed "s/pr_number: *//" | tr -d ' ' || true)
PR_START_BRANCH=$(echo "$STATE_FRONTMATTER" | grep "^start_branch:" | sed "s/start_branch: *//; s/^\"//; s/\"\$//" || true)
PR_CODEX_MODEL=$(echo "$STATE_FRONTMATTER" | grep "^codex_model:" | sed "s/codex_model: *//" | tr -d ' ' || true)
PR_CODEX_EFFORT=$(echo "$STATE_FRONTMATTER" | grep "^codex_effort:" | sed "s/codex_effort: *//" | tr -d ' ' || true)
PR_CODEX_TIMEOUT=$(echo "$STATE_FRONTMATTER" | grep "^codex_timeout:" | sed "s/codex_timeout: *//" | tr -d ' ' || true)
PR_POLL_INTERVAL=$(echo "$STATE_FRONTMATTER" | grep "^poll_interval:" | sed "s/poll_interval: *//" | tr -d ' ' || true)
PR_POLL_TIMEOUT=$(echo "$STATE_FRONTMATTER" | grep "^poll_timeout:" | sed "s/poll_timeout: *//" | tr -d ' ' || true)
PR_STARTED_AT=$(echo "$STATE_FRONTMATTER" | grep "^started_at:" | sed "s/started_at: *//" || true)
PR_LAST_TRIGGER_AT=$(echo "$STATE_FRONTMATTER" | grep "^last_trigger_at:" | sed "s/last_trigger_at: *//" || true)
# New state fields for Cases 1-5 and force push detection
PR_STARTUP_CASE=$(echo "$STATE_FRONTMATTER" | grep "^startup_case:" | sed "s/startup_case: *//" | tr -d ' ' || true)
PR_LATEST_COMMIT_SHA=$(echo "$STATE_FRONTMATTER" | grep "^latest_commit_sha:" | sed "s/latest_commit_sha: *//" | tr -d ' ' || true)
PR_LATEST_COMMIT_AT=$(echo "$STATE_FRONTMATTER" | grep "^latest_commit_at:" | sed "s/latest_commit_at: *//" || true)
PR_TRIGGER_COMMENT_ID=$(echo "$STATE_FRONTMATTER" | grep "^trigger_comment_id:" | sed "s/trigger_comment_id: *//" | tr -d ' ' || true)
# Parse configured_bots and active_bots as YAML lists
# configured_bots: never changes, used for polling all bots (allows re-add)
# active_bots: current bots with issues, shrinks as bots approve
# Arrays are declared outside function for macOS Bash 3.2 compatibility
PR_CONFIGURED_BOTS_ARRAY=()
PR_ACTIVE_BOTS_ARRAY=()
# Parse YAML list helper function
# NOTE: Avoids 'local -n' (nameref) which requires Bash 4.3+ and fails on macOS Bash 3.2
# Instead, outputs values to stdout and caller captures into array
parse_yaml_list() {
local field_name="$1"
local in_field=false
while IFS= read -r line; do
if [[ "$line" =~ ^${field_name}: ]]; then
in_field=true
# Check if it's inline format: field: value
local inline_value="${line#*: }"
if [[ -n "$inline_value" && "$inline_value" != "${field_name}:" ]]; then
# Old comma-separated format for backwards compatibility
echo "$inline_value" | tr ',' '\n' | tr -d ' '
in_field=false
fi
continue
fi
if [[ "$in_field" == "true" ]]; then
if [[ "$line" =~ ^[[:space:]]+-[[:space:]]+ ]]; then
# Extract bot name from " - botname"
local bot_name="${line#*- }"
bot_name=$(echo "$bot_name" | tr -d ' ')
if [[ -n "$bot_name" ]]; then
echo "$bot_name"
fi
elif [[ "$line" =~ ^[a-zA-Z_] ]]; then
# New field started, stop parsing
in_field=false
fi
fi
done <<< "$STATE_FRONTMATTER"
}
# Read parsed values into arrays (macOS Bash 3.2 compatible)
while IFS= read -r bot; do
[[ -n "$bot" ]] && PR_CONFIGURED_BOTS_ARRAY+=("$bot")
done < <(parse_yaml_list "configured_bots")
while IFS= read -r bot; do
[[ -n "$bot" ]] && PR_ACTIVE_BOTS_ARRAY+=("$bot")
done < <(parse_yaml_list "active_bots")
# Backwards compatibility: if configured_bots is empty, use active_bots
if [[ ${#PR_CONFIGURED_BOTS_ARRAY[@]} -eq 0 ]]; then
PR_CONFIGURED_BOTS_ARRAY=("${PR_ACTIVE_BOTS_ARRAY[@]}")
fi
# Apply defaults
PR_CURRENT_ROUND="${PR_CURRENT_ROUND:-0}"
PR_MAX_ITERATIONS="${PR_MAX_ITERATIONS:-42}"
PR_CODEX_MODEL="${PR_CODEX_MODEL:-$DEFAULT_CODEX_MODEL}"
PR_CODEX_EFFORT="${PR_CODEX_EFFORT:-$DEFAULT_CODEX_EFFORT}"
PR_CODEX_TIMEOUT="${PR_CODEX_TIMEOUT:-$DEFAULT_CODEX_TIMEOUT}"
PR_POLL_INTERVAL="${PR_POLL_INTERVAL:-$DEFAULT_POLL_INTERVAL}"
PR_POLL_TIMEOUT="${PR_POLL_TIMEOUT:-$DEFAULT_POLL_TIMEOUT}"
}
parse_pr_loop_state "$STATE_FILE"
# Build display string and mention string from active bots array
PR_ACTIVE_BOTS_DISPLAY=$(IFS=', '; echo "${PR_ACTIVE_BOTS_ARRAY[*]}")
PR_CONFIGURED_BOTS_DISPLAY=$(IFS=', '; echo "${PR_CONFIGURED_BOTS_ARRAY[*]}")
# Build mention string from configured bots (for detecting trigger comments)
PR_BOT_MENTION_STRING=$(build_bot_mention_string "${PR_CONFIGURED_BOTS_ARRAY[@]}")
# Validate required fields
if [[ -z "$PR_NUMBER" ]]; then
echo "Error: PR number not found in state file" >&2
exit 0
fi
if [[ ! "$PR_CURRENT_ROUND" =~ ^[0-9]+$ ]]; then
echo "Warning: Invalid current_round in state file" >&2
exit 0
fi
# ========================================
# Resolve PR Base Repository (for fork PRs)
# ========================================
# IMPORTANT: For fork PRs, comments are on the base repository, not the fork.
# gh pr view without --repo fails in forks because the PR number doesn't exist there.
# Strategy: First get current repo, check if PR exists there, then try parent repo for forks.
# NOTE: This MUST be done BEFORE PR state checks, which also need --repo for forks.
# Step 1: Get the current repo (works in both forks and base repos)
CURRENT_REPO=$(run_with_timeout "$GH_TIMEOUT" gh repo view --json owner,name \
-q '.owner.login + "/" + .name' 2>/dev/null) || CURRENT_REPO=""
# Step 2: Determine the correct repo for PR operations
# Try current repo first - if PR exists there, use it
PR_BASE_REPO=""
PR_LOOKUP_REPO="" # Repo where PR was found (for subsequent lookups)
if [[ -n "$CURRENT_REPO" ]]; then
if run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$CURRENT_REPO" --json number -q .number >/dev/null 2>&1; then
PR_BASE_REPO="$CURRENT_REPO"
PR_LOOKUP_REPO="$CURRENT_REPO"
fi
fi
if [[ -z "$PR_BASE_REPO" ]]; then
# PR not found in current repo - check if this is a fork and try parent repo
PARENT_REPO=$(run_with_timeout "$GH_TIMEOUT" gh repo view --json parent \
-q '.parent.owner.login + "/" + .parent.name' 2>/dev/null) || PARENT_REPO=""
if [[ -n "$PARENT_REPO" && "$PARENT_REPO" != "null/" && "$PARENT_REPO" != "/" ]]; then
if run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$PARENT_REPO" --json number -q .number >/dev/null 2>&1; then
PR_BASE_REPO="$PARENT_REPO"
PR_LOOKUP_REPO="$PARENT_REPO"
fi
fi
fi
if [[ -z "$PR_BASE_REPO" ]]; then
echo "Warning: Could not resolve PR base repository, using current repo" >&2
PR_BASE_REPO="$CURRENT_REPO"
PR_LOOKUP_REPO="$CURRENT_REPO"
fi
# ========================================
# Check PR State (detect CLOSED/MERGED before polling)
# ========================================
# NOTE: Uses PR_LOOKUP_REPO (resolved above) for fork PR support
PR_STATE=$(run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$PR_LOOKUP_REPO" --json state -q .state 2>/dev/null) || PR_STATE=""
if [[ "$PR_STATE" == "MERGED" ]]; then
echo "PR #$PR_NUMBER has been merged. Marking loop as complete." >&2
mv "$STATE_FILE" "$LOOP_DIR/merged-state.md"
exit 0
fi
if [[ "$PR_STATE" == "CLOSED" ]]; then
echo "PR #$PR_NUMBER has been closed. Marking loop as closed." >&2
mv "$STATE_FILE" "$LOOP_DIR/closed-state.md"
exit 0
fi
# ========================================
# Check Resolution File Exists
# ========================================
RESOLVE_FILE="$LOOP_DIR/round-${PR_CURRENT_ROUND}-pr-resolve.md"
if [[ ! -f "$RESOLVE_FILE" ]]; then
REASON="# Resolution Summary Missing
Please write your resolution summary to: $RESOLVE_FILE
The summary should include:
- Issues addressed
- Files modified
- Tests added (if any)"
jq -n --arg reason "$REASON" --arg msg "PR Loop: Resolution summary missing for round $PR_CURRENT_ROUND" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
# ========================================
# Check Git Status
# ========================================
if command -v git &>/dev/null && run_with_timeout "$GIT_TIMEOUT" git rev-parse --git-dir &>/dev/null 2>&1; then
GIT_STATUS_CACHED=$(run_with_timeout "$GIT_TIMEOUT" git status --porcelain 2>/dev/null) || GIT_EXIT=$?
GIT_EXIT=${GIT_EXIT:-0}
if [[ $GIT_EXIT -ne 0 ]]; then
REASON="# Git Status Failed
Git status operation failed. Please check your repository state and try again."
jq -n --arg reason "$REASON" --arg msg "PR Loop: Git status failed" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
# Filter out .humanize from status check
NON_HUMANIZE_STATUS=$(echo "$GIT_STATUS_CACHED" | grep -v '\.humanize' || true)
if [[ -n "$NON_HUMANIZE_STATUS" ]]; then
REASON="# Git Not Clean
You have uncommitted changes. Please commit all changes before exiting.
Changes detected:
\`\`\`
$NON_HUMANIZE_STATUS
\`\`\`"
jq -n --arg reason "$REASON" --arg msg "PR Loop: Uncommitted changes detected" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
# Step 6: Check for unpushed commits (PR loop always requires push)
CURRENT_BRANCH=$(git branch --show-current 2>/dev/null || echo "main")
AHEAD_COUNT=0
LOCAL_HEAD=$(git rev-parse HEAD 2>/dev/null) || LOCAL_HEAD=""
# First try: git status -sb works when upstream is configured
GIT_AHEAD=$(run_with_timeout "$GIT_TIMEOUT" git status -sb 2>/dev/null | grep -o 'ahead [0-9]*' || true)
if [[ -n "$GIT_AHEAD" ]]; then
AHEAD_COUNT=$(echo "$GIT_AHEAD" | grep -o '[0-9]*')
else
# Fallback: Check if upstream exists, if not compare with origin/branch or PR head
if ! git rev-parse --abbrev-ref '@{u}' >/dev/null 2>&1; then
# No upstream configured - try origin/branch first
REMOTE_HEAD=$(git rev-parse "origin/$CURRENT_BRANCH" 2>/dev/null) || REMOTE_HEAD=""
if [[ -n "$LOCAL_HEAD" && -n "$REMOTE_HEAD" && "$LOCAL_HEAD" != "$REMOTE_HEAD" ]]; then
# Count commits ahead of remote
AHEAD_COUNT=$(git rev-list --count "origin/$CURRENT_BRANCH..HEAD" 2>/dev/null) || AHEAD_COUNT=0
elif [[ -z "$REMOTE_HEAD" && -n "$PR_NUMBER" ]]; then
# No origin/branch exists - compare with PR's headRefOid from GitHub
# This handles cases where branch was never pushed or remote ref is missing
# NOTE: Use --repo for fork PR support (PR_BASE_REPO resolved earlier)
PR_HEAD_SHA=$(run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$PR_BASE_REPO" --json headRefOid -q '.headRefOid' 2>/dev/null) || PR_HEAD_SHA=""
if [[ -z "$PR_HEAD_SHA" ]]; then
# Failed to get PR head - fail closed (assume unpushed) for safety
echo "Warning: Could not fetch PR head SHA, assuming unpushed commits" >&2
AHEAD_COUNT=1
elif [[ -n "$LOCAL_HEAD" && "$LOCAL_HEAD" != "$PR_HEAD_SHA" ]]; then
# Local differs from PR head - count commits since PR head
AHEAD_COUNT=$(git rev-list --count "$PR_HEAD_SHA..HEAD" 2>/dev/null) || {
# PR head not in local history (force push?) - treat as 1 unpushed
AHEAD_COUNT=1
}
fi
fi
fi
fi
if [[ "$AHEAD_COUNT" -gt 0 ]]; then
FALLBACK_MSG="# Unpushed Commits Detected
You have $AHEAD_COUNT unpushed commit(s). PR loop requires pushing changes so bots can review them.
Please push: git push origin $CURRENT_BRANCH"
REASON=$(load_and_render_safe "$TEMPLATE_DIR" "block/unpushed-commits.md" "$FALLBACK_MSG" \
"AHEAD_COUNT=$AHEAD_COUNT" "CURRENT_BRANCH=$CURRENT_BRANCH")
jq -n --arg reason "$REASON" --arg msg "PR Loop: $AHEAD_COUNT unpushed commit(s)" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
fi
# ========================================
# Force Push Detection
# ========================================
# Detect if the remote branch HEAD has changed in a way that indicates force push
# This happens when previous commits are no longer reachable from current HEAD
if [[ -n "$PR_LATEST_COMMIT_SHA" ]]; then
CURRENT_HEAD=$(run_with_timeout "$GIT_TIMEOUT" git rev-parse HEAD 2>/dev/null) || CURRENT_HEAD=""
# Check if the stored commit SHA is still reachable from current HEAD
# If not, a force push (history rewrite) has occurred
if [[ -n "$CURRENT_HEAD" && "$CURRENT_HEAD" != "$PR_LATEST_COMMIT_SHA" ]]; then
# Check if old commit is ancestor of current HEAD
IS_ANCESTOR=$(run_with_timeout "$GIT_TIMEOUT" git merge-base --is-ancestor "$PR_LATEST_COMMIT_SHA" "$CURRENT_HEAD" 2>/dev/null && echo "yes" || echo "no")
if [[ "$IS_ANCESTOR" == "no" ]]; then
echo "Force push detected: $PR_LATEST_COMMIT_SHA is no longer reachable from $CURRENT_HEAD" >&2
# Preserve OLD commit SHA before updating state
OLD_COMMIT_SHA="$PR_LATEST_COMMIT_SHA"
# Get the timestamp of the new HEAD commit for trigger validation
# This ensures detect_trigger_comment only accepts comments AFTER the force push
# NOTE: Uses PR_LOOKUP_REPO for fork PR support
NEW_HEAD_COMMIT_AT=$(run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$PR_LOOKUP_REPO" --json commits \
--jq '.commits | sort_by(.committedDate) | last | .committedDate' 2>/dev/null) || NEW_HEAD_COMMIT_AT=""
if [[ -z "$NEW_HEAD_COMMIT_AT" ]]; then
# Fallback: use current timestamp
NEW_HEAD_COMMIT_AT=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
fi
# Update state file with new commit SHA/timestamp and clear trigger state
# Clear BOTH last_trigger_at AND trigger_comment_id to prevent stale eyes checks
TEMP_FILE="${STATE_FILE}.forcepush.$$"
sed -e "s/^latest_commit_sha:.*/latest_commit_sha: $CURRENT_HEAD/" \
-e "s/^latest_commit_at:.*/latest_commit_at: $NEW_HEAD_COMMIT_AT/" \
-e "s/^last_trigger_at:.*/last_trigger_at:/" \
-e "s/^trigger_comment_id:.*/trigger_comment_id:/" \
"$STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$STATE_FILE"
# Update local variables to reflect the change
PR_LATEST_COMMIT_SHA="$CURRENT_HEAD"
PR_LATEST_COMMIT_AT="$NEW_HEAD_COMMIT_AT"
PR_LAST_TRIGGER_AT=""
PR_TRIGGER_COMMENT_ID=""
FALLBACK_MSG="# Force Push Detected
A force push (history rewrite) has been detected. Post a new @bot trigger comment: $PR_BOT_MENTION_STRING"
REASON=$(load_and_render_safe "$TEMPLATE_DIR" "block/force-push-detected.md" "$FALLBACK_MSG" \
"OLD_COMMIT=$OLD_COMMIT_SHA" "NEW_COMMIT=$CURRENT_HEAD" "BOT_MENTION_STRING=$PR_BOT_MENTION_STRING" \
"PR_NUMBER=$PR_NUMBER")
jq -n --arg reason "$REASON" --arg msg "PR Loop: Force push detected - please re-trigger bots" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
fi
fi
# ========================================
# Check Max Iterations
# ========================================
NEXT_ROUND=$((PR_CURRENT_ROUND + 1))
if [[ $NEXT_ROUND -gt $PR_MAX_ITERATIONS ]]; then
echo "PR loop reached max iterations ($PR_MAX_ITERATIONS). Exiting." >&2
mv "$STATE_FILE" "$LOOP_DIR/maxiter-state.md"
exit 0
fi
# ========================================
# Check if Active Bots Remain
# ========================================
# NOTE: Step 8 (Codex +1 check) has been moved to after trigger detection
# to ensure it uses the correct timestamp that accounts for new commits.
if [[ ${#PR_ACTIVE_BOTS_ARRAY[@]} -eq 0 ]]; then
echo "All bots have approved. PR loop complete!" >&2
mv "$STATE_FILE" "$LOOP_DIR/approve-state.md"
exit 0
fi
# ========================================
# Detect Trigger Comment and Update last_trigger_at
# ========================================
# Get current GitHub user login for trigger comment filtering
get_current_user() {
run_with_timeout "$GH_TIMEOUT" gh api user --jq '.login' 2>/dev/null || echo ""
}
# Find the most recent PR comment from CURRENT USER that contains bot mentions
# Returns: "timestamp|comment_id" on success
# This timestamp is used for --after filtering to catch fast bot replies
# NOTE: Uses --paginate to handle PRs with >30 comments
# IMPORTANT: If latest_commit_at is set, only accepts comments AFTER that timestamp
# This prevents old triggers from being re-used after force push
# IMPORTANT: Uses PR_BASE_REPO (not {owner}/{repo}) for fork PR support
detect_trigger_comment() {
local pr_num="$1"
local current_user="$2"
local after_timestamp="${3:-}" # Optional: only accept comments after this timestamp
# Fetch ALL issue comments on the PR (paginated to handle >30 comments)
# Using --paginate ensures we don't miss the latest @mention on large PRs
# IMPORTANT: --jq with --paginate runs per-page, so we output objects (not array)
# and use jq -s to aggregate all pages into a single array before filtering
# IMPORTANT: Use PR_BASE_REPO for fork PRs - comments are on base repo, not fork
local comments_json
comments_json=$(run_with_timeout "$GH_TIMEOUT" gh api "repos/$PR_BASE_REPO/issues/$pr_num/comments" \
--paginate --jq '.[] | {id: .id, author: .user.login, created_at: .created_at, body: .body}' 2>/dev/null \
| jq -s '.') || return 1
if [[ -z "$comments_json" || "$comments_json" == "[]" ]]; then
return 1
fi
# Build pattern to match any @bot mention
local bot_pattern=""
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
if [[ -n "$bot_pattern" ]]; then
bot_pattern="${bot_pattern}|@${bot}"
else
bot_pattern="@${bot}"
fi
done
# Find most recent trigger comment from CURRENT USER (sorted by created_at descending)
# comments_json is already aggregated from all pages into a single array
# If after_timestamp is set, only accept comments created after that timestamp
# Returns both timestamp and comment ID
local trigger_info
if [[ -n "$after_timestamp" ]]; then
# Filter to only comments AFTER the specified timestamp (force push protection)
trigger_info=$(echo "$comments_json" | jq -r \
--arg pattern "$bot_pattern" \
--arg user "$current_user" \
--arg after "$after_timestamp" '
[.[] | select(
.author == $user and
(.body | test($pattern; "i")) and
(.created_at >= $after)
)] |
sort_by(.created_at) | reverse | .[0] | "\(.created_at)|\(.id)" // empty
')
else
trigger_info=$(echo "$comments_json" | jq -r --arg pattern "$bot_pattern" --arg user "$current_user" '
[.[] | select(.author == $user and (.body | test($pattern; "i")))] |
sort_by(.created_at) | reverse | .[0] | "\(.created_at)|\(.id)" // empty
')
fi
if [[ -n "$trigger_info" && "$trigger_info" != "null|null" && "$trigger_info" != "|" ]]; then
echo "$trigger_info"
return 0
fi
return 1
}
# Get current user for trigger comment filtering
CURRENT_USER=$(get_current_user)
if [[ -z "$CURRENT_USER" ]]; then
echo "Warning: Could not determine current GitHub user" >&2
fi
# ========================================
# Refresh latest_commit_at from PR Before Trigger Detection
# ========================================
# Ensure trigger validation uses the CURRENT latest commit timestamp,
# not a stale value from state. This prevents old triggers from being accepted
# after new (non-force) commits are pushed.
# NOTE: Uses PR_LOOKUP_REPO for fork PR support
CURRENT_LATEST_COMMIT_AT=$(run_with_timeout "$GH_TIMEOUT" gh pr view "$PR_NUMBER" --repo "$PR_LOOKUP_REPO" --json commits \
--jq '.commits | sort_by(.committedDate) | last | .committedDate' 2>/dev/null) || CURRENT_LATEST_COMMIT_AT=""
# Track if new commits were detected (used to override REQUIRE_TRIGGER for cases 2/3)
NEW_COMMITS_DETECTED=false
if [[ -n "$CURRENT_LATEST_COMMIT_AT" && "$CURRENT_LATEST_COMMIT_AT" != "$PR_LATEST_COMMIT_AT" ]]; then
echo "Updating latest_commit_at: $PR_LATEST_COMMIT_AT -> $CURRENT_LATEST_COMMIT_AT" >&2
echo " Clearing stale trigger fields (new commits require new @bot mention)" >&2
# Persist to state file and clear trigger fields to prevent stale polling
# New commits mean old trigger is invalid - user must post new @bot comment
TEMP_FILE="${STATE_FILE}.commitrefresh.$$"
sed -e "s/^latest_commit_at:.*/latest_commit_at: $CURRENT_LATEST_COMMIT_AT/" \
-e "s/^last_trigger_at:.*/last_trigger_at:/" \
-e "s/^trigger_comment_id:.*/trigger_comment_id:/" \
"$STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$STATE_FILE"
PR_LATEST_COMMIT_AT="$CURRENT_LATEST_COMMIT_AT"
PR_LAST_TRIGGER_AT=""
PR_TRIGGER_COMMENT_ID=""
NEW_COMMITS_DETECTED=true
fi
# ALWAYS check for newer trigger comments and update last_trigger_at
# This ensures we use the most recent trigger, not a stale one
# IMPORTANT: Pass latest_commit_at to filter out old triggers (force push protection)
# After a force push, we need a NEW trigger comment, not one from before the push
echo "Detecting trigger comment timestamp from user '$CURRENT_USER'..." >&2
if [[ -n "$PR_LATEST_COMMIT_AT" ]]; then
echo " (Filtering for comments after: $PR_LATEST_COMMIT_AT)" >&2
fi
DETECTED_TRIGGER_INFO=$(detect_trigger_comment "$PR_NUMBER" "$CURRENT_USER" "$PR_LATEST_COMMIT_AT") || true
DETECTED_TRIGGER_AT=""
DETECTED_TRIGGER_COMMENT_ID=""
if [[ -n "$DETECTED_TRIGGER_INFO" ]]; then
# Parse timestamp and comment ID from "timestamp|id" format
DETECTED_TRIGGER_AT="${DETECTED_TRIGGER_INFO%%|*}"
DETECTED_TRIGGER_COMMENT_ID="${DETECTED_TRIGGER_INFO##*|}"
fi
if [[ -n "$DETECTED_TRIGGER_AT" ]]; then
# Check if detected trigger is newer than stored one
if [[ -z "$PR_LAST_TRIGGER_AT" ]] || [[ "$DETECTED_TRIGGER_AT" > "$PR_LAST_TRIGGER_AT" ]]; then
echo "Found trigger comment at: $DETECTED_TRIGGER_AT (ID: $DETECTED_TRIGGER_COMMENT_ID)" >&2
if [[ -n "$PR_LAST_TRIGGER_AT" ]]; then
echo " (Updating from older trigger: $PR_LAST_TRIGGER_AT)" >&2
fi
PR_LAST_TRIGGER_AT="$DETECTED_TRIGGER_AT"
PR_TRIGGER_COMMENT_ID="$DETECTED_TRIGGER_COMMENT_ID"
# Persist to state file
TEMP_FILE="${STATE_FILE}.trigger.$$"
sed -e "s/^last_trigger_at:.*/last_trigger_at: $DETECTED_TRIGGER_AT/" \
-e "s/^trigger_comment_id:.*/trigger_comment_id: $DETECTED_TRIGGER_COMMENT_ID/" \
"$STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$STATE_FILE"
# Note: Claude eyes verification is done in the dedicated section below
# (after trigger detection) to ensure it runs on EVERY exit attempt
else
echo "Using existing trigger timestamp: $PR_LAST_TRIGGER_AT" >&2
fi
fi
# ========================================
# Determine if Trigger is Required (needed for Claude eyes check below)
# ========================================
# Trigger requirement logic:
# - Round 0, startup_case 1: No trigger required (waiting for initial auto-reviews)
# - Round 0, startup_case 2/3: No trigger required (process existing comments)
# - Round 0, startup_case 4/5: Trigger required (new commits after reviews)
# - Round > 0: Always require trigger
# - NEW: If new commits detected during this poll, require trigger (overrides cases 2/3)
REQUIRE_TRIGGER=false
if [[ "$PR_CURRENT_ROUND" -gt 0 ]]; then
# Subsequent rounds always require a trigger
REQUIRE_TRIGGER=true
elif [[ "$NEW_COMMITS_DETECTED" == "true" ]]; then
# New commits detected during this poll - require fresh trigger
# This overrides cases 2/3 to prevent reusing stale reviews
REQUIRE_TRIGGER=true
elif [[ "$PR_CURRENT_ROUND" -eq 0 ]]; then
case "${PR_STARTUP_CASE:-1}" in
1|2|3)
# Case 1: No comments yet - wait for initial auto-reviews
# Case 2/3: Comments exist - process them without requiring new trigger
REQUIRE_TRIGGER=false
;;
4|5)
# Case 4/5: All commented but new commits pushed - require re-trigger
REQUIRE_TRIGGER=true
;;
*)
# Unknown case, default to not requiring trigger
REQUIRE_TRIGGER=false
;;
esac
fi
# ========================================
# Step 8: Check for Codex +1 Reaction (After Trigger Detection)
# ========================================
# IMPORTANT: This check runs AFTER trigger detection to ensure:
# 1. We use the correct timestamp that accounts for new commits
# 2. If trigger is required but missing, we don't approve based on old +1
# Check for codex bot in active bots
CODEX_IN_ACTIVE=false
for bot in "${PR_ACTIVE_BOTS_ARRAY[@]}"; do
if [[ "$bot" == "codex" ]]; then
CODEX_IN_ACTIVE=true
break
fi
done
if [[ "$CODEX_IN_ACTIVE" == "true" ]]; then
# Skip +1 check if trigger is required but not yet posted
# (User needs to post @codex comment first)
if [[ "$REQUIRE_TRIGGER" == "true" && -z "$PR_LAST_TRIGGER_AT" ]]; then
echo "Skipping Codex +1 check: trigger required but not yet posted" >&2
else
echo "Round $PR_CURRENT_ROUND: Checking for Codex +1 reaction on PR..." >&2
# Determine the timestamp for filtering +1 reactions
# Use trigger timestamp if available, otherwise fall back to loop start time
CODEX_REACTION_AFTER="${PR_LAST_TRIGGER_AT:-$PR_STARTED_AT}"
echo " (Checking for +1 after: $CODEX_REACTION_AFTER)" >&2
# Check for +1 reaction from Codex
CODEX_REACTION=$("$PLUGIN_ROOT/scripts/check-bot-reactions.sh" codex-thumbsup "$PR_NUMBER" --after "$CODEX_REACTION_AFTER" 2>/dev/null) || CODEX_REACTION=""
if [[ -n "$CODEX_REACTION" && "$CODEX_REACTION" != "null" ]]; then
REACTION_AT=$(echo "$CODEX_REACTION" | jq -r '.created_at')
echo "Codex +1 detected at $REACTION_AT - removing codex from active_bots" >&2
# Remove only codex from active_bots, keep other bots
declare -a NEW_ACTIVE_BOTS_AFTER_THUMBSUP=()
for bot in "${PR_ACTIVE_BOTS_ARRAY[@]}"; do
if [[ "$bot" != "codex" ]]; then
NEW_ACTIVE_BOTS_AFTER_THUMBSUP+=("$bot")
fi
done
# If no other bots remain, loop is complete
if [[ ${#NEW_ACTIVE_BOTS_AFTER_THUMBSUP[@]} -eq 0 ]]; then
echo "Codex was the only active bot - PR loop approved!" >&2
mv "$STATE_FILE" "$LOOP_DIR/approve-state.md"
exit 0
fi
# Update active_bots in state file and continue with other bots
echo "Continuing with remaining bots: ${NEW_ACTIVE_BOTS_AFTER_THUMBSUP[*]}" >&2
PR_ACTIVE_BOTS_ARRAY=("${NEW_ACTIVE_BOTS_AFTER_THUMBSUP[@]}")
# Update state file
NEW_ACTIVE_BOTS_YAML=$(build_yaml_list "${PR_ACTIVE_BOTS_ARRAY[@]}")
TEMP_FILE="${STATE_FILE}.thumbsup.$$"
# Replace active_bots section in state file
awk -v new_bots="$NEW_ACTIVE_BOTS_YAML" '
/^active_bots:/ {
print "active_bots:" new_bots
in_bots=1
next
}
in_bots && /^[[:space:]]+-/ { next }
in_bots && /^[a-zA-Z]/ { in_bots=0 }
{ print }
' "$STATE_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$STATE_FILE"
fi
fi
fi
# ========================================
# Validate Trigger Comment Exists (Based on startup_case and round)
# ========================================
# Validate trigger FIRST, before Claude eyes check
# This ensures we don't waste time checking eyes on a stale trigger_comment_id
if [[ "$REQUIRE_TRIGGER" == "true" && -z "$PR_LAST_TRIGGER_AT" ]]; then
# Determine startup case description for template
STARTUP_CASE_DESC="requires trigger comment"
case "${PR_STARTUP_CASE:-1}" in
4) STARTUP_CASE_DESC="New commits after all bots reviewed" ;;
5) STARTUP_CASE_DESC="New commits after partial bot reviews" ;;
*) STARTUP_CASE_DESC="Subsequent round requires trigger" ;;
esac
FALLBACK_MSG="# Missing Trigger Comment
No @bot mention found. Please run: gh pr comment $PR_NUMBER --body \"$PR_BOT_MENTION_STRING please review\""
REASON=$(load_and_render_safe "$TEMPLATE_DIR" "block/no-trigger-comment.md" "$FALLBACK_MSG" \
"STARTUP_CASE=${PR_STARTUP_CASE:-1}" "STARTUP_CASE_DESC=$STARTUP_CASE_DESC" \
"CURRENT_ROUND=$PR_CURRENT_ROUND" "BOT_MENTION_STRING=$PR_BOT_MENTION_STRING")
jq -n --arg reason "$REASON" --arg msg "PR Loop: Missing trigger comment - please @mention bots first" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
# ========================================
# Claude Eyes Verification (AFTER trigger validation)
# ========================================
# Verify Claude eyes ONLY AFTER trigger is confirmed to exist
# This prevents checking eyes on a stale trigger_comment_id
# Conditions:
# 1. Claude is configured AND
# 2. A trigger is actually required (REQUIRE_TRIGGER=true) AND
# 3. A trigger comment ID exists (PR_TRIGGER_COMMENT_ID from confirmed detection above)
CLAUDE_CONFIGURED=false
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
if [[ "$bot" == "claude" ]]; then
CLAUDE_CONFIGURED=true
break
fi
done
if [[ "$CLAUDE_CONFIGURED" == "true" && "$REQUIRE_TRIGGER" == "true" ]]; then
# Use the confirmed trigger comment ID (updated by detect_trigger_comment above)
TRIGGER_ID_TO_CHECK="${PR_TRIGGER_COMMENT_ID:-}"
if [[ -n "$TRIGGER_ID_TO_CHECK" ]]; then
echo "Verifying Claude eyes reaction on trigger comment (ID: $TRIGGER_ID_TO_CHECK)..." >&2
# Check for eyes reaction with 3x5s retry
# Pass --pr for fork PR support (reactions are on base repo)
EYES_REACTION=$("$PLUGIN_ROOT/scripts/check-bot-reactions.sh" claude-eyes "$TRIGGER_ID_TO_CHECK" --pr "$PR_NUMBER" --retry 3 --delay 5 2>/dev/null) || EYES_REACTION=""
if [[ -z "$EYES_REACTION" || "$EYES_REACTION" == "null" ]]; then
# Claude eyes verification is BLOCKING - error after 3x5s retries
FALLBACK_MSG="# Claude Bot Not Responding
The Claude bot did not respond with an 'eyes' reaction within 15 seconds (3 x 5s retries).
Please verify the Claude bot is installed and configured for this repository."
REASON=$(load_and_render_safe "$TEMPLATE_DIR" "block/claude-eyes-timeout.md" "$FALLBACK_MSG" \
"RETRY_COUNT=3" "TOTAL_WAIT_SECONDS=15")
jq -n --arg reason "$REASON" --arg msg "PR Loop: Claude bot not responding - check bot configuration" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
else
echo "Claude eyes reaction confirmed!" >&2
fi
else
# Trigger exists (PR_LAST_TRIGGER_AT is set) but no ID - should not happen normally
echo "Warning: Trigger exists but no comment ID for eyes verification" >&2
fi
elif [[ "$CLAUDE_CONFIGURED" == "true" ]]; then
echo "Claude is configured but trigger not required (startup_case=${PR_STARTUP_CASE:-1}, round=$PR_CURRENT_ROUND) - skipping eyes verification" >&2
fi
# ========================================
# Poll for New Bot Reviews (per-bot tracking)
# ========================================
# Poll ALL configured bots, not just active - allows re-adding approved bots if they post new issues
echo "Polling for new bot reviews on PR #$PR_NUMBER..." >&2
echo "Configured bots: $PR_CONFIGURED_BOTS_DISPLAY" >&2
echo "Active bots: $PR_ACTIVE_BOTS_DISPLAY" >&2
echo "Poll interval: ${PR_POLL_INTERVAL}s, Timeout: ${PR_POLL_TIMEOUT}s per bot" >&2
POLL_SCRIPT="$PLUGIN_ROOT/scripts/poll-pr-reviews.sh"
# Consistent file naming: round-N files all refer to round N
COMMENT_FILE="$LOOP_DIR/round-${NEXT_ROUND}-pr-comment.md"
# Get timestamp for filtering based on startup_case and round
# - With trigger: use trigger timestamp (most accurate)
# - Round 0, Case 1: use started_at (waiting for new auto-reviews)
# - Round 0, Case 2/3: use epoch 0 to collect ALL existing comments
# - Round 0, Case 4/5: should have trigger (blocked above if missing)
AFTER_TIMESTAMP=""
USE_ALL_COMMENTS=false
if [[ -n "$PR_LAST_TRIGGER_AT" ]]; then
# Always use trigger timestamp when available
AFTER_TIMESTAMP="$PR_LAST_TRIGGER_AT"
echo "Round $PR_CURRENT_ROUND: using trigger timestamp for --after: $AFTER_TIMESTAMP" >&2
elif [[ "$PR_CURRENT_ROUND" -eq 0 ]]; then
case "${PR_STARTUP_CASE:-1}" in
1)
# Case 1: No comments yet - filter by started_at to wait for new reviews
AFTER_TIMESTAMP="${PR_STARTED_AT}"
echo "Round 0, Case 1: using started_at for --after: $AFTER_TIMESTAMP" >&2
;;
2|3)
# Case 2/3: Existing comments - collect ALL of them (no timestamp filter)
USE_ALL_COMMENTS=true
AFTER_TIMESTAMP="1970-01-01T00:00:00Z" # Epoch 0 to include all comments
echo "Round 0, Case ${PR_STARTUP_CASE}: collecting ALL existing bot comments" >&2
;;
*)
# Case 4/5 should have been blocked above, use started_at as fallback
AFTER_TIMESTAMP="${PR_STARTED_AT}"
echo "Round 0, Case ${PR_STARTUP_CASE}: using started_at for --after: $AFTER_TIMESTAMP" >&2
;;
esac
else
# Round N>0 with no trigger - this should have been blocked earlier
# but handle defensively by blocking here too
REASON="# Missing Trigger Comment
No @bot mention comment found from you on this PR.
Before polling for bot reviews, you must comment on the PR to trigger the bots.
**Please run:**
\`\`\`bash
gh pr comment $PR_NUMBER --body \"$PR_BOT_MENTION_STRING please review the latest changes\"
\`\`\`
Then try exiting again."
jq -n --arg reason "$REASON" --arg msg "PR Loop: Missing trigger comment" \
'{"decision": "block", "reason": $reason, "systemMessage": $msg}'
exit 0
fi
# Convert trigger timestamp to epoch for timeout anchoring
# Per-bot timeouts are measured from the TRIGGER time, not poll start time
# Special case: when USE_ALL_COMMENTS is true (startup cases 2/3), we're looking at
# ALL historical comments. In this case, anchor timeout to NOW (poll start time)
# rather than PR_STARTED_AT, which could be hours old and cause instant timeout.
if [[ "$USE_ALL_COMMENTS" == "true" ]]; then
# Use current time as timeout anchor for historical comment review
TRIGGER_EPOCH=$(date +%s)
else
TRIGGER_EPOCH=$(date -d "$AFTER_TIMESTAMP" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$AFTER_TIMESTAMP" +%s 2>/dev/null || date +%s)
fi
# Track which bots have responded and their individual timeouts
# IMPORTANT: Poll ALL configured bots (not just active) so we can detect when
# previously approved bots post new issues and re-add them to active_bots
# IMPORTANT: Timeouts are anchored to TRIGGER_EPOCH, not poll start time
# This ensures the 15-minute window is measured from when the @mention was posted
#
# NOTE: Using dynamic variable names instead of associative arrays (declare -A)
# for macOS Bash 3.2 compatibility. Associative arrays require Bash 4.0+.
# Helper functions to get/set values:
_sanitize_key() { echo "$1" | tr -c 'a-zA-Z0-9_' '_'; }
_map_get() { local var="$1_$(_sanitize_key "$2")"; echo "${!var}"; }
_map_set() { local var="$1_$(_sanitize_key "$2")"; eval "$var=\"$3\""; }
_map_isset() { local var="$1_$(_sanitize_key "$2")"; [[ -n "${!var+x}" ]]; }
POLL_START_EPOCH=$(date +%s)
echo "Timeout anchor: trigger at epoch $TRIGGER_EPOCH (poll started at $POLL_START_EPOCH)" >&2
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
_map_set "BOTS_RESPONDED" "$bot" "false"
_map_set "BOTS_TIMED_OUT" "$bot" "false"
# Use TRIGGER_EPOCH for timeout, not poll start
_map_set "BOTS_TIMEOUT_START" "$bot" "$TRIGGER_EPOCH"
done
# Collect all new comments with deduplication by id
# Using dynamic variables: SEEN_ID_<sanitized_id>=1
ALL_NEW_COMMENTS="[]"
while true; do
CURRENT_TIME=$(date +%s)
# Check if all configured bots have responded OR timed out (per-bot 15min timeout)
ALL_DONE=true
WAITING_BOTS=""
TIMED_OUT_BOTS=""
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
if [[ "$(_map_get BOTS_RESPONDED "$bot")" == "true" ]]; then
continue # Bot already responded
fi
# Check per-bot timeout (15 minutes each) - auto-remove after timeout
BOT_ELAPSED=$((CURRENT_TIME - $(_map_get BOTS_TIMEOUT_START "$bot")))
if [[ $BOT_ELAPSED -ge $PR_POLL_TIMEOUT ]]; then
echo "Bot '$bot' timed out after ${PR_POLL_TIMEOUT}s - will be removed from active_bots" >&2
_map_set "BOTS_TIMED_OUT" "$bot" "true" # Mark as timed out for later removal
if [[ -n "$TIMED_OUT_BOTS" ]]; then
TIMED_OUT_BOTS="${TIMED_OUT_BOTS}, ${bot}"
else
TIMED_OUT_BOTS="$bot"
fi
continue # Mark as done (timed out)
fi
# Bot still waiting
ALL_DONE=false
if [[ -n "$WAITING_BOTS" ]]; then
WAITING_BOTS="${WAITING_BOTS},${bot}"
else
WAITING_BOTS="$bot"
fi
done
if [[ "$ALL_DONE" == "true" ]]; then
if [[ -n "$TIMED_OUT_BOTS" ]]; then
echo "Polling complete. Timed out bots: $TIMED_OUT_BOTS" >&2
else
echo "All configured bots have responded!" >&2
fi
break
fi
# Check for cancel signal
if [[ -f "$LOOP_DIR/.cancel-requested" ]]; then
echo "Cancel requested, exiting poll loop..." >&2
exit 0
fi
TOTAL_ELAPSED=$((CURRENT_TIME - POLL_START_EPOCH))
echo "Poll attempt (elapsed: ${TOTAL_ELAPSED}s, waiting for: $WAITING_BOTS)..." >&2
# Poll for new comments from bots we're still waiting for
POLL_RESULT=$("$POLL_SCRIPT" "$PR_NUMBER" --after "$AFTER_TIMESTAMP" --bots "$WAITING_BOTS" 2>/dev/null) || {
echo "Warning: Poll script failed, retrying..." >&2
sleep "$PR_POLL_INTERVAL"
continue
}
# Check which bots responded (check all configured bots)
# Poll script returns author names (e.g., chatgpt-codex-connector[bot])
# We need to map them back to bot names (e.g., codex)
RESPONDED_BOTS=$(echo "$POLL_RESULT" | jq -r '.bots_responded[]' 2>/dev/null || true)
for responded_author in $RESPONDED_BOTS; do
# Map author name to bot name (e.g., chatgpt-codex-connector[bot] -> codex)
responded_bot=$(map_author_to_bot "$responded_author")
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
if [[ "$responded_bot" == "$bot" ]]; then
if [[ "$(_map_get BOTS_RESPONDED "$bot")" != "true" ]]; then
_map_set "BOTS_RESPONDED" "$bot" "true"
echo "Bot '$bot' has responded!" >&2
fi
fi
done
done
# Check for Codex +1 reaction during polling (any round)
# Codex may give +1 instead of commenting if no issues found
if [[ "$(_map_get BOTS_RESPONDED codex)" != "true" ]]; then
# Check if codex is a configured bot
CODEX_CONFIGURED=false
for bot in "${PR_CONFIGURED_BOTS_ARRAY[@]}"; do
[[ "$bot" == "codex" ]] && CODEX_CONFIGURED=true && break
done
if [[ "$CODEX_CONFIGURED" == "true" ]]; then
# Determine timestamp for filtering - use trigger if available, else loop start
POLL_REACTION_AFTER="${PR_LAST_TRIGGER_AT:-$PR_STARTED_AT}"
# Check for +1 reaction
THUMBSUP_RESULT=$("$PLUGIN_ROOT/scripts/check-bot-reactions.sh" codex-thumbsup "$PR_NUMBER" --after "$POLL_REACTION_AFTER" 2>/dev/null) || THUMBSUP_RESULT=""