Skip to content

ci(openspec): gate every PR in a stack on its tip archiving - #727

Merged
aibex-agent[bot] merged 2 commits into
mainfrom
aibex/t_FZrz1MHUE0UTTIHBbRVm5QI5
Nov 30, 2026
Merged

aibex-agent[bot] merged 2 commits into
mainfrom
aibex/t_FZrz1MHUE0UTTIHBbRVm5QI5

Conversation

@aibex-agent

@aibex-agent aibex-agent Bot commented Nov 30, 2026

Copy link
Copy Markdown

The daily sweep reports OpenSpec rot only after it reaches main. This adds a gate before merge.

openspec-stack.yml runs 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 an OpenSpec: stack archived commit status on every open member: failure while any tip still carries a directory under openspec/changes/ other than archive/, success once the tip archives.

  • Status, not a failing job: the push that archives lands on the tip, a different PR from the lower layers. GitHub never re-runs one PR's checks for another, so each run recomputes the whole stack and rewrites every member's status.
  • API errors fail the run and write nothing, so a gh timeout cannot redden a PR (what killed the earlier gates).
  • Not part of Validate, so it cannot gate a nightly publish.
  • A branching stack is judged on every tip above the PR; a standalone PR is its own tip.
  • Fork PRs cannot write statuses and cannot be in a stack here, so the job result itself is the verdict for them.

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.

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.
@aibex-agent aibex-agent Bot added the skip-changeset PR intentionally ships no release note (bypasses the changeset requirement) label Nov 30, 2026
@aibex-agent

aibex-agent Bot commented Nov 30, 2026

Copy link
Copy Markdown
Author

@claude /review

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @aibex-agent[bot]'s task in 4m 21s —— View job


Review mode: incremental — read 0 prior review thread(s) before reviewing.

.prior-review.json contained no reviews, review threads, or comments for this PR, so this is the first review — nothing prior to classify as addressed or still-open. Every finding below is [New].

Summary

The PR adds openspec-stack.yml + openspec-stack.cjs (with a solid node --test suite wired into pnpm test:scripts) to gate every PR in an OpenSpec stack on its tip having archived, writing a commit status rather than failing a job (since the fix lands on a different PR than the ones it turns green). The stack-resolution logic (findRoot, subtree, tipsOf, affectedRoots) is careful about cycles and about closed/retargeted PRs, and AGENTS.md is updated consistently with the new behavior.

One finding is significant enough to flag before merge:

  • Security (.github/workflows/openspec-stack.yml lines 42-53): the job checks out and executes openspec-stack.cjs from the PR's own head commit, in a job granted statuses: write / pull-requests: read. For a same-repo branch, GitHub does not downgrade the token, so a modified script on the PR branch runs with real write access to commit statuses across the repo before any human review. This repo already has the right pattern next door — stack-breadcrumb.yml's dispatch job checks out default_branch instead of the PR head specifically because "this privileged job... must run trusted, reviewed logic, never PR-supplied code." Worth aligning openspec-stack.yml with that precedent.

Smaller notes, all inline:

  • The "overlapping runs converge" claim (openspec-stack.yml line 27) isn't fully guaranteed without serialization — out-of-order run completion could leave a stale status. stack-breadcrumb.yml solves this with per-root concurrency grouping + a two-stage dispatch; this workflow has neither.
  • evaluate() reads each stack tip's tree serially (openspec-stack.cjs lines 188-191); Promise.all would parallelize without changing the abort-on-first-error behavior. Low impact at typical stack sizes.
  • There's a plausible transient-consistency window right after a parent PR closes (merge + delete-branch) and before GitHub's auto-retarget edited event reaches its children (openspec-stack.cjs lines 33-46): the child can briefly be judged as its own root/tip instead of its real stack. Self-heals on the next event per the stated design, but it's untested and worth a second look.

Everything else — the fork handling, the truncation to the 140-char status limit, the readChanges 404-as-empty handling, the pinned action SHAs (consistent with the rest of the repo), and the AGENTS.md wording — checked out fine.

Comment on lines +42 to +53
- 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 });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Comment thread .github/workflows/openspec-stack.yml Outdated

# 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread .github/scripts/openspec-stack.cjs Outdated
Comment on lines +188 to +191
const changesByTip = new Map();
for (const tip of tips) {
changesByTip.set(tip, await readChanges(index.byNumber.get(tip).headSha));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: tips are read with Promise.all.

Comment on lines +33 to +46
* 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.
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@aibex-agent
aibex-agent Bot merged commit 35c1813 into main Nov 30, 2026
9 checks passed
@aibex-agent
aibex-agent Bot deleted the aibex/t_FZrz1MHUE0UTTIHBbRVm5QI5 branch November 30, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changeset PR intentionally ships no release note (bypasses the changeset requirement)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant