Repository navigation
ci(openspec): gate every PR in a stack on its tip archiving - #727
Conversation
openspec-stack.yml resolves the stack a pull request belongs to, reads the head tree of each tip, and writes an 'OpenSpec: stack archived' commit status on every open member. Every layer fails while a tip still carries an unarchived change under openspec/changes/, and turns green when the tip archives, since each run recomputes the whole stack.
|
@claude /review |
|
Claude finished @aibex-agent[bot]'s task in 4m 21s —— View job Review mode: incremental — read 0 prior review thread(s) before reviewing.
SummaryThe PR adds One finding is significant enough to flag before merge:
Smaller notes, all inline:
Everything else — the fork handling, the truncation to the 140-char status limit, the |
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| # The head commit rather than the default merge ref, which no longer | ||
| # exists once a pull request has closed. | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| sparse-checkout: .github/scripts | ||
|
|
||
| - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| with: | ||
| script: | | ||
| const gate = require(`${process.env.GITHUB_WORKSPACE}/.github/scripts/openspec-stack.cjs`); | ||
| await gate.run({ github, context, core }); |
There was a problem hiding this comment.
[New] This checks out and require()s openspec-stack.cjs from the PR's own head commit (ref: github.event.pull_request.head.sha), then runs it in a job granted statuses: write and pull-requests: read.
For a same-repo branch (not a fork), GitHub does not downgrade the GITHUB_TOKEN, so the script that actually executes is whatever the PR author put in .github/scripts/openspec-stack.cjs on their own branch, running with real write access to commit statuses and read access to all PRs — before any human review. A modified copy on a branch could, for example, forge "success" statuses on arbitrary PRs in this repo (the script already enumerates every open PR), or exfiltrate the job's Actions runtime token.
This repo already has an established mitigation for exactly this pattern: stack-breadcrumb.yml's dispatch job explicitly checks out ${{ github.event.repository.default_branch }} instead of the PR head, with the comment "this privileged job (contents: write) must run trusted, reviewed logic, never PR-supplied code." That job also explicitly excludes PRs whose head repo isn't this one. openspec-stack.yml has neither: it runs PR-supplied code for same-repo PRs, and for fork PRs it still executes the fork's script (mitigated there only by GitHub's automatic read-only token downgrade, not by this workflow's own design).
Consider checking out the default branch's copy of openspec-stack.cjs (as stack-breadcrumb.yml does) rather than the PR head's.
There was a problem hiding this comment.
Fixed: the job now checks out the default branch's script, with the same bootstrap skip as stack-breadcrumb.yml. (The workflow YAML itself still comes from the PR under pull_request, so this narrows rather than closes the gap, the same as the breadcrumb job.)
|
|
||
| # No cancel-in-progress: two runs can belong to two different stacks, and | ||
| # cancelling one would leave its stack's statuses stale. Each run rewrites | ||
| # every status from fresh API reads, so overlapping runs converge. |
There was a problem hiding this comment.
[New] "Overlapping runs converge" assumes runs complete in the same order they started, which isn't guaranteed. If PR push A triggers run 1 and a near-simultaneous push B triggers run 2 with fresher data, but run 2 finishes first and run 1 (reading older state) finishes after it, run 1's stale statuses silently overwrite run 2's correct ones — and nothing else corrects them until some later event touches the stack again.
This repo's own stack-breadcrumb.yml solves the analogous problem with a concurrency: group: stack-breadcrumb-dispatch-${{ pr number }} plus a two-stage dispatch/reconcile split serialized per stack root. This workflow has no such serialization, so the convergence claim here is weaker than stated — worth a second look, even if the window is narrow in practice.
There was a problem hiding this comment.
Fixed: rather than a concurrency group (which would cancel another stack's pending run), a run now re-reads the open PRs just before writing and writes nothing if any member's head, base or children moved. The run that move triggered writes the current verdict. Covered by a new test.
| const changesByTip = new Map(); | ||
| for (const tip of tips) { | ||
| changesByTip.set(tip, await readChanges(index.byNumber.get(tip).headSha)); | ||
| } |
There was a problem hiding this comment.
[New] Minor: tips are read one at a time (await inside the for loop), so a stack with multiple tips does its getContent calls serially. Since nothing here depends on a prior iteration's result, await Promise.all(tips.map(...)) would read them concurrently without changing the abort-on-first-error semantics described above (Promise.all still rejects as soon as one read throws). Low impact at typical stack sizes, but scales linearly with tip count as-is.
There was a problem hiding this comment.
Fixed: tips are read with Promise.all.
| * request for a reason unrelated to it. Here an API error fails THIS run and | ||
| * writes no status at all: whatever the previous run decided stays in place, | ||
| * and the run is visible as an errored workflow instead of a verdict. | ||
| */ | ||
|
|
||
| const CONTEXT = "OpenSpec: stack archived"; | ||
| const ARCHIVE_DIRECTORY = "archive"; | ||
| const CHANGES_PATH = "openspec/changes"; | ||
|
|
||
| /** | ||
| * Index open pull requests by number and by head branch. Same-repository | ||
| * pull requests only: the stack is keyed by branch name, and a fork sharing a | ||
| * branch name would attach itself to an unrelated stack. | ||
| */ |
There was a problem hiding this comment.
[New] indexPullRequests (and therefore byHead/childrenOf) is built only from currently-open pull requests. When a parent PR merges with --delete-branch (the documented workflow per AGENTS.md), there's a window between GitHub's closed webhook for the parent and the separate auto-retarget edited webhook(s) it sends to each child once the branch is actually deleted. A closed event processed in that window sees the child's baseRefName still pointing at the now-closed parent's branch, which is no longer a key in byHead — so the child is (temporarily) treated as its own root/tip, judged only on its own head tree rather than the stack it's actually still logically part of, and a status can be written for that wrong, narrower verdict.
This self-heals once the edited retarget event arrives and triggers another run, so it's a transient-consistency gap rather than a lasting bug, and it matches the stated "any event on any layer refreshes every layer" design — but it's not covered by a test, and it's worth confirming the self-heal always actually fires in practice (e.g., if a user retargets a child manually after noticing instead of GitHub doing it automatically, or if branch deletion is delayed/skipped).
There was a problem hiding this comment.
Not changing this. A member's verdict depends only on the tips ABOVE it, never on its ancestors. When a parent closes, the child's subtree (and so its tips) is the same whether or not the child is momentarily treated as a root, so the verdict written in that window is the correct one, not a narrower one. A merged parent's own head SHA is never written to, since only open PRs get statuses.
- Check out the default branch's script: the job holds statuses: write and a same-repository token is not downgraded, so a PR-supplied script could forge a green status. - Re-read the stack before writing and write nothing if any member moved, so a run that finishes late cannot overwrite a newer verdict. - Read tips concurrently.
The daily sweep reports OpenSpec rot only after it reaches
main. This adds a gate before merge.openspec-stack.ymlruns on every PR event, resolves the stack the PR belongs to, walks up to its tip(s), and reads each tip's head tree. It writes anOpenSpec: stack archivedcommit status on every open member: failure while any tip still carries a directory underopenspec/changes/other thanarchive/, success once the tip archives.ghtimeout cannot redden a PR (what killed the earlier gates).Validate, so it cannot gate a nightly publish.AGENTS.md is updated where it said nothing gates archiving. The label, tip warning, tracking issue and sweep stay as reporting.
Note: the status is not yet a required check on
main.