Skip to content

Commit bc98875

Browse files
committed
fix(drift-sync): keep one rolling needs-human PR instead of a sibling per changeset
The changeset key dedups an identical family set only. While a decision waited, every new or vanished family minted a new key, and the needs-human step opened a new PR beside the one still open: #406/#409/#411/#412 were open together (2026-09-02..09-08), and #475/#477/#481/#483/#484 (2026-09-22..10-02). Now a run whose set differs is written onto the open rolling PR: its commit is force-pushed (leased to the head that was inspected) onto that PR's branch, and the body is rewritten with this run's markers and notes. A new rolling PR lives on drift-needs-human/rolling-<date>-<run id>, a branch with no changeset key, because a key there would go stale once later runs rewrite the content. The rolling PR is rewritten only when it is still open, was opened by the drift bot (app/copilotkit-bot), and every commit on it is the bot's own. A human's commit leaves it alone and opens no sibling. A PR that cannot be read fails closed. The tests replay two runs through the real step body: run 1's pushed branch and created body become run 2's open PR.
1 parent 87979d6 commit bc98875

2 files changed

Lines changed: 349 additions & 69 deletions

File tree

‎.github/workflows/fix-drift.yml‎

Lines changed: 157 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,15 @@ jobs:
949949
# SECONDARY — skip when an open PR already proposes one of this run's note
950950
# files, matched by the per-note body marker. The Slack alert below still
951951
# fires either way.
952+
#
953+
# ONE ROLLING PR. The changeset key dedups an IDENTICAL family set only, so
954+
# while a decision waited, every new or vanished family minted a new key and
955+
# a new sibling PR beside the open one (#406..#412 and #475..#484 were each
956+
# open together). So a run whose set differs is written onto the open
957+
# ROLLING PR (force-push with lease, then a body rewrite) instead of opening
958+
# another. That PR lives on `drift-needs-human/rolling-<date>-<run id>`, a
959+
# branch with NO changeset key: its content changes, so a key in its name
960+
# would go stale.
952961
- name: Persist needs-human note + open PR
953962
id: needs_human_pr
954963
if: steps.sync.outputs.reason == 'needs-human' && success()
@@ -957,6 +966,9 @@ jobs:
957966
BASE_SHA: ${{ steps.gitcfg.outputs.base_sha }}
958967
RUN_ID: ${{ github.run_id }}
959968
CHANGESET_KEY: ${{ steps.sync.outputs.changeset_key }}
969+
# The login gh reports for every PR the app token above opens (app slug
970+
# `copilotkit-bot`; checked on #337..#541). The rolling-PR guard reads it.
971+
DRIFT_BOT_LOGIN: app/copilotkit-bot
960972
run: |
961973
set -euo pipefail
962974
# ---- RECORD WHAT ACTUALLY HAPPENS IN HERE ----------------------------
@@ -1515,6 +1527,144 @@ jobs:
15151527
exit 0
15161528
fi
15171529
1530+
# ---- THE ONE BODY WRITER ------------------------------------------------
1531+
# Used to OPEN the rolling PR and to REWRITE it in place, so both carry
1532+
# exactly this run's markers and notes.
1533+
PR_BODY_FILE="${RUNNER_TEMP}/drift-needs-human-pr-body.md"
1534+
PR_TITLE="chore(drift-sync): needs-human model-family decision ($(date +%Y-%m-%d))"
1535+
write_nh_body() {
1536+
{
1537+
echo "## Needs a human decision (drift-sync)"
1538+
echo ""
1539+
echo "The deterministic, zero-LLM drift-sync found a model-family change it must"
1540+
echo "NOT auto-apply (a genuinely new/unclassified family, or a registry"
1541+
echo "structural mismatch). It wrote the note file(s) below and opened this PR so"
1542+
echo "the decision is REACHABLE in the repo."
1543+
echo ""
1544+
echo "This is the ONE open needs-human PR. A later run whose family set differs"
1545+
echo "rewrites this PR's branch and this body in place instead of opening another."
1546+
echo "A commit by anyone but the drift bot on this branch stops those rewrites."
1547+
echo ""
1548+
echo "> **Not auto-merged — a human decides, and applies it by hand.**"
1549+
echo "> For a *new-family* note, DO NOT expect merging this PR to apply anything:"
1550+
echo "> \`includeFamilies\` is checksum-pinned, and adding a family moves that pin,"
1551+
echo "> which reds the freeze test gate-2 re-runs. drift-sync cannot re-pin — the"
1552+
echo "> gate-1 allowlist permits only \`model-registry.ts\` and \`drift-proposals/\`."
1553+
echo "> So a follow-up run would report \`gate-failed\`, not \`ok-applied\`."
1554+
echo ">"
1555+
echo "> The procedure that actually works, in ONE commit: add the family to"
1556+
echo "> \`includeFamilies\` (or \`excludeFamilies\` to reject), re-pin BOTH checksums,"
1557+
echo "> resolve the note file(s) below, and update the offline \`/models\` wave."
1558+
echo "> Then close this PR. Prior examples: 72f85f8, 936b59c."
1559+
echo ""
1560+
echo "### Note file(s) in this PR"
1561+
echo ""
1562+
for note in "${NOTES[@]:-}"; do
1563+
[ -n "$note" ] || continue
1564+
echo "- \`${note}\`"
1565+
done
1566+
echo ""
1567+
# Emitted from the SINGLE MARKERS array built at the top of this step
1568+
# (the PRIMARY changeset marker plus one per note), so the set written
1569+
# here is by construction the set the marker self-heal re-asserts and
1570+
# the set the guards above match.
1571+
for m in "${MARKERS[@]}"; do
1572+
echo "<!-- ${m} -->"
1573+
done
1574+
echo ""
1575+
echo "### drift-sync outcome"
1576+
echo ""
1577+
echo '```'
1578+
cat "${RUNNER_TEMP}/drift-sync.log" 2>/dev/null || echo "(see drift-sync-log artifact)"
1579+
echo '```'
1580+
} > "$1"
1581+
}
1582+
1583+
# ---- THE ROLLING PR: UPDATE IT, DO NOT OPEN A SIBLING --------------------
1584+
# Reached only after DUP (no open PR carries this key), REJECTED (no human
1585+
# closed this key) and the no-new-commit return have all declined, so this
1586+
# run's set is un-proposed. If the bot's rolling PR is open, this run's
1587+
# commit goes onto ITS branch and its body is rewritten. Read from UNMERGED:
1588+
# the listing audited complete above, which holds every OPEN PR. The OLDEST
1589+
# open rolling PR opened by DRIFT_BOT_LOGIN is the one.
1590+
if [ -z "${DRIFT_BOT_LOGIN:-}" ]; then
1591+
echo "::error::DRIFT_BOT_LOGIN is not set, so this run cannot tell the drift bot's rolling PR from a human's — refusing to either rewrite one or open a sibling beside it."
1592+
exit 1
1593+
fi
1594+
ROLLING_ERR="${RUNNER_TEMP}/drift-rolling-select.err"
1595+
if ! ROLLING="$(printf '%s' "$UNMERGED" | jq -c '
1596+
[ .[]
1597+
| select(.state == "OPEN"
1598+
and ((.headRefName // "") | test("^drift-needs-human/rolling-[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]+$"))
1599+
and (((.author.login // "") | ascii_downcase) == ($ENV.DRIFT_BOT_LOGIN | ascii_downcase))) ]
1600+
| sort_by(.number) | .[0] // empty' 2>"$ROLLING_ERR")"; then
1601+
echo "::error::selecting the open rolling needs-human PR FAILED, so this run cannot tell whether to update it instead of opening another — refusing to risk a duplicate: $(cat "$ROLLING_ERR" 2>/dev/null)"
1602+
exit 1
1603+
fi
1604+
if [ -n "$ROLLING" ]; then
1605+
R_NUMBER="$(printf '%s' "$ROLLING" | jq -r '.number')"
1606+
R_URL="$(printf '%s' "$ROLLING" | jq -r '.url')"
1607+
R_BRANCH="$(printf '%s' "$ROLLING" | jq -r '.headRefName')"
1608+
# THE GUARD, read LIVE: still OPEN, opened by the drift bot, on its rolling
1609+
# branch, and EVERY commit on it made by the bot's own git identity (the
1610+
# gitcfg step's). A commit by anyone else means a human is working on it,
1611+
# and force-pushing would destroy that work. Anything this run cannot read
1612+
# fails CLOSED: neither a force-push nor a sibling.
1613+
BOT_EMAIL="$(git config user.email || true)"
1614+
if [ -z "$BOT_EMAIL" ]; then
1615+
echo "::error::no git user.email is configured, so the drift bot's commits on PR #${R_NUMBER} cannot be told from a human's — refusing to rewrite it or open a sibling."
1616+
exit 1
1617+
fi
1618+
if ! R_VIEW="$(gh pr view "$R_NUMBER" --json number,state,author,headRefName,headRefOid,commits 2>&1)"; then
1619+
echo "::error::open rolling PR #${R_NUMBER} could not be read (${R_VIEW}), so this run neither force-pushes over it nor opens a sibling beside it — refusing."
1620+
exit 1
1621+
fi
1622+
if ! R_VERDICT="$(printf '%s' "$R_VIEW" | jq -r --arg e "$BOT_EMAIL" --arg b "$R_BRANCH" '
1623+
if ((.commits | type) != "array") or ((.commits | length) == 0)
1624+
or ((.headRefOid // "") == "")
1625+
then "unknown:it came back with no commit list or no head commit"
1626+
elif .state != "OPEN" then "refuse:it is \(.state), not OPEN"
1627+
elif ((.author.login // "") | ascii_downcase) != ($ENV.DRIFT_BOT_LOGIN | ascii_downcase)
1628+
then "refuse:it was opened by \(.author.login // "nobody"), not by the drift bot"
1629+
elif (.headRefName // "") != $b then "refuse:its branch is now \(.headRefName // "")"
1630+
elif any(.commits[]; ((.authors // []) | length) == 0
1631+
or any((.authors // [])[]; (.email // "") != $e))
1632+
then "refuse:it carries a commit NOT made by the drift bot, so a human is working on it"
1633+
else "ok" end' 2>&1)"; then
1634+
echo "::error::open rolling PR #${R_NUMBER} came back unusable (${R_VERDICT}) — refusing to rewrite it or open a sibling."
1635+
exit 1
1636+
fi
1637+
case "$R_VERDICT" in
1638+
ok) ;;
1639+
refuse:*)
1640+
# No sibling either: that is the duplicate this exists to prevent. The
1641+
# decision waits until a human merges or closes that PR, so the alert
1642+
# names it: resolving it is the action that unblocks this changeset.
1643+
echo "::warning::open needs-human PR #${R_NUMBER} is not the bot's to rewrite: ${R_VERDICT#refuse:}. So changeset ${CHANGESET_KEY} is NOT proposed this run — that PR is left alone and no sibling is opened. Merge or close #${R_NUMBER} and the next run proposes it."
1644+
emit_pr "$R_NUMBER" "$R_URL"
1645+
exit 0
1646+
;;
1647+
*)
1648+
echo "::error::open rolling PR #${R_NUMBER} cannot be proven safe to rewrite (${R_VERDICT#unknown:}) — refusing to rewrite it or open a sibling."
1649+
exit 1
1650+
;;
1651+
esac
1652+
R_HEAD="$(printf '%s' "$R_VIEW" | jq -r '.headRefOid')"
1653+
write_nh_body "$PR_BODY_FILE"
1654+
# Leased to the head the guard inspected: a push that landed after the
1655+
# check is rejected instead of overwritten.
1656+
git push --force-with-lease="refs/heads/${R_BRANCH}:${R_HEAD}" \
1657+
origin "HEAD:refs/heads/${R_BRANCH}"
1658+
echo "Pushed ${HEAD_SHA} onto rolling needs-human PR #${R_NUMBER}'s branch ${R_BRANCH}"
1659+
if ! EDIT_ERR="$(gh pr edit "$R_NUMBER" --title "$PR_TITLE" --body-file "$PR_BODY_FILE" 2>&1)"; then
1660+
echo "::error::pushed changeset ${CHANGESET_KEY} onto PR #${R_NUMBER}, but rewriting its body FAILED, so its markers still name the OLD changeset (the next run rewrites it again): ${EDIT_ERR}"
1661+
exit 1
1662+
fi
1663+
echo "needs-human: updated rolling PR #${R_NUMBER} IN PLACE to changeset ${CHANGESET_KEY} — not opening a sibling"
1664+
emit_pr "$R_NUMBER" "$R_URL"
1665+
exit 0
1666+
fi
1667+
15181668
# A PER-NOTE OVERLAP IS REPORTED. IT DOES NOT DECIDE THE RUN.
15191669
#
15201670
# This loop used to `exit 0` the whole step on the FIRST note that some
@@ -1550,61 +1700,19 @@ jobs:
15501700
fi
15511701
done
15521702
1553-
# Distinct branch (never collides with the ok-applied fix/drift-* branch).
1554-
# The changeset key is the LAST segment on purpose: it is the
1555-
# body-INDEPENDENT anchor the marker self-heal above uses to recognise this
1556-
# PR again if a human ever deletes its body markers. Keep it last — the
1557-
# self-heal matches on `endswith("-<key>")`.
1558-
BRANCH="drift-needs-human/$(date +%Y-%m-%d)-${RUN_ID}-${CHANGESET_KEY}"
1703+
# No rolling PR is open: open it. Distinct from the ok-applied fix/drift-*
1704+
# branch, unique per run (a closed rolling PR is never reused, so reopening
1705+
# it stays a way out of a suppression), and with NO changeset key in it,
1706+
# because later runs rewrite its content.
1707+
BRANCH="drift-needs-human/rolling-$(date +%Y-%m-%d)-${RUN_ID}"
15591708
git checkout -B "$BRANCH"
15601709
git push -u origin "$BRANCH"
15611710
echo "Pushed needs-human branch ${BRANCH} at ${HEAD_SHA}"
15621711
1563-
PR_BODY_FILE="${RUNNER_TEMP}/drift-needs-human-pr-body.md"
1564-
{
1565-
echo "## Needs a human decision (drift-sync)"
1566-
echo ""
1567-
echo "The deterministic, zero-LLM drift-sync found a model-family change it must"
1568-
echo "NOT auto-apply (a genuinely new/unclassified family, or a registry"
1569-
echo "structural mismatch). It wrote the note file(s) below and opened this PR so"
1570-
echo "the decision is REACHABLE in the repo."
1571-
echo ""
1572-
echo "> **Not auto-merged — a human decides, and applies it by hand.**"
1573-
echo "> For a *new-family* note, DO NOT expect merging this PR to apply anything:"
1574-
echo "> \`includeFamilies\` is checksum-pinned, and adding a family moves that pin,"
1575-
echo "> which reds the freeze test gate-2 re-runs. drift-sync cannot re-pin — the"
1576-
echo "> gate-1 allowlist permits only \`model-registry.ts\` and \`drift-proposals/\`."
1577-
echo "> So a follow-up run would report \`gate-failed\`, not \`ok-applied\`."
1578-
echo ">"
1579-
echo "> The procedure that actually works, in ONE commit: add the family to"
1580-
echo "> \`includeFamilies\` (or \`excludeFamilies\` to reject), re-pin BOTH checksums,"
1581-
echo "> resolve the note file(s) below, and update the offline \`/models\` wave."
1582-
echo "> Then close this PR. Prior examples: 72f85f8, 936b59c."
1583-
echo ""
1584-
echo "### Note file(s) in this PR"
1585-
echo ""
1586-
for note in "${NOTES[@]:-}"; do
1587-
[ -n "$note" ] || continue
1588-
echo "- \`${note}\`"
1589-
done
1590-
echo ""
1591-
# Emitted from the SINGLE MARKERS array built at the top of this step
1592-
# (the PRIMARY changeset marker plus one per note), so the set written
1593-
# here is by construction the set the marker self-heal re-asserts and
1594-
# the set the guards above match.
1595-
for m in "${MARKERS[@]}"; do
1596-
echo "<!-- ${m} -->"
1597-
done
1598-
echo ""
1599-
echo "### drift-sync outcome"
1600-
echo ""
1601-
echo '```'
1602-
cat "${RUNNER_TEMP}/drift-sync.log" 2>/dev/null || echo "(see drift-sync-log artifact)"
1603-
echo '```'
1604-
} > "$PR_BODY_FILE"
1712+
write_nh_body "$PR_BODY_FILE"
16051713
16061714
gh pr create \
1607-
--title "chore(drift-sync): needs-human model-family decision ($(date +%Y-%m-%d))" \
1715+
--title "$PR_TITLE" \
16081716
--assignee jpr5 \
16091717
--body-file "$PR_BODY_FILE"
16101718

0 commit comments

Comments
 (0)