Skip to content

Commit e55de43

Browse files
steipetePeter Steinberger
andauthored
fix: stop durable comment sync from starving automatic closures (#1016)
* fix: coalesce bounded comment sync without starving closures * fix: preserve source freshness and locked review synchronization * fix: preserve truthful guarded review publication and queue fairness --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent fd51157 commit e55de43

16 files changed

Lines changed: 4884 additions & 234 deletions

‎.github/workflows/sweep.yml‎

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ env:
239239
OPENROUTER_API_KEY: ${{ vars.CLAWSWEEPER_RUNNER == 'openclaw' && secrets.OPENROUTER_API_KEY || '' }}
240240

241241
concurrency:
242-
group: ${{ (github.event_name == 'schedule' && (github.event.schedule == '4/5 * * * *' || github.event.schedule == '41/10 * * * *' || github.event.schedule == '37 */6 * * *')) && format('clawsweeper-target-fanout-{0}', github.event.schedule || github.run_id) || github.event_name == 'repository_dispatch' && format('clawsweeper-event-{0}-{1}', github.event.client_payload.target_repo || 'openclaw/openclaw', github.event.client_payload.queue_lease_id || github.event.client_payload.item_number || github.run_id) || format('{0}-{1}', (github.event_name == 'schedule' && github.event.schedule == '13 * * * *') && 'clawsweeper-failed-review-retry' || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing == 'true' && github.event.inputs.apply_sync_comments_only == 'true') && format('clawsweeper-comment-sync-{0}', github.run_id) || (github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *') && 'clawsweeper-comment-sync' || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing == 'true') && format('clawsweeper-apply-{0}', github.run_id) || (github.event_name == 'schedule' && (github.event.schedule == '3 * * * *' || github.event.schedule == '18 * * * *' || github.event.schedule == '33 * * * *' || github.event.schedule == '48 * * * *' || github.event.schedule == '8,23,38,53 * * * *')) && 'clawsweeper-apply' || (github.event_name == 'workflow_dispatch' && (github.event.inputs.item_number != '' || github.event.inputs.item_numbers != '')) && format('clawsweeper-intake-exact-{0}', github.event.inputs.item_number || github.event.inputs.item_numbers) || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing != 'true') && format('clawsweeper-operator-dispatch-{0}', github.run_id) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.hot_intake == 'true') || (github.event_name == 'schedule' && (github.event.schedule == '*/5 * * * *' || github.event.schedule == '2/5 * * * *'))) && 'clawsweeper-intake-v2' || ((github.event_name == 'workflow_dispatch' && github.event.inputs.audit_dashboard == 'true') || (github.event_name == 'schedule' && (github.event.schedule == '7 */6 * * *' || github.event.schedule == '12 */6 * * *' || github.event.schedule == '17 */6 * * *'))) && 'clawsweeper-audit' || 'clawsweeper-review', github.event.inputs.target_repo || github.event.client_payload.target_repo || ((github.event.schedule == '17 */6 * * *') && 'openclaw/clawsweeper' || ((github.event.schedule == '2/5 * * * *' || github.event.schedule == '22 * * * *' || github.event.schedule == '8,23,38,53 * * * *' || github.event.schedule == '12 */6 * * *') && 'openclaw/clawhub' || 'openclaw/openclaw'))) }}
242+
group: ${{ (github.event_name == 'schedule' && (github.event.schedule == '4/5 * * * *' || github.event.schedule == '41/10 * * * *' || github.event.schedule == '37 */6 * * *')) && format('clawsweeper-target-fanout-{0}', github.event.schedule || github.run_id) || github.event_name == 'repository_dispatch' && format('clawsweeper-event-{0}-{1}', github.event.client_payload.target_repo || 'openclaw/openclaw', github.event.client_payload.queue_lease_id || github.event.client_payload.item_number || github.run_id) || format('{0}-{1}', (github.event_name == 'schedule' && github.event.schedule == '13 * * * *') && 'clawsweeper-failed-review-retry' || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing == 'true' && github.event.inputs.apply_sync_comments_only == 'true') && (github.event.inputs.apply_item_numbers == '__cursor__' && github.event.inputs.apply_kind == 'all' && github.event.inputs.apply_comment_sync_min_age_days == '0' && github.event.inputs.apply_limit == '40' && github.event.inputs.apply_min_age_days == '0' && github.event.inputs.apply_min_age_minutes == '' && (github.event.inputs.apply_close_reasons == '' || github.event.inputs.apply_close_reasons == (vars.CLAWSWEEPER_AUTO_CLOSE_REASONS || 'all')) && github.event.inputs.apply_stale_min_age_days == '60' && github.event.inputs.apply_close_delay_ms == '2000' && github.event.inputs.apply_checkpoint_size == '40' && 'clawsweeper-comment-sync' || format('clawsweeper-comment-sync-{0}', github.run_id)) || (github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *') && 'clawsweeper-comment-sync' || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing == 'true') && format('clawsweeper-apply-{0}', github.run_id) || (github.event_name == 'schedule' && (github.event.schedule == '3 * * * *' || github.event.schedule == '18 * * * *' || github.event.schedule == '33 * * * *' || github.event.schedule == '48 * * * *' || github.event.schedule == '8,23,38,53 * * * *')) && 'clawsweeper-apply' || (github.event_name == 'workflow_dispatch' && (github.event.inputs.item_number != '' || github.event.inputs.item_numbers != '')) && format('clawsweeper-intake-exact-{0}', github.event.inputs.item_number || github.event.inputs.item_numbers) || (github.event_name == 'workflow_dispatch' && github.event.inputs.apply_existing != 'true') && format('clawsweeper-operator-dispatch-{0}', github.run_id) || ((github.event_name == 'workflow_dispatch' && github.event.inputs.hot_intake == 'true') || (github.event_name == 'schedule' && (github.event.schedule == '*/5 * * * *' || github.event.schedule == '2/5 * * * *'))) && 'clawsweeper-intake-v2' || ((github.event_name == 'workflow_dispatch' && github.event.inputs.audit_dashboard == 'true') || (github.event_name == 'schedule' && (github.event.schedule == '7 */6 * * *' || github.event.schedule == '12 */6 * * *' || github.event.schedule == '17 */6 * * *'))) && 'clawsweeper-audit' || 'clawsweeper-review', github.event.inputs.target_repo || github.event.client_payload.target_repo || ((github.event.schedule == '17 */6 * * *') && 'openclaw/clawsweeper' || ((github.event.schedule == '2/5 * * * *' || github.event.schedule == '22 * * * *' || github.event.schedule == '8,23,38,53 * * * *' || github.event.schedule == '12 */6 * * *') && 'openclaw/clawhub' || 'openclaw/openclaw'))) }}
243243
cancel-in-progress: false
244244

245245
jobs:
@@ -4711,10 +4711,12 @@ jobs:
47114711
-f target_repo="$TARGET_REPO" \
47124712
-f apply_existing=true \
47134713
-f apply_sync_comments_only=true \
4714-
-f apply_item_numbers="$item_numbers" \
4715-
-f apply_limit=0 \
4716-
-f apply_comment_sync_min_age_days=7; then
4717-
echo "Dispatched background comment sync for item numbers: $item_numbers"
4714+
-f apply_item_numbers=__cursor__ \
4715+
-f apply_kind=all \
4716+
-f apply_limit=40 \
4717+
-f apply_min_age_days=0 \
4718+
-f apply_comment_sync_min_age_days=0; then
4719+
echo "Dispatched coalesced durable comment sync for item numbers: $item_numbers"
47184720
exit 0
47194721
fi
47204722
sleep "$((attempt * 10))"
@@ -5400,6 +5402,7 @@ jobs:
54005402
build-script: build:all
54015403

54025404
- name: Reconcile read-only proof inputs
5405+
if: ${{ !(github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *') && !(github.event_name == 'workflow_dispatch' && (github.event.inputs.apply_sync_comments_only == 'true' || github.event.inputs.apply_item_numbers == '__cursor__')) }}
54035406
env:
54045407
GH_TOKEN: ${{ github.token }}
54055408
TARGET_REPO: ${{ steps.target.outputs.target_repo }}
@@ -5428,6 +5431,10 @@ jobs:
54285431
stale_min_age_days="${{ github.event_name == 'workflow_dispatch' && github.event.inputs.apply_stale_min_age_days || '60' }}"
54295432
item_numbers="${{ github.event.inputs.apply_item_numbers || '' }}"
54305433
sync_comments_only="${{ github.event_name == 'workflow_dispatch' && github.event.inputs.apply_sync_comments_only || 'false' }}"
5434+
if [ "${{ github.event_name }}" = "schedule" ] &&
5435+
[ "${{ github.event.schedule || '' }}" = "6,21,36,51 * * * *" ]; then
5436+
sync_comments_only="true"
5437+
fi
54315438
if [ "$item_numbers" = "__cursor__" ]; then
54325439
item_numbers=""
54335440
sync_comments_only="true"
@@ -5767,13 +5774,18 @@ jobs:
57675774
set -euo pipefail
57685775
source scripts/apply-workflow-helpers.sh
57695776
item_numbers="${{ github.event_name == 'repository_dispatch' && github.event.client_payload.item_number || github.event.inputs.apply_item_numbers || '' }}"
5777+
if [ "${{ github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *' && 'true' || 'false' }}" = "true" ]; then
5778+
echo "Deferring reconciliation until the bounded comment-sync cursor is selected."
5779+
exit 0
5780+
fi
57705781
if [ "$item_numbers" = "__cursor__" ]; then
5782+
if [ "${{ github.event.inputs.apply_sync_comments_only || 'false' }}" = "true" ]; then
5783+
echo "Deferring reconciliation until the bounded comment-sync cursor is selected."
5784+
exit 0
5785+
fi
57715786
item_numbers=""
57725787
fi
5773-
reconcile_args=(--target-repo "$TARGET_REPO" --skip-closed-at)
5774-
if [ -n "$item_numbers" ]; then
5775-
reconcile_args+=(--item-numbers "$item_numbers")
5776-
fi
5788+
prepare_apply_reconciliation_args
57775789
persist_reconciliation "${reconcile_args[@]}"
57785790
57795791
- name: Apply unchanged proposed decisions with checkpoints
@@ -5806,7 +5818,7 @@ jobs:
58065818
base_close_processed_limit=600
58075819
close_processed_limit="$base_close_processed_limit"
58085820
adaptive_apply_scan_reason="base_window"
5809-
sync_batch_size="${{ github.event_name == 'workflow_dispatch' && github.event.inputs.apply_limit || '25' }}"
5821+
sync_batch_size="${{ github.event_name == 'workflow_dispatch' && github.event.inputs.apply_limit || '40' }}"
58105822
item_numbers="${{ github.event_name == 'repository_dispatch' && github.event.client_payload.item_number || github.event.inputs.apply_item_numbers || '' }}"
58115823
scheduled_comment_sync="${{ github.event_name == 'schedule' && github.event.schedule == '6,21,36,51 * * * *' && 'true' || 'false' }}"
58125824
sync_open_pr_batch="$scheduled_comment_sync"
@@ -5853,6 +5865,10 @@ jobs:
58535865
candidate_counts_json=""
58545866
cursor_advance_count=""
58555867
coverage_proof_item_numbers=""
5868+
if [ "$checkpoint_size" -gt 40 ]; then
5869+
echo "Capping apply checkpoint size at 40"
5870+
checkpoint_size=40
5871+
fi
58565872
normalize_comment_sync_mode
58575873
prepare_comment_sync_batch
58585874
sync_comments_arg=()
@@ -5863,18 +5879,8 @@ jobs:
58635879
if [ -n "$min_age_minutes" ]; then
58645880
min_age_minutes_arg=(--min-age-minutes "$min_age_minutes")
58655881
fi
5866-
if [ "$checkpoint_size" -gt 40 ]; then
5867-
echo "Capping apply checkpoint size at 40"
5868-
checkpoint_size=40
5869-
fi
58705882
select_adaptive_apply_batch
58715883
mkdir -p .artifacts/apply-reports
5872-
reconcile_args=(--target-repo "$TARGET_REPO" --skip-closed-at)
5873-
if [ -n "$item_numbers" ]; then
5874-
reconcile_args+=(--item-numbers "$item_numbers")
5875-
fi
5876-
persist_reconciliation "${reconcile_args[@]}"
5877-
summarize_apply_candidate_quality
58785884
if [ "$sync_open_pr_batch" = "true" ] && [ -z "$item_numbers" ]; then
58795885
batch_env=".artifacts/comment-sync-batch.env"
58805886
pnpm run --silent workflow -- comment-sync-batch \
@@ -5917,6 +5923,9 @@ jobs:
59175923
fi
59185924
echo "Selected cursor-based comment sync batch: $item_numbers"
59195925
fi
5926+
prepare_apply_reconciliation_args
5927+
persist_reconciliation "${reconcile_args[@]}"
5928+
summarize_apply_candidate_quality
59205929
if [ "$sync_comments_only" != "true" ] && [ -z "$item_numbers" ]; then
59215930
auto_selected_apply_batch=true
59225931
select_apply_candidate_inventory

‎scripts/apply-workflow-helpers.sh‎

Lines changed: 110 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ normalize_comment_sync_mode() {
5050
fi
5151
sync_comments_only=true
5252
if [ "${scheduled_comment_sync:-false}" = "true" ]; then
53-
apply_kind="pull_request"
53+
apply_kind="all"
5454
comment_sync_min_age_days=0
5555
fi
5656
}
@@ -80,6 +80,27 @@ trim_comment_sync_cycle_batch() {
8080
fi
8181
}
8282

83+
comment_sync_uses_default_mutation_policy() {
84+
[ "${apply_close_reasons:-${CLAWSWEEPER_AUTO_CLOSE_REASONS:-all}}" = "${CLAWSWEEPER_AUTO_CLOSE_REASONS:-all}" ] &&
85+
[ "${stale_min_age_days:-60}" = "60" ] &&
86+
[ "${close_delay_ms:-2000}" = "2000" ] &&
87+
[ "${checkpoint_size:-40}" = "40" ] &&
88+
[ "${limit:-40}" = "40" ]
89+
}
90+
91+
comment_sync_uses_automatic_policy() {
92+
[ "${sync_open_pr_batch:-false}" = "true" ] &&
93+
comment_sync_matches_automatic_policy
94+
}
95+
96+
comment_sync_matches_automatic_policy() {
97+
[ "${apply_kind:-all}:${comment_sync_min_age_days:-0}" = "all:0" ] &&
98+
[ "${sync_batch_size:-40}" = "40" ] &&
99+
[ "${min_age_days:-0}" = "0" ] &&
100+
[ -z "${min_age_minutes:-}" ] &&
101+
comment_sync_uses_default_mutation_policy
102+
}
103+
83104
prepare_comment_sync_batch() {
84105
comment_sync_pending_items=""
85106
comment_sync_cursor_advance_count=0
@@ -91,9 +112,27 @@ prepare_comment_sync_batch() {
91112
fi
92113
if [ -z "${item_numbers:-}" ] &&
93114
[ "${scheduled_comment_sync:-false}" != "true" ] &&
94-
{ [ "${apply_kind:-all}" != "pull_request" ] ||
95-
[ "${comment_sync_min_age_days:-0}" != "0" ]; }; then
115+
! comment_sync_matches_automatic_policy; then
96116
cursor_path="results/comment-sync-cursors/${target_slug}-${apply_kind:-all}-age${comment_sync_min_age_days:-0}.json"
117+
if ! comment_sync_uses_default_mutation_policy ||
118+
[ "${sync_batch_size:-40}" != "40" ] ||
119+
[ "${min_age_days:-0}" != "0" ] ||
120+
[ -n "${min_age_minutes:-}" ]; then
121+
local mutation_policy
122+
mutation_policy="$(printf '%s\n' \
123+
"${apply_close_reasons:-${CLAWSWEEPER_AUTO_CLOSE_REASONS:-all}}" \
124+
"${stale_min_age_days:-60}" \
125+
"${close_delay_ms:-2000}" \
126+
"${checkpoint_size:-40}" \
127+
"${limit:-40}" \
128+
"${sync_batch_size:-40}" \
129+
"${min_age_days:-0}" \
130+
"${min_age_minutes:-}" | sha256sum | cut -c1-16)"
131+
cursor_path="${cursor_path%.json}-policy-${mutation_policy}.json"
132+
fi
133+
fi
134+
if [ -z "${cursor_path:-}" ] && [ -z "${item_numbers:-}" ]; then
135+
cursor_path="results/comment-sync-cursors/${target_slug}.json"
97136
fi
98137
if [ -f "${cursor_path:-}" ]; then
99138
comment_sync_initial_cursor="$(jq -er '
@@ -185,18 +224,62 @@ complete_comment_sync_batch() {
185224
if [ -n "${item_numbers:-}" ]; then
186225
IFS=, read -r -a selected_items <<<"$item_numbers"
187226
fi
227+
local canonical_target_slug="${target_slug:-}"
228+
if [ -z "$canonical_target_slug" ]; then
229+
canonical_target_slug="$(printf '%s' "$TARGET_REPO" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9_.-]/-/g')"
230+
fi
231+
local selected_item
232+
for selected_item in "${selected_items[@]}"; do
233+
case ",$completed_csv," in
234+
*",$selected_item,"*) continue ;;
235+
esac
236+
local canonical_items_dir="records/${canonical_target_slug}/items"
237+
local canonical_closed_dir="records/${canonical_target_slug}/closed"
238+
if [ ! -f "${canonical_items_dir}/${selected_item}.md" ] &&
239+
[ ! -f "${canonical_items_dir}/${canonical_target_slug}-${selected_item}.md" ] &&
240+
{ [ -f "${canonical_closed_dir}/${selected_item}.md" ] ||
241+
[ -f "${canonical_closed_dir}/${canonical_target_slug}-${selected_item}.md" ]; }; then
242+
completed_csv="${completed_csv:+$completed_csv,}$selected_item"
243+
fi
244+
done
188245
local completed_count=0
189246
local cursor_count=0
190247
local safe_cursor=""
191248
local blocked_prefix=false
249+
local initial_cursor="${comment_sync_initial_cursor:-0}"
192250
local unfinished_items=()
193-
local selected_item
194251
for selected_item in "${selected_items[@]}"; do
195252
case ",$completed_csv," in
196253
*",$selected_item,"*)
197254
completed_count=$((completed_count + 1))
198255
if [ "$blocked_prefix" != "true" ]; then
199-
safe_cursor="$selected_item"
256+
if [ "$sync_open_pr_batch" = "true" ] && [ -n "${next_cursor:-}" ] &&
257+
{ [ "$selected_item" -gt "$next_cursor" ] ||
258+
{ [ "$selected_item" -le "$initial_cursor" ] &&
259+
[ "$next_cursor" -ge "$initial_cursor" ]; }; }; then
260+
continue
261+
fi
262+
local blocks_cursor_advance=false
263+
local pending_item
264+
for pending_item in "${selected_items[@]}"; do
265+
case ",$completed_csv," in
266+
*",$pending_item,"*) continue ;;
267+
esac
268+
if [ "$pending_item" -lt "$selected_item" ] &&
269+
{ { [ "$selected_item" -gt "$initial_cursor" ] &&
270+
[ "$pending_item" -gt "$initial_cursor" ]; } ||
271+
{ [ "$selected_item" -le "$initial_cursor" ] &&
272+
[ "$pending_item" -le "$initial_cursor" ]; }; }; then
273+
blocks_cursor_advance=true
274+
break
275+
fi
276+
done
277+
if [ "$blocks_cursor_advance" = "true" ]; then
278+
continue
279+
fi
280+
if [ -z "$safe_cursor" ] || [ "$selected_item" -gt "$safe_cursor" ]; then
281+
safe_cursor="$selected_item"
282+
fi
200283
cursor_count=$((cursor_count + 1))
201284
fi
202285
;;
@@ -222,15 +305,15 @@ complete_comment_sync_batch() {
222305
--cursor-path "$cursor_path" \
223306
--next-cursor "$next_cursor" \
224307
--target-repo "$TARGET_REPO"
225-
if [ "$TARGET_REPO" != "openclaw/openclaw" ] ||
226-
[ "${apply_kind:-pull_request}" != "pull_request" ] ||
227-
[ "${comment_sync_min_age_days:-0}" != "0" ]; then
228-
local initial_cursor="${comment_sync_initial_cursor:-0}"
308+
if [ "${scheduled_comment_sync:-false}" != "true" ] &&
309+
{ [ "$TARGET_REPO" != "openclaw/openclaw" ] ||
310+
! comment_sync_uses_automatic_policy; }; then
229311
local cycle_start="${comment_sync_cycle_start:-0}"
230312
local cycle_wrapped="${comment_sync_cycle_wrapped:-false}"
231313
if [ "${#selected_items[@]}" -gt 0 ] &&
232314
[ "${selected_items[0]}" -le "$initial_cursor" ] &&
233-
[ "$initial_cursor" -gt 0 ]; then
315+
[ "$initial_cursor" -gt 0 ] &&
316+
[ "$safe_cursor" -le "$initial_cursor" ]; then
234317
cycle_wrapped=true
235318
fi
236319
local lookahead_env
@@ -260,8 +343,11 @@ complete_comment_sync_batch() {
260343
if [ "$cycle_wrapped" = "true" ] && [ "$lookahead_count" -gt 0 ]; then
261344
local lookahead_items
262345
lookahead_items="$(awk -F= '$1 == "item_numbers" { print $2 }' "$lookahead_env")"
263-
local first_lookahead_item="${lookahead_items%%,*}"
264-
if [ "$first_lookahead_item" -gt "$cycle_start" ]; then
346+
if ! jq -enr --arg selected "$lookahead_items" --argjson boundary "$cycle_start" '
347+
$selected | split(",")
348+
| map(tonumber? | select(. > 0 and . <= $boundary))
349+
| length > 0
350+
' >/dev/null; then
265351
lookahead_count=0
266352
fi
267353
fi
@@ -510,6 +596,18 @@ publish_reconciled_records() {
510596
publish_changes_with_strategy normal "$message" "${publish_paths[@]}" || return 1
511597
}
512598

599+
prepare_apply_reconciliation_args() {
600+
reconcile_args=(--target-repo "$TARGET_REPO" --skip-closed-at)
601+
if [ -z "${item_numbers:-}" ]; then
602+
return 0
603+
fi
604+
reconcile_args+=(--item-numbers "$item_numbers")
605+
if [ "${sync_comments_only:-false}" = "true" ] &&
606+
[ "${sync_open_pr_batch:-false}" = "true" ]; then
607+
reconcile_args+=(--only-item-numbers)
608+
fi
609+
}
610+
513611
persist_reconciliation() {
514612
local reconcile_json
515613
local canonical_baseline_dir

0 commit comments

Comments
 (0)