diff --git a/.github/workflows/context-cadence.yml b/.github/workflows/context-cadence.yml index 4df8861..0b9cc4a 100644 --- a/.github/workflows/context-cadence.yml +++ b/.github/workflows/context-cadence.yml @@ -50,9 +50,11 @@ jobs: # during the measurement, and rebasing on a depth-1 clone lacks the history # to replay onto. # Never below @v5: v4 targets Node 20, which runners force onto Node 24 - # with a deprecation warning on every run (#163). The template's pin is - # only a floor: a re-render keeps an installed SHA pin, or an installed - # major at or above the template's, so a Dependabot bump survives (#373). + # with a deprecation warning on every run (#163). A SHA pin, because + # this job holds the key and write access, and a tag can be moved + # (#675). The template's pin is a floor: a re-render keeps an installed + # SHA pin, or a tag above the template's, so a Dependabot bump survives + # (#373). - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: submodules: recursive @@ -91,6 +93,40 @@ jobs: echo "merge mode: $M" echo "MERGE_MODE=$M" >>"$GITHUB_ENV" + # merge_mode pr (#683): the PR path merges the measurement branch into + # the branch the run is on and pushes the result to that branch, and a + # dispatch can name any branch. Run on feature/x, the row's parents + # carried feature/x's commits into the open measurement PR into the + # default branch, unreviewed, under a bot's title. So a pr run stops + # here, before the credential is spent, unless it is on the default + # branch. That is read from origin, not the event, so a schedule and a + # dispatch read it alike, and unread is a refusal. It binds this file as + # rendered: a branch that edits its own workflow is past it, as anyone + # with write access already is. `direct` is not held: its row lands on + # the dispatched branch itself, reviewed in that branch's own PR. The + # full GITHUB_REF, not GITHUB_REF_NAME (CR 2565): a dispatch on a tag + # named like the default branch has the same short name. Without it + # (a run outside Actions) the checked-out branch answers. + - name: Hold the run to the default branch + run: | + [ "${MERGE_MODE:-}" = pr ] || { echo "merge mode ${MERGE_MODE:-unset}: the row goes to the branch the run is on"; exit 0; } + HEAD_REF=$(git ls-remote --symref origin HEAD) || HEAD_REF='' + DEFAULT=$(awk '$1 == "ref:" && $3 == "HEAD" { sub("^refs/heads/", "", $2); print $2; exit }' <<<"$HEAD_REF") + if [ -z "$DEFAULT" ]; then + echo "::error::could not read origin's default branch, so whether this run is on it is unknown — nothing was measured." + exit 1 + fi + if [ -n "${GITHUB_REF:-}" ]; then + ON=$GITHUB_REF WANT=refs/heads/$DEFAULT + else + ON=$(git branch --show-current) WANT=$DEFAULT + fi + if [ "$ON" != "$WANT" ]; then + echo "::error::this run is on ${ON:-a detached HEAD}, not the default branch $DEFAULT. In pr mode a run merges the measurement branch into the branch it is on and pushes the result, so that branch's commits would reach the measurement PR into $DEFAULT unreviewed (#683). Nothing was measured: dispatch the workflow on $DEFAULT." + exit 1 + fi + echo "on $DEFAULT, the default branch" + # One path per script, each probed on its own and exported under its own # name: measure-context.sh as MEASURE_CONTEXT_SH, and so on. Never one # directory for all of them — a project scripts/ holding only @@ -125,22 +161,30 @@ jobs: # --no-commit: HEAD stays the default branch's tip, so the row's # repo_commit is a commit that branch keeps, and the commit step # records the merge with the row. Which PR is decided as the push - # decides it, and the push decides again: a PR can merge mid-run. + # decides it, and the push decides again: a PR can merge mid-run. What + # was taken in goes to the commit step as TAKEN and TAKEN_PR (#683). - name: Take in the measurement PR's rows env: GH_TOKEN: ${{ github.token }} run: | [ "${MERGE_MODE:-}" = pr ] || { echo "merge mode ${MERGE_MODE:-unset}: no PR's rows to take in"; exit 0; } BRANCH="${GITHUB_REF_NAME:-$(git branch --show-current)}" + LOST="nothing was measured" + # >>> which measurement PR (#683) + # Every PR from the branch, any state, and only this repo's: --head matches + # a branch name, a fork's included. gh failing is not "none". KEEP when the + # branch carries rows no finished PR took: an open PR's, or a run's whose PR + # was never opened. An open PR always wins: one closed and another opened + # from the same head leaves a finished PR at the tip of an open one's rows. PR_BRANCH=context/weekly-measurement PRS=$(gh pr list --head "$PR_BRANCH" --base "$BRANCH" --state all --json number,state,headRefOid,isCrossRepository --jq '.[] | select(.isCrossRepository | not) | "\(.number) \(.state) \(.headRefOid)"') || { - echo "::error::gh pr list failed, so which measurement PR is open is unknown — nothing was measured." + echo "::error::gh pr list failed, so which measurement PR is open is unknown — $LOST." exit 1; } REMOTE=$(git ls-remote origin "refs/heads/$PR_BRANCH") || { - echo "::error::could not read $PR_BRANCH from origin — nothing was measured." + echo "::error::could not read $PR_BRANCH from origin — $LOST." exit 1; } TIP=${REMOTE%%[[:space:]]*} - OPEN='' ENDED='' + OPEN='' ENDED='' KEEP='' while read -r N S OID; do [ -n "$N" ] || continue if [ "$S" = OPEN ]; then @@ -150,6 +194,10 @@ jobs: fi done <<<"$PRS" if [ -n "$TIP" ] && { [ -n "$OPEN" ] || [ -z "$ENDED" ]; }; then + KEEP=1 + fi + # <<< which measurement PR + if [ -n "$KEEP" ]; then # The commit step sets these too; the merge needs them now. git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" @@ -160,6 +208,7 @@ jobs: echo "::error::could not merge $PR_BRANCH in — nothing was measured." git diff --name-only --diff-filter=U | sed 's/^/::error:: conflicted: /' exit 1; } + { echo "TAKEN=$TIP"; echo "TAKEN_PR=$OPEN"; } >>"$GITHUB_ENV" echo "measuring after the rows on $PR_BRANCH${OPEN:+ (PR #$OPEN)}" else echo "no rows on $PR_BRANCH to take in: the row is measured after $BRANCH's" @@ -262,19 +311,23 @@ jobs: case "${MERGE_MODE:-}" in direct) ;; pr) - PR_BRANCH=context/weekly-measurement + LOST="the row was not pushed" ROW=$(git rev-parse HEAD) - # Every PR from the branch, any state, and only this repo's: - # --head matches a branch name, a fork's included. gh failing - # is not "none". + # >>> which measurement PR (#683) + # Every PR from the branch, any state, and only this repo's: --head matches + # a branch name, a fork's included. gh failing is not "none". KEEP when the + # branch carries rows no finished PR took: an open PR's, or a run's whose PR + # was never opened. An open PR always wins: one closed and another opened + # from the same head leaves a finished PR at the tip of an open one's rows. + PR_BRANCH=context/weekly-measurement PRS=$(gh pr list --head "$PR_BRANCH" --base "$BRANCH" --state all --json number,state,headRefOid,isCrossRepository --jq '.[] | select(.isCrossRepository | not) | "\(.number) \(.state) \(.headRefOid)"') || { - echo "::error::gh pr list failed, so which measurement PR is open is unknown — the row was not pushed." + echo "::error::gh pr list failed, so which measurement PR is open is unknown — $LOST." exit 1; } REMOTE=$(git ls-remote origin "refs/heads/$PR_BRANCH") || { - echo "::error::could not read $PR_BRANCH from origin — the row was not pushed." + echo "::error::could not read $PR_BRANCH from origin — $LOST." exit 1; } TIP=${REMOTE%%[[:space:]]*} - OPEN='' ENDED='' + OPEN='' ENDED='' KEEP='' while read -r N S OID; do [ -n "$N" ] || continue if [ "$S" = OPEN ]; then @@ -283,9 +336,54 @@ jobs: ENDED=$N fi done <<<"$PRS" - # An open PR always wins: one closed and another opened from the - # same head leaves a finished PR at the tip of an open one's rows. if [ -n "$TIP" ] && { [ -n "$OPEN" ] || [ -z "$ENDED" ]; }; then + KEEP=1 + fi + # <<< which measurement PR + # An OPEN the branch wasn't kept for is stale (CR 2575): gh pr + # list runs before ls-remote, and a PR merged with its branch + # deleted between the two left OPEN set and no branch. Deleting a + # PR's head branch closes it, so the restart path opens a PR + # rather than reporting "updated" on one that ended. + [ -n "$KEEP" ] || OPEN='' + # The take-in step merged a tip in (TAKEN) because the branch was + # to be kept. Not kept now, its PR merged or closed mid-run + # (#683), and ROW has that tip as a parent: restarted at ROW, the + # branch reopens those rows in a new PR whose merge base is the + # old default tip, showing its merged rows again. Rebuilding the row on + # the new tip means replaying the calibration files' merge too, so + # the row is refused instead, and a fresh run measures after them. + # Not GitHub's "Re-run jobs" (CR 2564): it reuses GITHUB_SHA, so + # checkout gives it the commit from before the merge again. Unless + # the PR merged with a merge commit (CR 2567): then the tip is in + # $BRANCH's history, so a PR from the row can't show its rows + # again, and the restart below is right. A fetch + # that fails can't say so, and refuses. The row is merged INTO + # $BRANCH's new tip first (CR 2571, CR 2574): the row's other + # parent is this run's checkout, which the taken-in tip lacks + # whenever $BRANCH moved between runs, and two merge bases put + # the merged rows back in the PR's diff. Into, not from: as on + # the keep-the-branch path, merge=union then writes $BRANCH's + # rows first, so one that landed mid-run stays before this one. + # With HEAD on $BRANCH, merge=ours keeps $BRANCH's ratio file, a + # trade that path already accepts: the next --exact recomputes + # it (#173). + if [ -n "${TAKEN:-}" ] && [ -z "$KEEP" ]; then + ENDED_WHAT=${TAKEN_PR:+PR #$TAKEN_PR} + if git fetch -q origin "+refs/heads/$BRANCH:refs/remotes/origin/$BRANCH" \ + && git merge-base --is-ancestor "$TAKEN" "origin/$BRANCH"; then + echo "${ENDED_WHAT:-$PR_BRANCH} merged into $BRANCH during this run, its rows with it: the branch restarts at this row" + { git checkout -q --detach "origin/$BRANCH" \ + && git merge -q --no-edit -m "chore: weekly context measurement" "$ROW"; } || { + echo "::error::merging the row into $BRANCH's new tip failed — the row was not pushed." + git diff --name-only --diff-filter=U | sed 's/^/::error:: conflicted: /' + exit 1; } + else + echo "::error::${ENDED_WHAT:-$PR_BRANCH} ended during this run (merged, closed or deleted), after its rows were taken in at ${TAKEN:0:12}. Pushed now, this row would reopen them in a new PR. The row was not pushed: dispatch the workflow again (gh workflow run context-cadence.yml) or let the next scheduled run measure after them, not Re-run jobs, which reuses this run's commit, from before the merge." + exit 1 + fi + fi + if [ -n "$KEEP" ]; then # The branch carries rows no finished PR took: an open PR's, or # a run's whose PR was never opened. Merge the row INTO it, so # those rows stay first and in order and $BRANCH comes in with diff --git a/docs/DEPENDENCIES.md b/docs/DEPENDENCIES.md index 0ad7b77..b4e6626 100644 --- a/docs/DEPENDENCIES.md +++ b/docs/DEPENDENCIES.md @@ -23,7 +23,7 @@ Ported from broker#88 and broker#90 (#80). These are broker's majors (operator, 2026-10-29). `tests/test_dependabot.py` holds the form. `tests/test_ci.py` holds each major to a floor, so a Dependabot major passes and a downgrade fails. `tests/test_lock_audit.py` holds `audit.yml`'s pins equal to `ci.yml`'s. -The generated `context-cadence.yml` pins `actions/checkout` the same way, by hand; its installer keeps the pin on a re-render ([SKILLS.md § Context budget](SKILLS.md#context-budget)). `tests/test_dependabot.py` reads every workflow. +The generated `context-cadence.yml` pins `actions/checkout` the same way, by hand; its installer keeps the pin on a re-render ([SKILLS.md § Context budget](SKILLS.md#context-budget)). `tests/test_dependabot.py` reads every workflow, and `tests/test_ci.py` holds this one's major to the same floor (#165). That a SHA *is* its tag needs the network, so it is checked by hand whenever a pin moves: diff --git a/docs/SKILLS.md b/docs/SKILLS.md index 27d64a1..883bf4d 100644 --- a/docs/SKILLS.md +++ b/docs/SKILLS.md @@ -95,7 +95,7 @@ The post-merge pass runs as written. Its `git pull --ff-only` is a no-op after t `curating-context` holds `AGENTS.md` to 6,000 tokens and each live doc under `docs/` (not `plans/`, `specs/`) to 10,000 (`.skills/context-budget`, `.skills/context-doc-budget`). Two surfaces keep a curation's ground, installed by #126 (operator, 2026-11-08): - **The write guard.** A `PostToolUse` hook on `Edit|Write|MultiEdit`, symlinked into the vendored skill. It never blocks. It speaks only when an edit grows a file past its budget (`WARN`), or into its last 10% (`NEAR`). Every decision goes to `$(git rev-parse --absolute-git-dir)/context-budget.log`, one per worktree. A shell redirect (`cat >>`, `sed -i`) escapes it; `reviewing-code*`'s `context-delta.sh` sees the branch whole. -- **The weekly measurement.** `context-cadence.yml`, Tuesdays 16:14 UTC and on dispatch. It measures and never curates. It records one `baseline:scheduled` row in `.skills/context-metrics.jsonl`, and warns (`::warning::`) when a file is over budget, or seams or counts accrued. Under `merge_mode pr` it pushes `context/weekly-measurement` and keeps one standing PR open, merged like any other ([§ Merging under `pr`](#merging-under-pr)). A PR opened with `GITHUB_TOKEN` starts no CI: close and reopen it to run the checks. It needs the `ANTHROPIC_API_KEY` repository secret, and *Allow GitHub Actions to create and approve pull requests* (Settings → Actions → General). Without the secret it fails at its first step and records nothing. +- **The weekly measurement.** `context-cadence.yml`, Tuesdays 16:14 UTC and on dispatch. It measures and never curates. It records one `baseline:scheduled` row in `.skills/context-metrics.jsonl`, and warns (`::warning::`) when a file is over budget, or seams or counts accrued. Under `merge_mode pr` it pushes `context/weekly-measurement` and keeps one standing PR open, merged like any other ([§ Merging under `pr`](#merging-under-pr)). A PR opened with `GITHUB_TOKEN` starts no CI: close and reopen it to run the checks. It runs on `main` only: dispatched on another branch it fails before measuring ([gregoryfoster/skills#683](https://github.com/gregoryfoster/skills/issues/683)). If that PR is FF-merged during a run, the branch restarts at the new row. Any other end (squash, close, deleted branch) fails the run: dispatch it again, never *Re-run jobs*, which reuses the commit from before the merge. It needs the `ANTHROPIC_API_KEY` repository secret, and *Allow GitHub Actions to create and approve pull requests* (Settings → Actions → General). Without the secret it fails at its first step and records nothing. The workflow is generated: re-run `install-cadence.sh`, don't edit it. A re-render keeps the `actions/checkout` SHA pin ([DEPENDENCIES.md § CI pins and Dependabot](DEPENDENCIES.md#ci-pins-and-dependabot)), and Dependabot moves it. Its `runs-on: ubuntu-latest` is the template's, and a re-render writes it back. The job reads Markdown with bash, curl and python3 and builds nothing, so the node's image doesn't apply. diff --git a/tests/test_ci.py b/tests/test_ci.py index a9a6373..cd40879 100644 --- a/tests/test_ci.py +++ b/tests/test_ci.py @@ -118,7 +118,7 @@ def test_checkout_brings_the_skill_submodules(ci: dict, name: str) -> None: assert checkout["with"]["submodules"] is True -@pytest.mark.parametrize("workflow", ["ci.yml", "audit.yml"]) +@pytest.mark.parametrize("workflow", ["ci.yml", "audit.yml", "context-cadence.yml"]) def test_actions_are_at_or_above_their_floor(workflow: str) -> None: """The form itself (`@ # vX.Y.Z` on every `uses:`) is test_dependabot.py's.""" text = (WORKFLOW.parent / workflow).read_text()