From 9e34019f87468da032afe4dbadba11866149612c Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:49:06 -0500 Subject: [PATCH 1/5] fix(work-items): hand back on an outranked release and judge hand-back leases at a clock read before the list A release refused because an earlier live lease outranks the handle now supersedes its own lease and hands the assignee back before exiting 7, as a claim step 4 loss does; before, the live lease and its login stayed until the TTL (rule NoStrandedAssignee). The assignee hand-back reads the clock before each of its three lease lists and passes it to _wit_login_holds_other, so a same-login lease renewed after the list is never judged expired on its stale listed body (rule HolderAssigned). Refs: #16505 Refs: #16458 Co-Authored-By: Claude Opus 5.5 --- ...ms-release-outranked-hand-back-dac20e43.md | 8 +++ .../tools/work-item-tracker/CONTRACT.md | 13 ++-- .../adapters/github/common.sh | 19 +++--- .../github/lease-coordination.test.sh | 68 ++++++++++++++++--- .../adapters/github/release.sh | 2 + 5 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 .changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md diff --git a/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md b/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md new file mode 100644 index 00000000000..9cb03971a11 --- /dev/null +++ b/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md @@ -0,0 +1,8 @@ +--- +bump: patch +--- + +### Fixed + +- The GitHub adapter's `release` of a live lease that an earlier live lease outranks supersedes its own lease and hands the assignee back before it exits `7`. Before, it wrote nothing, so the lease and its login stayed until the TTL ran out. +- The assignee hand-back judges each lease list at a clock read taken before that list. Before, it read the clock after the list, so a same-login lease renewed meanwhile could look expired and its holder was unassigned. diff --git a/plugins/work-items/tools/work-item-tracker/CONTRACT.md b/plugins/work-items/tools/work-item-tracker/CONTRACT.md index 02922c952ad..954bba2c1b3 100644 --- a/plugins/work-items/tools/work-item-tracker/CONTRACT.md +++ b/plugins/work-items/tools/work-item-tracker/CONTRACT.md @@ -743,7 +743,8 @@ Claim sequence (race-safe, same-identity aware): comment, the foreign lease wins → supersede own comment, exit `7`. On that loss, on the step 2 back-off, and on any failure from step 1 until the emit, the GitHub adapter runs the **hand-back** after superseding its own lease. Each of its lease lists retries once before it - counts as failed. It lists the leases (a failed list counts as empty) and stops if one shows a + counts as failed, and each judges liveness at a clock read taken before that list, since a + lease live then was live when listed and a renew only extends it. It lists the leases (a failed list counts as empty) and stops if one shows a live lease of the login other than its own; else it reads the assignees and stops only if that read succeeds without the login; else it removes the login, re-lists the leases (stopping on a failed list), and assigns the login again if a live lease of it other than its own has @@ -769,10 +770,12 @@ that is all of: on the addressed item, held by the authenticated login (or the ` which, like `claim`'s, replaces the `gh api user` lookup and is asserted by the caller; the comment edit still needs the token's write access), and, for a live lease, not outranked by an earlier live lease under claim step 4 (the completed-run rule included); a newer live lease is a claimant -that has not backed off yet and does not stop the release. Anything else is refused with exit `7` -and no lease is written: a cross-item handle, another login's lease, a live handle an earlier -live lease outranks, or an expired handle under a newer non-superseded lease. Only that last -refusal edits anything: it runs the hand-back below, since its lease no longer holds the item. The login check +that has not backed off yet and does not stop the release. Anything else is refused with exit `7`: +a cross-item handle, another login's lease, a live handle an earlier live lease outranks, or an +expired handle under a newer non-superseded lease. The first two write nothing. An outranked live +handle is superseded (best-effort, as a claim step 4 loss), then its holder's assignee goes +through the hand-back below; an expired handle under a newer lease runs the hand-back alone. Either +way its lease no longer holds the item. The login check exists because lease handles are public (a GitHub comment id), and within one login the handle tells sessions apart, as for `renew-lease`. An already-superseded or expired lease is an idempotent no-op (`released: false`, exit `0`), because it already blocks nobody. A live lease is diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh index d4cb10e46c8..64d4347f44b 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh @@ -502,30 +502,33 @@ wit_lease_released_by_run() { # the assignee before it re-lists, so one of the two sees the other. A failed assignee read # still removes, since an assignee that no lease holds keeps the item off the frontier for good. wit_hand_back_assignee() { - local owner="$1" repo="$2" number="$3" login="$4" own="$5" leases + local owner="$1" repo="$2" number="$3" login="$4" own="$5" leases now + now="$(date -u +%s)" leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || leases='[]' - _wit_login_holds_other "$leases" "$login" "$own" && return 0 + _wit_login_holds_other "$leases" "$login" "$own" "$now" && return 0 if wit_try_read_assignees "$owner" "$repo" "$number"; then jq -e --arg l "$login" 'any(.[]; . == $l)' <<<"$WIT_GH_OUT" >/dev/null || return 0 fi wit_try_remove_assignee "$owner" "$repo" "$number" "$login" + now="$(date -u +%s)" leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || return 0 - _wit_login_holds_other "$leases" "$login" "$own" || return 0 + _wit_login_holds_other "$leases" "$login" "$own" "$now" || return 0 wit_try_add_assignee "$owner" "$repo" "$number" "$login" # The lease that earned the re-add may have ended meanwhile (a renew that revived it for a # moment backs off and finds the login already gone), so the re-add must not outlive it. + now="$(date -u +%s)" leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || return 0 - _wit_login_holds_other "$leases" "$login" "$own" || wit_try_remove_assignee "$owner" "$repo" "$number" "$login" + _wit_login_holds_other "$leases" "$login" "$own" "$now" || wit_try_remove_assignee "$owner" "$repo" "$number" "$login" } -# _wit_login_holds_other : 0 when a live lease other than -# is held by . +# _wit_login_holds_other : 0 when a live lease +# other than is held by at . The caller reads the clock before +# it lists: a body live then was live when listed, and a renew only extends it. _wit_login_holds_other() { - local row cand now - now="$(date -u +%s)" + local row cand now="$4" while IFS= read -r row; do [[ -n "$row" && "$(jq -r '.id' <<<"$row")" != "$3" ]] || continue cand="$(wit_lease_json "$(jq -r '.body' <<<"$row")")" diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh index 324fc1cb748..49a563f1349 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh @@ -408,8 +408,8 @@ assert_not_contains "release does not supersede a foreign lease" "$(calls)" "PAT cleanup_scenario # [outranked] two live leases: the earlier one holds the item (claim step 4) and the -# newer one is a claimant that has not backed off. The newer handle is refused; the -# earlier one is released. +# newer one is a claimant that has not backed off. The newer handle is refused and +# superseded; the earlier one is released. two_live_leases() { jq -cn --arg o "$(marker alice "$FUTURE" 24)" --arg n "$(marker alice "$FUTURE" 24)" \ '[{id:100, node_id:"MDEx", body:$o, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, @@ -422,7 +422,8 @@ two_live_leases rc=0 bash "$RELEASE" "$ID" --lease-comment-id 200 >/dev/null 2>&1 || rc=$? assert_eq "release refuses (7) a handle outranked by an earlier live lease" "7" "$rc" -assert_not_contains "release writes nothing for an outranked handle" "$(calls)" "PATCH" +assert_contains "release supersedes an outranked handle" "$(calls)" "PATCH 200" +assert_not_contains "release leaves the earlier live lease alone" "$(calls)" "PATCH 100" cleanup_scenario new_scenario printf 'alice\n' >"$GH_STUB_DIR/login" @@ -1111,15 +1112,28 @@ refused_release_case() { refused_release_case "a refused release under a lane lease unassigns alice" "$LANE" '[]' refused_release_case "a refused release under alice's own newer lease keeps her" alice '["alice"]' -# [release clock] the lease expires between two clock reads; release decides on one read, -# so it releases the lease that was live at that read. -new_scenario -: >"$GH_STUB_DIR/stateful" -printf 'alice\n' >"$GH_STUB_DIR/login" -printf '["alice"]\n' >"$GH_STUB_DIR/assignees" +# [outranked live release] alice's live lease 2 is outranked by an earlier live lease 1: the +# release is refused (7), supersedes lease 2 and hands back alice unless lease 1 is alice's own. +outranked_release_case() { + new_scenario + : >"$GH_STUB_DIR/stateful" + printf 'alice\n' >"$GH_STUB_DIR/login" + printf '%s\n' "$3" >"$GH_STUB_DIR/assignees" + jq -cn --arg a "$(marker "$2" "$FUTURE" 24)" --arg b "$(marker alice "$FUTURE" 24)" \ + '[{id:1, node_id:"MDEx", body:$a, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, + {id:2, node_id:"MDEx", body:$b, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}]' >"$GH_STUB_DIR/lease-comments" + rc=0 + bash "$RELEASE" "$ID" --lease-comment-id 2 >/dev/null 2>&1 || rc=$? + assert_eq "$1: exit" "7" "$rc" + assert_eq "$1: own lease 2 superseded" "true" "$(superseded 2)" + assert_eq "$1: earlier lease 1 left live" "false" "$(superseded 1)" + assert_eq "$1: assignees" "$4" "$(assignees_now)" + cleanup_scenario +} +outranked_release_case "an outranked release under bob's earlier lease unassigns alice" bob '["bob","alice"]' '["bob"]' +outranked_release_case "an outranked release under alice's own earlier lease keeps her" alice '["alice"]' '["alice"]' + T0=1783828800 -lease_array 1 "$(marker alice "2026-07-12T04:00:00Z" 1)" >"$GH_STUB_DIR/lease-comments" -printf '%s\n' "$((T0 + 3599))" "$((T0 + 3600))" >"$GH_STUB_DIR/clock" # date: `date -u +%s` answers the next line of $GH_STUB_DIR/clock, repeating the last. # shellcheck disable=SC2329 # exported to the release.sh child date() { @@ -1132,6 +1146,38 @@ date() { fi } export -f date + +# [hand-back clock] releasing lease 1, the hand-back lists alice's lease 2 (old expiry +# T0+3600); a same-login renew then extends it and the clock passes that old expiry before +# the judgment. Lease 2 was live at a clock read before the list, so alice is never removed. +new_scenario +: >"$GH_STUB_DIR/stateful" +printf 'alice\n' >"$GH_STUB_DIR/login" +printf '["alice"]\n' >"$GH_STUB_DIR/assignees" +jq -cn --arg a "$(marker alice "2026-07-12T04:00:00Z" 24)" --arg b "$(marker alice "2026-07-12T04:00:00Z" 1)" \ + '[{id:1, node_id:"MDEx", body:$a, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, + {id:2, node_id:"MDEx", body:$b, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}]' >"$GH_STUB_DIR/lease-comments" +printf '%s\n' "$((T0 + 10))" >"$GH_STUB_DIR/clock" +h="$(next_read list 2)-after" +# shellcheck disable=SC2016 # expanded by the hook's bash +hook "$h" bash -c 'put_lease 2 "$1" && printf "%s\n" "$2" >"$GH_STUB_DIR/clock"' _ \ + "$(marker alice "2026-07-12T05:00:00Z" 1)" "$((T0 + 3600))" +rc=0 +bash "$RELEASE" "$ID" --lease-comment-id 1 >/dev/null 2>&1 || rc=$? +assert_eq "the release of lease 1 succeeds (0)" "0" "$rc" +assert_eq "the renew landed after the hand-back's list" "0" "$(hook_rc "$h")" +assert_not_contains "the hand-back never removes alice for renewed lease 2" "$(calls)" "REMOVE_ASSIGNEE alice" +assert_eq "alice stays assigned" '["alice"]' "$(assignees_now)" +cleanup_scenario + +# [release clock] the lease expires between two clock reads; release decides on one read, +# so it releases the lease that was live at that read. +new_scenario +: >"$GH_STUB_DIR/stateful" +printf 'alice\n' >"$GH_STUB_DIR/login" +printf '["alice"]\n' >"$GH_STUB_DIR/assignees" +lease_array 1 "$(marker alice "2026-07-12T04:00:00Z" 1)" >"$GH_STUB_DIR/lease-comments" +printf '%s\n' "$((T0 + 3599))" "$((T0 + 3600))" >"$GH_STUB_DIR/clock" rc=0 bash "$RELEASE" "$ID" --lease-comment-id 1 >/dev/null 2>&1 || rc=$? assert_eq "release of a lease live at its clock read succeeds (0)" "0" "$rc" diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh index 9d2a3a18560..a2eabf75c05 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh @@ -111,6 +111,8 @@ fi # that has not backed off yet and does not stop the holder's release. wit_lease_winner "$leases" "$now_epoch" "$lease_comment_id" wit_lease_released_by_run if [[ "$WIT_WINNER_ID" != "$lease_comment_id" ]]; then + wit_try_supersede_lease_comment "$owner" "$repo" "$lease_comment_id" "$lease_json" + wit_hand_back_assignee "$owner" "$repo" "$number" "$login" "$lease_comment_id" printf 'release: comment %s is outranked by the earlier live lease, comment %s\n' \ "$lease_comment_id" "${WIT_WINNER_ID:-none}" >&2 exit "$EX_CONFLICT" From 4cae0ac6e68b2624c365d50539c77ec61fc4be0c Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:17:29 -0500 Subject: [PATCH 2/5] fix(work-items): hand back on an outranked release only after its supersede The outranked release refusal now backs off as renew-lease's back-off does: it tries the supersede twice and hands the assignee back only after one try succeeded, else keeps the assignee and says so; exit 7 either way. The hand-back reads the clock before each list attempt, retries included, through _wit_list_at_clock. CONTRACT.md and the comment state the property that holds: a renew only moves an expiry later, so a renewing holder never looks expired, and a lease expiring during the list still counts as live. Refs: #16505 Co-Authored-By: Claude Opus 5.5 --- ...ms-release-outranked-hand-back-dac20e43.md | 4 +- .../tools/work-item-tracker/CONTRACT.md | 12 +++-- .../adapters/github/common.sh | 24 +++++----- .../github/lease-coordination.test.sh | 47 +++++++++++++++++++ .../adapters/github/release.sh | 13 +++-- 5 files changed, 78 insertions(+), 22 deletions(-) diff --git a/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md b/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md index 9cb03971a11..145d302cc41 100644 --- a/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md +++ b/.changes/work-items/fix-work-items-release-outranked-hand-back-dac20e43.md @@ -4,5 +4,5 @@ bump: patch ### Fixed -- The GitHub adapter's `release` of a live lease that an earlier live lease outranks supersedes its own lease and hands the assignee back before it exits `7`. Before, it wrote nothing, so the lease and its login stayed until the TTL ran out. -- The assignee hand-back judges each lease list at a clock read taken before that list. Before, it read the clock after the list, so a same-login lease renewed meanwhile could look expired and its holder was unassigned. +- The GitHub adapter's `release` of a live lease that an earlier live lease outranks backs off as `renew-lease` does before it exits `7`: it tries to supersede its own lease twice and hands the assignee back once one try succeeds, else keeps the assignee and says so. Before, it wrote nothing, so the lease and its login stayed until the TTL ran out. +- The assignee hand-back judges each lease list attempt at a clock read taken just before it. Before, it read the clock after the list, so a same-login lease renewed meanwhile could look expired and its holder was unassigned. diff --git a/plugins/work-items/tools/work-item-tracker/CONTRACT.md b/plugins/work-items/tools/work-item-tracker/CONTRACT.md index f5343be7e0b..3546eec3403 100644 --- a/plugins/work-items/tools/work-item-tracker/CONTRACT.md +++ b/plugins/work-items/tools/work-item-tracker/CONTRACT.md @@ -749,8 +749,9 @@ Claim sequence (race-safe, same-identity aware): comment, the foreign lease wins → supersede own comment, exit `7`. On that loss, on the step 2 back-off, and on any failure from step 1 until the emit, the GitHub adapter runs the **hand-back** after superseding its own lease. Each of its lease lists retries once before it - counts as failed, and each judges liveness at a clock read taken before that list, since a - lease live then was live when listed and a renew only extends it. It lists the leases (a failed list counts as empty) and stops if one shows a + counts as failed, and each attempt judges liveness at a clock read taken just before it: a + renew only moves an expiry later, so a renewing holder never looks expired, while a lease that + expires during the list still counts as live and its login then stays for reclaim. It lists the leases (a failed list counts as empty) and stops if one shows a live lease of the login other than its own; else it reads the assignees and stops only if that read succeeds without the login; else it removes the login, re-lists the leases (stopping on a failed list), and assigns the login again if a live lease of it other than its own has @@ -783,9 +784,10 @@ live lease under claim step 4 (the completed-run rule included); a newer live le that has not backed off yet and does not stop the release. Anything else is refused with exit `7`: a cross-item handle, another login's lease, a live handle an earlier live lease outranks, or an expired handle under a newer non-superseded lease. The first two write nothing. An outranked live -handle is superseded (best-effort, as a claim step 4 loss), then its holder's assignee goes -through the hand-back below; an expired handle under a newer lease runs the hand-back alone. Either -way its lease no longer holds the item. The login check +handle backs off as `renew-lease`'s back-off does: it tries the supersede twice and runs the +hand-back below only after one succeeded, since a lease it could not supersede may still be live, +so the holder stays assigned and the message says so. An expired handle under a newer lease runs +the hand-back alone, since its lease no longer holds the item. The login check exists because lease handles are public (a GitHub comment id), and within one login the handle tells sessions apart, as for `renew-lease`. An already-superseded or expired lease is an idempotent no-op (`released: false`, exit `0`), because it already blocks nobody; an diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh index 327423502e9..a1a08cbf23e 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh @@ -505,30 +505,32 @@ wit_lease_released_by_run() { # still removes, since an assignee that no lease holds keeps the item off the frontier for good. wit_hand_back_assignee() { local owner="$1" repo="$2" number="$3" login="$4" own="$5" leases now - now="$(date -u +%s)" - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || leases='[]' + _wit_list_at_clock "$owner" "$repo" "$number" || _wit_list_at_clock "$owner" "$repo" "$number" || leases='[]' _wit_login_holds_other "$leases" "$login" "$own" "$now" && return 0 if wit_try_read_assignees "$owner" "$repo" "$number"; then jq -e --arg l "$login" 'any(.[]; . == $l)' <<<"$WIT_GH_OUT" >/dev/null || return 0 fi wit_try_remove_assignee "$owner" "$repo" "$number" "$login" - now="$(date -u +%s)" - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || return 0 + _wit_list_at_clock "$owner" "$repo" "$number" || _wit_list_at_clock "$owner" "$repo" "$number" || return 0 _wit_login_holds_other "$leases" "$login" "$own" "$now" || return 0 wit_try_add_assignee "$owner" "$repo" "$number" "$login" # The lease that earned the re-add may have ended meanwhile (a renew that revived it for a # moment backs off and finds the login already gone), so the re-add must not outlive it. - now="$(date -u +%s)" - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || - leases="$(wit_list_lease_comments "$owner" "$repo" "$number")" || return 0 + _wit_list_at_clock "$owner" "$repo" "$number" || _wit_list_at_clock "$owner" "$repo" "$number" || return 0 _wit_login_holds_other "$leases" "$login" "$own" "$now" || wit_try_remove_assignee "$owner" "$repo" "$number" "$login" } +# _wit_list_at_clock : set the caller's now to a clock read, then its +# leases to a lease list taken after it. A renew only moves an expiry later, so judging the +# listed bodies at that earlier read never makes a renewing holder look expired; a lease that +# expires during the list still counts as live, and its login then stays for reclaim. +_wit_list_at_clock() { + now="$(date -u +%s)" + leases="$(wit_list_lease_comments "$1" "$2" "$3")" +} + # _wit_login_holds_other : 0 when a live lease -# other than is held by at . The caller reads the clock before -# it lists: a body live then was live when listed, and a renew only extends it. +# other than is held by at . _wit_login_holds_other() { local row cand now="$4" while IFS= read -r row; do diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh index bc3a7a01c5c..86ecd8f76a3 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh @@ -1139,6 +1139,26 @@ outranked_release_case() { outranked_release_case "an outranked release under bob's earlier lease unassigns alice" bob '["bob","alice"]' '["bob"]' outranked_release_case "an outranked release under alice's own earlier lease keeps her" alice '["alice"]' '["alice"]' +# [outranked release, supersede fails twice] lease 2 may still be live, so alice stays. +new_scenario +: >"$GH_STUB_DIR/stateful" +printf 'alice\n' >"$GH_STUB_DIR/login" +printf '["bob","alice"]\n' >"$GH_STUB_DIR/assignees" +jq -cn --arg a "$(marker bob "$FUTURE" 24)" --arg b "$(marker alice "$FUTURE" 24)" \ + '[{id:1, node_id:"MDEx", body:$a, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, + {id:2, node_id:"MDEx", body:$b, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}]' >"$GH_STUB_DIR/lease-comments" +: >"$GH_STUB_DIR/patch-lost-1" +: >"$GH_STUB_DIR/patch-lost-2" +rc=0 +err="$(bash "$RELEASE" "$ID" --lease-comment-id 2 2>&1 >/dev/null)" || rc=$? +assert_eq "an outranked release whose supersede fails twice exits conflict (7)" "7" "$rc" +assert_eq "that release tries the supersede twice" "2" "$(grep -c '^PATCH 2' "$GH_STUB_DIR/calls.log")" +assert_eq "lease 2 is not superseded" "false" "$(superseded 2)" +assert_not_contains "that release never removes alice" "$(calls)" "REMOVE_ASSIGNEE alice" +assert_eq "alice stays assigned for lease 2" '["bob","alice"]' "$(assignees_now)" +assert_contains "the refusal says the assignee is kept" "$err" "could not supersede the lease, assignee kept" +cleanup_scenario + T0=1783828800 # date: `date -u +%s` answers the next line of $GH_STUB_DIR/clock, repeating the last. # shellcheck disable=SC2329 # exported to the release.sh child @@ -1176,6 +1196,33 @@ assert_not_contains "the hand-back never removes alice for renewed lease 2" "$(c assert_eq "alice stays assigned" '["alice"]' "$(assignees_now)" cleanup_scenario +# [hand-back clock, later lists] releasing lease 1, the hand-back's first list shows no +# lease 2, so it removes alice; lease 2 (old expiry T0+3600) is in every later list. On the +# named list a renew extends lease 2 and the clock passes its old expiry before the judgment; +# judged at a clock read before that list, alice ends assigned. +handback_clock_case() { + new_scenario + : >"$GH_STUB_DIR/stateful" + printf 'alice\n' >"$GH_STUB_DIR/login" + printf '["alice"]\n' >"$GH_STUB_DIR/assignees" + jq -cn --arg a "$(marker alice "2026-07-12T04:00:00Z" 24)" --arg b "$(marker alice "2026-07-12T04:00:00Z" 1)" \ + '[{id:1, node_id:"MDEx", body:$a, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, + {id:2, node_id:"MDEx", body:$b, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}]' >"$GH_STUB_DIR/lease-comments" + lease_array 1 "$(superseded_marker alice)" >"$GH_STUB_DIR/lease-comments-2" + printf '%s\n' "$((T0 + 10))" >"$GH_STUB_DIR/clock" + # shellcheck disable=SC2016 # expanded by the hook's bash + hook "list-$2-after" bash -c 'put_lease 2 "$1" && printf "%s\n" "$2" >"$GH_STUB_DIR/clock"' _ \ + "$(marker alice "2026-07-12T05:00:00Z" 1)" "$((T0 + 3600))" + rc=0 + bash "$RELEASE" "$ID" --lease-comment-id 1 >/dev/null 2>&1 || rc=$? + assert_eq "$1: release exit" "0" "$rc" + assert_eq "$1: the renew landed after that list" "0" "$(hook_rc "list-$2-after")" + assert_eq "$1: alice ends assigned" '["alice"]' "$(assignees_now)" + cleanup_scenario +} +handback_clock_case "renew during the hand-back's re-list" 3 +handback_clock_case "renew during the hand-back's confirm list" 4 + # [release clock] the lease expires between two clock reads; release decides on one read, # so it releases the lease that was live at that read. new_scenario diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh index 90b527aec23..7ccc61097a8 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/release.sh @@ -112,10 +112,15 @@ fi # that has not backed off yet and does not stop the holder's release. wit_lease_winner "$leases" "$now_epoch" "$lease_comment_id" wit_lease_released_by_run if [[ "$WIT_WINNER_ID" != "$lease_comment_id" ]]; then - wit_try_supersede_lease_comment "$owner" "$repo" "$lease_comment_id" "$lease_json" - wit_hand_back_assignee "$owner" "$repo" "$number" "$login" "$lease_comment_id" - printf 'release: comment %s is outranked by the earlier live lease, comment %s\n' \ - "$lease_comment_id" "${WIT_WINNER_ID:-none}" >&2 + outcome="lease superseded" + if wit_try_supersede_lease_comment "$owner" "$repo" "$lease_comment_id" "$lease_json" || + wit_try_supersede_lease_comment "$owner" "$repo" "$lease_comment_id" "$lease_json"; then + wit_hand_back_assignee "$owner" "$repo" "$number" "$login" "$lease_comment_id" + else + outcome="could not supersede the lease, assignee kept" + fi + printf 'release: comment %s is outranked by the earlier live lease, comment %s; %s\n' \ + "$lease_comment_id" "${WIT_WINNER_ID:-none}" "$outcome" >&2 exit "$EX_CONFLICT" fi From a46a5a11642ea3f84e9891c15ebc871c0c62ae91 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:35:52 -0500 Subject: [PATCH 3/5] fix(work-items): count a hand-back lease live at either clock read A renew landing during the lease list can carry a renewed_at more than the skew margin ahead of the clock read taken before the list, when the holder's clock runs fast; the lease is then also judged at a read taken after the list. Co-Authored-By: Claude Fable 5.2 --- .../tools/work-item-tracker/adapters/github/common.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh index a1a08cbf23e..c3e20563e6b 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh @@ -530,14 +530,16 @@ _wit_list_at_clock() { } # _wit_login_holds_other : 0 when a live lease -# other than is held by at . +# other than is held by at or at a clock read taken now, after +# the list: a renew that landed during the list can carry a renewed_at too far ahead of the +# earlier read for a holder whose clock runs fast. _wit_login_holds_other() { local row cand now="$4" while IFS= read -r row; do [[ -n "$row" && "$(jq -r '.id' <<<"$row")" != "$3" ]] || continue cand="$(wit_lease_json "$(jq -r '.body' <<<"$row")")" [[ -n "$cand" && "$(jq -r '.holder // empty' <<<"$cand")" == "$2" ]] || continue - wit_lease_is_live "$cand" "$now" && return 0 + { wit_lease_is_live "$cand" "$now" || wit_lease_is_live "$cand" "$(date -u +%s)"; } && return 0 done < <(jq -c '.[]' <<<"$1") return 1 } From 32c4ce3773904ad41842c4d2fc1734353cf27792 Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:36:30 -0500 Subject: [PATCH 4/5] style(work-items): shorten the hand-back liveness comment Co-Authored-By: Claude Fable 5.2 --- .../tools/work-item-tracker/adapters/github/common.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh index c3e20563e6b..2c107a2d9e1 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/common.sh @@ -530,9 +530,7 @@ _wit_list_at_clock() { } # _wit_login_holds_other : 0 when a live lease -# other than is held by at or at a clock read taken now, after -# the list: a renew that landed during the list can carry a renewed_at too far ahead of the -# earlier read for a holder whose clock runs fast. +# other than is held by at or at the current clock. _wit_login_holds_other() { local row cand now="$4" while IFS= read -r row; do From 57b3e270913636ec4ed15280da4616f8e0230ffe Mon Sep 17 00:00:00 2001 From: Kyle Sexton <153232337+kyle-sexton@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:59:19 -0500 Subject: [PATCH 5/5] test(work-items): cover a fast-clock renew during the hand-back's list A same-login holder whose clock runs 300 s ahead renews two seconds into the hand-back's lease list. The case fails when the hand-back judges only at the clock read before the list. CONTRACT.md now states both reads. Co-Authored-By: Claude Fable 5.2 --- .../tools/work-item-tracker/CONTRACT.md | 8 ++++--- .../github/lease-coordination.test.sh | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/plugins/work-items/tools/work-item-tracker/CONTRACT.md b/plugins/work-items/tools/work-item-tracker/CONTRACT.md index 3546eec3403..6a389ef0f95 100644 --- a/plugins/work-items/tools/work-item-tracker/CONTRACT.md +++ b/plugins/work-items/tools/work-item-tracker/CONTRACT.md @@ -749,9 +749,11 @@ Claim sequence (race-safe, same-identity aware): comment, the foreign lease wins → supersede own comment, exit `7`. On that loss, on the step 2 back-off, and on any failure from step 1 until the emit, the GitHub adapter runs the **hand-back** after superseding its own lease. Each of its lease lists retries once before it - counts as failed, and each attempt judges liveness at a clock read taken just before it: a - renew only moves an expiry later, so a renewing holder never looks expired, while a lease that - expires during the list still counts as live and its login then stays for reclaim. It lists the leases (a failed list counts as empty) and stops if one shows a + counts as failed, and each attempt counts a lease live when it is live at a clock read taken + just before the list or at one taken after it: a renew only moves an expiry later, so a + renewing holder never looks expired at the earlier read, a holder whose clock runs up to the + skew bound ahead and renews during the list is inside that bound at the later read, and a + lease that expires during the list still counts as live, so its login stays for reclaim. It lists the leases (a failed list counts as empty) and stops if one shows a live lease of the login other than its own; else it reads the assignees and stops only if that read succeeds without the login; else it removes the login, re-lists the leases (stopping on a failed list), and assigns the login again if a live lease of it other than its own has diff --git a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh index 86ecd8f76a3..a7cd760ee82 100755 --- a/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh +++ b/plugins/work-items/tools/work-item-tracker/adapters/github/lease-coordination.test.sh @@ -1223,6 +1223,30 @@ handback_clock_case() { handback_clock_case "renew during the hand-back's re-list" 3 handback_clock_case "renew during the hand-back's confirm list" 4 +# [hand-back clock, fast holder] releasing lease 1, the hand-back reads its clock (T0+10), +# then a same-login holder whose clock runs 300 s fast renews lease 2 two seconds into the +# list (renewed_at T0+312). That is past the skew bound at the earlier read and inside it at +# a read after the list, so alice is never removed. +new_scenario +: >"$GH_STUB_DIR/stateful" +printf 'alice\n' >"$GH_STUB_DIR/login" +printf '["alice"]\n' >"$GH_STUB_DIR/assignees" +jq -cn --arg a "$(marker alice "2026-07-12T04:00:00Z" 24)" --arg b "$(marker alice "2026-07-12T04:00:00Z" 1)" \ + '[{id:1, node_id:"MDEx", body:$a, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}, + {id:2, node_id:"MDEx", body:$b, created_at:"2020-01-01T00:00:00Z", author_association:"MEMBER"}]' >"$GH_STUB_DIR/lease-comments" +printf '%s\n' "$((T0 + 10))" >"$GH_STUB_DIR/clock" +h="$(next_read list 2)" +# shellcheck disable=SC2016 # expanded by the hook's bash +hook "$h" bash -c 'put_lease 2 "$1" && printf "%s\n" "$2" >"$GH_STUB_DIR/clock"' _ \ + "$(marker alice "2026-07-12T04:05:12Z" 1)" "$((T0 + 12))" +rc=0 +bash "$RELEASE" "$ID" --lease-comment-id 1 >/dev/null 2>&1 || rc=$? +assert_eq "the release of lease 1 succeeds (0) beside a fast-clock holder" "0" "$rc" +assert_eq "the fast-clock renew landed during the hand-back's list" "0" "$(hook_rc "$h")" +assert_not_contains "the hand-back never removes alice for the fast-clock lease 2" "$(calls)" "REMOVE_ASSIGNEE alice" +assert_eq "alice stays assigned beside the fast-clock holder" '["alice"]' "$(assignees_now)" +cleanup_scenario + # [release clock] the lease expires between two clock reads; release decides on one read, # so it releases the lease that was live at that read. new_scenario