Skip to content

Commit 1343d8e

Browse files
authored
fix(scheduler): count active runs by workflow path (#393)
fix(scheduler): count active runs by workflow path
1 parent 56431d2 commit 1343d8e

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

‎.github/workflows/commit-review.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ jobs:
171171
for run_status in in_progress pending queued waiting requested; do
172172
gh api "repos/${{ github.repository }}/actions/runs?per_page=100&status=${run_status}" \
173173
--paginate \
174-
--jq '.workflow_runs[] | {databaseId:.id, workflowName:.name, displayTitle:.display_title, status:.status, createdAt:.created_at, updatedAt:.updated_at}' 2>/dev/null \
174+
--jq '.workflow_runs[] | {databaseId:.id, workflowPath:.path, displayTitle:.display_title, status:.status, createdAt:.created_at, updatedAt:.updated_at}' 2>/dev/null \
175175
|| true
176176
done
177177
} | jq -s 'unique_by(.databaseId)'
@@ -180,14 +180,14 @@ jobs:
180180
STALE_QUEUED_CUTOFF="$(date -u -d '6 hours ago' '+%Y-%m-%dT%H:%M:%SZ')"
181181
active_run_count() {
182182
printf '%s' "$ACTIVE_RUNS_JSON" \
183-
| WORKFLOW_NAME="$1" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowName == env.WORKFLOW_NAME) | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF)))] | length' 2>/dev/null \
183+
| WORKFLOW_PATH="$1" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowPath == env.WORKFLOW_PATH) | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF)))] | length' 2>/dev/null \
184184
|| printf '0'
185185
}
186186
active_sweep_background_workers() {
187187
local runs total id title status active_shards
188188
total=0
189189
runs="$(printf '%s' "$ACTIVE_RUNS_JSON" \
190-
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq -r '.[] | select(.workflowName == "ClawSweeper") | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) | select(.displayTitle == "Review ClawSweeper items" or .displayTitle == "Review hot ClawSweeper items") | [.databaseId, .displayTitle, .status] | @tsv' 2>/dev/null || true)"
190+
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq -r '.[] | select(.workflowPath == ".github/workflows/sweep.yml") | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) | select(.displayTitle == "Review ClawSweeper items" or .displayTitle == "Review hot ClawSweeper items") | [.databaseId, .displayTitle, .status] | @tsv' 2>/dev/null || true)"
191191
while IFS=$'\t' read -r id title status; do
192192
if [ -z "$id" ]; then
193193
continue
@@ -216,11 +216,11 @@ jobs:
216216
}
217217
active_sweep_exact_count() {
218218
printf '%s' "$ACTIVE_RUNS_JSON" \
219-
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowName == "ClawSweeper") | select((.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) and (.displayTitle | startswith("Review event item ")))] | length' 2>/dev/null \
219+
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowPath == ".github/workflows/sweep.yml") | select((.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) and (.displayTitle | startswith("Review event item ")))] | length' 2>/dev/null \
220220
|| printf '0'
221221
}
222222
if [ -z "$PAGE_SIZE" ]; then
223-
active_critical_workers="$(( $(active_run_count "repair cluster worker") + $(active_sweep_exact_count) ))"
223+
active_critical_workers="$(( $(active_run_count ".github/workflows/repair-cluster-worker.yml") + $(active_sweep_exact_count) ))"
224224
active_background_workers="$(active_sweep_background_workers)"
225225
PAGE_SIZE="$(pnpm --dir clawsweeper run --silent workflow -- worker-limit commit_review --active-critical "$active_critical_workers" --active-background "$active_background_workers")"
226226
fi

‎.github/workflows/sweep.yml‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ jobs:
961961
for run_status in in_progress pending queued waiting requested; do
962962
gh api "repos/${{ github.repository }}/actions/runs?per_page=100&status=${run_status}" \
963963
--paginate \
964-
--jq '.workflow_runs[] | {databaseId:.id, workflowName:.name, displayTitle:.display_title, status:.status, createdAt:.created_at, updatedAt:.updated_at}' 2>/dev/null \
964+
--jq '.workflow_runs[] | {databaseId:.id, workflowPath:.path, displayTitle:.display_title, status:.status, createdAt:.created_at, updatedAt:.updated_at}' 2>/dev/null \
965965
|| true
966966
done
967967
} | jq -s 'unique_by(.databaseId)'
@@ -970,19 +970,19 @@ jobs:
970970
STALE_QUEUED_CUTOFF="$(date -u -d '6 hours ago' '+%Y-%m-%dT%H:%M:%SZ')"
971971
active_run_count() {
972972
printf '%s' "$ACTIVE_RUNS_JSON" \
973-
| WORKFLOW_NAME="$1" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowName == env.WORKFLOW_NAME) | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF)))] | length' 2>/dev/null \
973+
| WORKFLOW_PATH="$1" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowPath == env.WORKFLOW_PATH) | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF)))] | length' 2>/dev/null \
974974
|| printf '0'
975975
}
976976
active_sweep_exact_count() {
977977
printf '%s' "$ACTIVE_RUNS_JSON" \
978-
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowName == "ClawSweeper") | select((.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) and (.displayTitle | startswith("Review event item ")))] | length' 2>/dev/null \
978+
| STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq '[.[] | select(.workflowPath == ".github/workflows/sweep.yml") | select((.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) and (.displayTitle | startswith("Review event item ")))] | length' 2>/dev/null \
979979
|| printf '0'
980980
}
981981
active_sweep_background_workers() {
982982
local runs total id title status jobs_json active_shards total_shards
983983
total=0
984984
runs="$(printf '%s' "$ACTIVE_RUNS_JSON" \
985-
| CURRENT_RUN_ID="${GITHUB_RUN_ID:-0}" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq -r '.[] | select((.databaseId | tostring) != env.CURRENT_RUN_ID) | select(.workflowName == "ClawSweeper") | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) | select(.displayTitle == "Review ClawSweeper items" or .displayTitle == "Review hot ClawSweeper items" or (.displayTitle | startswith("Review target repo ")) or (.displayTitle | startswith("Review hot target repo "))) | [.databaseId, .displayTitle, .status] | @tsv' 2>/dev/null || true)"
985+
| CURRENT_RUN_ID="${GITHUB_RUN_ID:-0}" STALE_QUEUED_CUTOFF="$STALE_QUEUED_CUTOFF" jq -r '.[] | select((.databaseId | tostring) != env.CURRENT_RUN_ID) | select(.workflowPath == ".github/workflows/sweep.yml") | select(.status == "in_progress" or ((.status == "pending" or .status == "queued" or .status == "waiting" or .status == "requested") and ((.updatedAt // .createdAt // "") >= env.STALE_QUEUED_CUTOFF))) | select(.displayTitle == "Review ClawSweeper items" or .displayTitle == "Review hot ClawSweeper items" or (.displayTitle | startswith("Review target repo ")) or (.displayTitle | startswith("Review hot target repo "))) | [.databaseId, .displayTitle, .status] | @tsv' 2>/dev/null || true)"
986986
while IFS=$'\t' read -r id title status; do
987987
if [ -z "$id" ]; then
988988
continue
@@ -1022,8 +1022,8 @@ jobs:
10221022
normal_active_floor="$(limit review_shards.normal_active_floor)"
10231023
hard_shard_cap="$(limit review_shards.hard_cap)"
10241024
commit_page_size="$(limit commit_review.page_size_default)"
1025-
active_critical_workers="$(( $(active_run_count "repair cluster worker") + $(active_sweep_exact_count) ))"
1026-
active_background_workers="$(( $(active_run_count "ClawSweeper Commit Review") * commit_page_size + $(active_sweep_background_workers) ))"
1025+
active_critical_workers="$(( $(active_run_count ".github/workflows/repair-cluster-worker.yml") + $(active_sweep_exact_count) ))"
1026+
active_background_workers="$(( $(active_run_count ".github/workflows/commit-review.yml") * commit_page_size + $(active_sweep_background_workers) ))"
10271027
hot_intake_shards="$(worker_limit hot_intake --active-critical "$active_critical_workers" --active-background "$active_background_workers")"
10281028
normal_shards="$(worker_limit normal_review --active-critical "$active_critical_workers" --active-background "$active_background_workers")"
10291029
hot_intake="${{ ((github.event_name == 'repository_dispatch' && github.event.client_payload.hot_intake == 'true') || (github.event_name == 'workflow_dispatch' && github.event.inputs.hot_intake == 'true') || (github.event_name == 'schedule' && (github.event.schedule == '*/5 * * * *' || github.event.schedule == '2/5 * * * *'))) && 'true' || 'false' }}"
@@ -2645,7 +2645,11 @@ jobs:
26452645
set -euo pipefail
26462646
hot_intake_shards="$(pnpm run --silent workflow -- limit review_shards.hot_intake_default)"
26472647
normal_shards="$(pnpm run --silent workflow -- limit review_shards.normal_default)"
2648-
runs_json="$(gh run list --repo "${{ github.repository }}" --limit 100 --json workflowName,displayTitle,status,createdAt,updatedAt)"
2648+
runs_json="$({
2649+
gh api "repos/${{ github.repository }}/actions/runs?per_page=100" \
2650+
--jq '.workflow_runs[] | {databaseId:.id, workflowPath:.path, displayTitle:.display_title, status:.status, createdAt:.created_at, updatedAt:.updated_at}' 2>/dev/null \
2651+
|| true
2652+
} | jq -s 'unique_by(.databaseId)')"
26492653
eval "$(
26502654
RUNS_JSON="$runs_json" node <<'NODE'
26512655
const runs = JSON.parse(process.env.RUNS_JSON || "[]");
@@ -2661,7 +2665,7 @@ jobs:
26612665
}
26622666
function recent(title, windowMs) {
26632667
return runs.some((run) => {
2664-
if (run.workflowName !== "ClawSweeper") return false;
2668+
if (run.workflowPath !== ".github/workflows/sweep.yml") return false;
26652669
if (run.displayTitle !== title) return false;
26662670
if (activeRun(run)) return true;
26672671
const createdAt = Date.parse(String(run.createdAt || ""));

‎test/sweep-workflow.test.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ test("review capacity probes use REST actions run listing", () => {
608608
assert.match(block, /actions\/runs\?per_page=100/);
609609
assert.match(block, /--paginate/);
610610
assert.match(block, /status=\$\{run_status\}/);
611-
assert.match(block, /workflowName:\.name/);
611+
assert.match(block, /workflowPath:\.path/);
612+
assert.doesNotMatch(block, /workflowName:\.name/);
612613
assert.match(block, /displayTitle:\.display_title/);
613614
assert.match(block, /createdAt:\.created_at/);
614615
assert.match(block, /updatedAt:\.updated_at/);
@@ -634,6 +635,10 @@ test("background review capacity reserves expanding matrices and caps broad manu
634635
assert.match(modeBlock, /limit review_shards\.normal_default/);
635636
assert.match(modeBlock, /STALE_QUEUED_CUTOFF/);
636637
assert.match(modeBlock, /updatedAt:\.updated_at/);
638+
assert.match(modeBlock, /workflowPath == "\.github\/workflows\/sweep\.yml"/);
639+
assert.match(modeBlock, /WORKFLOW_PATH="\$1"/);
640+
assert.doesNotMatch(modeBlock, /workflowName == "ClawSweeper"/);
641+
assert.doesNotMatch(modeBlock, /WORKFLOW_NAME="\$1"/);
637642
assert.match(modeBlock, /total_shards/);
638643
assert.match(modeBlock, /completed shard jobs are publishing and consume no/);
639644
assert.match(modeBlock, /\[ "\$active_shards" -lt 1 \] && \[ "\$total_shards" -lt 1 \]/);
@@ -644,6 +649,21 @@ test("background review capacity reserves expanding matrices and caps broad manu
644649
assert.match(commitBlock, /limit review_shards\.normal_default/);
645650
assert.match(commitBlock, /STALE_QUEUED_CUTOFF/);
646651
assert.match(commitBlock, /updatedAt:\.updated_at/);
652+
assert.match(commitBlock, /workflowPath == "\.github\/workflows\/sweep\.yml"/);
653+
assert.match(commitBlock, /WORKFLOW_PATH="\$1"/);
654+
assert.doesNotMatch(commitBlock, /workflowName == "ClawSweeper"/);
655+
assert.doesNotMatch(commitBlock, /WORKFLOW_NAME="\$1"/);
656+
});
657+
658+
test("review backstops identify sweep runs by stable workflow path", () => {
659+
const workflow = readText(".github/workflows/sweep.yml");
660+
const block = workflow.slice(workflow.indexOf("- name: Queue review backstops"));
661+
662+
assert.match(block, /actions\/runs\?per_page=100/);
663+
assert.match(block, /workflowPath:\.path/);
664+
assert.match(block, /run\.workflowPath !== "\.github\/workflows\/sweep\.yml"/);
665+
assert.doesNotMatch(block, /gh run list/);
666+
assert.doesNotMatch(block, /run\.workflowName/);
647667
});
648668

649669
test("scheduled background reviews serialize planners and refill released capacity", () => {

0 commit comments

Comments
 (0)