Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude/rules/sandcastle-hand-off.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ A pull request labelled before the repository began recording heads is not follo
## Which pull requests

- Opened by the repository's owner (a session's counts), in the same repository (not a fork), into `main`, not a draft.
- A branch name `scripts/check-branch-name.sh` accepts.
- A branch name `scripts/check-branch-name.sh` accepts, but not `dependabot/*`.
- Only a run on the default `Sandcastle` queue follows hand-offs: GitHub Actions always, a local run only with `SANDCASTLE_LABEL=Sandcastle`.
- The repository runs Sandcastle in GitHub Actions with its `SANDCASTLE_GH_TOKEN` secret set to the account the runs use, since a workflow there
records the head you label. Without that, nothing can be handed off.
Expand Down
6 changes: 3 additions & 3 deletions .github/ci/sandcastle-skills.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
# and runs no script of the repo's. Blank (or whitespace-only) lines and comments (a # in column 0)
# are ignored, LF line endings only, and any other line is one of two kinds:
#
# dotfiles-sha <40 lowercase hex digits> the commit of mpaulosky/dotfiles' main to take them from, given once
# skill <name> a skill under "Support For New Projects/.claude/skills/" there, once each
# dotfiles-sha <40 lowercase hex digits> the commit of the Application Factory's main (mpaulosky/Application_Factory) to take them from, given once
# skill <name> a skill under ".claude/skills/" there, once each
#
# Pin the commit like an action, so a push to dotfiles can't change what the
# Pin the commit like an action, so a push to the Factory can't change what the
# agents are told until the pin moves. The skills become the agents'
# instructions, so have a person review every change to this file.
#
Expand Down
210 changes: 201 additions & 9 deletions .github/scripts/tests/pr-automerge.test.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/lint-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
# actions, dangerous triggers. advanced-security: false makes findings
# fail the job instead of only uploading to code scanning.
- name: Run zizmor
uses: zizmorcore/zizmor-action@53290badfa369cb0bf7ad16342cb62c2fa47ac7b # v0.8.0
uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1
with:
version: "1.30.1"
min-severity: medium
Expand Down
44 changes: 40 additions & 4 deletions .github/workflows/pr-automerge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ name: PR Auto-Merge
# mpaulosky/dotfiles#369; one whose record never shows requests anyway, since a
# repeat request is idempotent, mpaulosky/dotfiles#374), requests the
# review with the PAT, and reads the request back for up to COPILOT_ASK_WAIT_MS.
# A request that fails or doesn't register counts as dropped at once, and a
# A request that fails, or whose read-back errors or finds the head moved, counts as dropped once the
# record is COPILOT_ASK_SETTLE_MS old (a wait capped by COPILOT_ASK_WAIT_MS, mpaulosky/Application_Factory#23);
# one that doesn't register counts the same after the read-back window, and a
# record COPILOT_ASK_SETTLE_MS old with no review of the head and no request
# pending counts the same way, whoever wrote it (land.sh writes the same
# record). Copilot is never asked again for the PR: a later head goes straight
Expand Down Expand Up @@ -298,6 +300,15 @@ jobs:
const CLAUDE_MARKER = "<!-- claude-review -->";
// Copilot's review comments are authored by this Bot.
const COPILOT_REVIEWER = "copilot-pull-request-reviewer";
// Whether a reviewer is the Copilot bot, by its exact login: the
// reviews API and GraphQL name it copilot-pull-request-reviewer,
// the requested-reviewer and timeline REST APIs name it Copilot.
// Only a Bot counts, so a user or team called copilot-x, or a
// user called Copilot, isn't taken for it.
const isCopilotBot = (login, type) => {
const name = (login ?? "").toLowerCase().replace(/\[bot\]$/, "");
return type === "Bot" && (name === "copilot" || name === COPILOT_REVIEWER);
};
// At most this many unreplied threads are named in the notice.
const UNREPLIED_NAMED = 3;
// The second line of a Claude review with findings outside the
Expand Down Expand Up @@ -1346,7 +1357,7 @@ jobs:
// the add (mpaulosky/dotfiles#381). A review Copilot submitted
// is a round, which bounds the removal already.
const copilotRequests = events
.filter((event) => event.event === "review_requested" && /copilot/i.test(event.requested_reviewer?.login ?? event.requested_team?.name ?? ""))
.filter((event) => event.event === "review_requested" && isCopilotBot(event.requested_reviewer?.login, event.requested_reviewer?.type))
.map((event) => Date.parse(event.created_at));
const adds = claudeEvents
.filter((event) => event.event === "labeled")
Expand Down Expand Up @@ -1540,15 +1551,16 @@ jobs:
// Fails closed: a request list that wasn't read, that holds more
// than the page, or has a request with no reviewer counts as
// pending, so Claude's review isn't taken for Copilot's. A Team's
// or Mannequin's request (no login comes back) isn't Copilot's.
// or Mannequin's request (no login comes back) isn't Copilot's, nor is a
// User's or Bot's with another login (copilot-x).
function copilotPending(pr) {
const requests = pr.reviewRequests;
if (!requests || requests.totalCount > requests.nodes.length) {
return true;
}
return requests.nodes.some((request) => {
const reviewer = request.requestedReviewer;
return !reviewer || /copilot/i.test(reviewer.login ?? "");
return !reviewer || isCopilotBot(reviewer.login, reviewer.__typename);
});
}

Expand Down Expand Up @@ -1680,6 +1692,7 @@ jobs:
}
// Names this run's record among any others on the PR.
const askId = `${Date.now()}-${Math.random().toString(36).slice(2)}`;
let askWrittenAt;
try {
await github.rest.issues.createComment({
owner,
Expand All @@ -1697,6 +1710,20 @@ jobs:
core.warning(`Couldn't record the ask for Copilot's review of PR #${pullNumber}, so it wasn't made: ${error.message}`);
return false;
}
// After the write returns, so a slow write can't shorten the wait:
// the record's own timestamp is no later than this.
askWrittenAt = Date.now();
// Wait out what is left of COPILOT_ASK_SETTLE_MS (capped by
// COPILOT_ASK_WAIT_MS) before a path that returns true without a
// registered request lets the caller add review:claude: other
// workflows see only the record, which Claude Review skips and PR
// Auto-Merge strips while it is younger than that
// (mpaulosky/Application_Factory#23). Every such path calls it:
// the failed request and the read-back's two early exits.
const waitOutSettle = () => new Promise((resolve) => setTimeout(
resolve,
Math.max(0, Math.min(COPILOT_ASK_WAIT_MS, askWrittenAt + COPILOT_ASK_SETTLE_MS - Date.now()))
));
// Two runs can both have read "no record" and both posted one.
// Read the comments again: only the run whose record is
// listed first makes the request (mpaulosky/dotfiles#369). GraphQL
Expand Down Expand Up @@ -1764,6 +1791,7 @@ jobs:
throw error;
}
core.warning(`Couldn't request Copilot's review of PR #${pullNumber}: ${error.message}`);
await waitOutSettle();
core.info(`Copilot's review of PR #${pullNumber} wasn't requested, so Claude is called in.`);
return true;
}
Expand All @@ -1774,11 +1802,13 @@ jobs:
// again, and only a sweep ends on an error that ends the run.
const deadline = Date.now() + COPILOT_ASK_WAIT_MS;
let registered = false;
let headMoved = false;
try {
do {
await new Promise((resolve) => setTimeout(resolve, Math.max(0, Math.min(COPILOT_ASK_POLL_MS, deadline - Date.now()))));
const seen = await getPullRequestStatus(pullNumber);
if (!seen || seen.headRefOid !== pr.headRefOid) {
headMoved = true;
break;
}
registered = copilotPending(seen) || copilotAnsweredHead(seen);
Expand All @@ -1788,8 +1818,14 @@ jobs:
throw error;
}
core.warning(`Couldn't read PR #${pullNumber} back after requesting Copilot's review, so the request is taken as made: ${error.message}`);
await waitOutSettle();
return true;
}
// The head moved or the PR couldn't be read: nothing was seen to
// register, so the record must settle before Claude is called in.
if (headMoved) {
await waitOutSettle();
}
core.info(registered
? `Copilot's review request for PR #${pullNumber} registered.`
: `Copilot's review request for PR #${pullNumber} didn't register (GitHub drops it once the Copilot code review budget is used up).`);
Expand Down
52 changes: 27 additions & 25 deletions .github/workflows/sandcastle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ jobs:
# lib/skills.mts mounts the skills the roles name (SANDBOX_SKILLS) into
# the sandbox from ~/.claude/skills, which a hosted runner doesn't have.
# The repo lists them in .github/ci/sandcastle-skills.txt, a Seed data
# file: one dotfiles-sha line (the commit to take them from, 40 hex
# digits) and one skill line per skill name. This step is Owned and
# file: one dotfiles-sha line (the Application Factory commit to take them
# from, 40 hex digits; the key keeps its old name so existing files stay valid) and one skill line per skill name. This step is Owned and
# reads that file as data only: it rejects any line that isn't blank, a
# # comment in column 0 or one of those two shapes (LF line endings), fetches
# the pinned commit of the Template owner's public dotfiles repository,
# refuses a commit that isn't in that repository's main (GitHub serves a
# the pinned commit of the Application Factory (mpaulosky/Application_Factory,
# public), which keeps the skills in its .claude/skills,
# refuses a commit that isn't in the Factory's main (GitHub serves a
# fork's commit by hash too), a skill that is missing and a symlink in a
# listed skill or above it, and links only the listed skills into ~/.claude/skills.
# This step runs no script of the repo's, so nothing its file says can
Expand All @@ -144,7 +145,7 @@ jobs:
- name: Install the skills
if: steps.secrets.outputs.enabled == 'true'
env:
DOTFILES_URL: https://github.com/mpaulosky/dotfiles.git
FACTORY_URL: https://github.com/mpaulosky/Application_Factory.git
Comment thread
mpaulosky marked this conversation as resolved.
Resolved
run: |
set -euo pipefail
list=.github/ci/sandcastle-skills.txt
Expand Down Expand Up @@ -196,43 +197,43 @@ jobs:
echo "::error file=$list::The file needs a dotfiles-sha line and at least one skill line, or neither."
exit 1
fi
base="Support For New Projects/.claude/skills"
dotfiles="$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/dotfiles.XXXXXX")"
git init --quiet "$dotfiles"
base=".claude/skills"
factory="$(mktemp -d "${RUNNER_TEMP:-${TMPDIR:-/tmp}}/factory.XXXXXX")"
git init --quiet "$factory"
paths=()
for skill in "${skills[@]}"; do
paths+=("$base/$skill")
done
git -C "$dotfiles" sparse-checkout set "${paths[@]}"
git -C "$dotfiles" fetch --quiet --depth 1 --filter=blob:none "$DOTFILES_URL" "$sha"
git -C "$dotfiles" checkout --quiet FETCH_HEAD
# GitHub serves a fork's commit by its hash, so the pin must be in dotfiles' own main.
git -C "$dotfiles" fetch --quiet --no-tags --filter=tree:0 "$DOTFILES_URL" refs/heads/main:refs/remotes/dotfiles/main
if ! git -C "$dotfiles" merge-base --is-ancestor "$sha" refs/remotes/dotfiles/main; then
echo "::error file=$list::The dotfiles-sha commit isn't in the main branch of dotfiles."
git -C "$factory" sparse-checkout set "${paths[@]}"
git -C "$factory" fetch --quiet --depth 1 --filter=blob:none "$FACTORY_URL" "$sha"
git -C "$factory" checkout --quiet FETCH_HEAD
# GitHub serves a fork's commit by its hash, so the pin must be in the Factory's own main.
git -C "$factory" fetch --quiet --no-tags --filter=tree:0 "$FACTORY_URL" refs/heads/main:refs/remotes/factory/main
if ! git -C "$factory" merge-base --is-ancestor "$sha" refs/remotes/factory/main; then
echo "::error file=$list::The dotfiles-sha commit isn't in the main branch of the Factory."
exit 1
fi
# The sandbox mount follows symlinks (lib/skills.mts mounts each skill's real path), so refuse one in
# a listed skill, in the skill folder itself or in any folder above it. Others in the commit are not ours.
for folder in "$dotfiles/Support For New Projects" "$dotfiles/Support For New Projects/.claude" "$dotfiles/$base"; do
for folder in "$factory/.claude" "$factory/$base"; do
if [ -L "$folder" ]; then
echo "::error::The dotfiles commit holds a symlink above the skills, which the sandbox mount would follow."
echo "::error::The Factory commit holds a symlink above the skills, which the sandbox mount would follow."
exit 1
fi
done
for skill in "${skills[@]}"; do
if [ -n "$(find "$dotfiles/$base/$skill" -type l -print -quit)" ]; then
if [ -n "$(find "$factory/$base/$skill" -type l -print -quit)" ]; then
echo "::error::The $skill skill holds a symlink, which the sandbox mount would follow."
exit 1
fi
if [ ! -f "$dotfiles/$base/$skill/SKILL.md" ]; then
echo "::error::dotfiles $sha has no $skill skill."
if [ ! -f "$factory/$base/$skill/SKILL.md" ]; then
echo "::error::The Factory commit $sha has no $skill skill."
exit 1
fi
done
mkdir -p "$HOME/.claude/skills"
for skill in "${skills[@]}"; do
ln -sT "$dotfiles/$base/$skill" "$HOME/.claude/skills/$skill"
ln -sT "$factory/$base/$skill" "$HOME/.claude/skills/$skill"
done

# The host's git fetches and pushes authenticate through gh's credential
Expand Down Expand Up @@ -304,12 +305,13 @@ jobs:
# keep running) can change a file in it before this step uploads it or
# the next step reads handbacks.json. The reviewers' logs reach its
# reviewer-logs/ folder through lib/role-logs.mts, which writes an
# allowlisted view of each (the last host status lines, the last known
# tool names without arguments, and the last few error lines), named
# allowlisted view of each (the host status lines, the known tool names
# without arguments, and the error words that appeared, each only as a
# fixed line in a sorted set: no order, count, number or text), named
# reviewer-<n>.log and never for the source file, and leaves
# out a view shaped like a secret. The view narrows what a steered
# reviewer can print into the artifact; it can't promise a few words on
# an error line stay out. The pre-push review's own log is never copied.
# reviewer can print into the artifact: no free text of a log line gets
# through. The pre-push review's own log is never copied.
- name: Upload the run report
if: always() && steps.secrets.outputs.enabled == 'true'
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
Expand Down
2 changes: 1 addition & 1 deletion .sandcastle/lib/branches.mts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function isIssueBranch(branch: string, issueNumber: number): boolean {
}

// Whether a branch is one scripts/check-branch-name.sh accepts, without --pr: feature/, fix/ or hotfix/{n}-{slug}, or
// chore/{slug}. The branch of a pull request the owner handed to Sandcastle (lib/follow-up.mts#sweepSubject) has to pass
// chore/{slug}, but not the script's dependabot/* (the hook lets a person push a fix to Dependabot's branch; Sandcastle hands off none). The branch of a pull request the owner handed to Sandcastle (lib/follow-up.mts#sweepSubject) has to pass
// it, because its name reaches a shell the way an issue branch's does ({{BRANCH}} in review-prompt.md, git in the sandbox).
export function isHandOffBranch(branch: string): boolean {
return /^(?:(?:feature|fix|hotfix)\/[0-9]+-[a-z0-9]+(?:-[a-z0-9]+)*|chore\/[a-z][a-z0-9]*(?:-[a-z0-9]+)*)$/.test(branch);
Expand Down
7 changes: 5 additions & 2 deletions .sandcastle/lib/branches.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("isIssueBranch", () => {
});

// The branches of a pull request handed to Sandcastle with the sandcastle:follow-up label: the same names
// scripts/check-branch-name.sh lets through, so a name that reaches a shell is as strict as an issue branch's.
// scripts/check-branch-name.sh lets through (but for its dependabot/*), so a name that reaches a shell is as strict as an issue branch's.
describe("isHandOffBranch", () => {
for (const branch of ["feature/5-add-search", "fix/12-stop-the-crash", "hotfix/1-patch", "chore/tidy-up", "chore/a", "chore/bump-v2-deps", "feature/314-x"]) {
it(`accepts ${branch}`, () => {
Expand Down Expand Up @@ -136,7 +136,10 @@ describe("isHandOffBranch", () => {
"chore/a\t", "chore/a\r", "feature/5-x ", "chore/ ",
];

for (const branch of corpus) assert.equal(isHandOffBranch(branch), passesBranchStandard(branch), JSON.stringify(branch));
// The script also lets a person push to Dependabot's dependabot/* branches; a hand-off branch is never one.
for (const branch of corpus) {
assert.equal(isHandOffBranch(branch), !branch.startsWith("dependabot/") && passesBranchStandard(branch), JSON.stringify(branch));
}
});

// Not in the corpus above: bash's [a-z] and [0-9] take letters and digits of other scripts in some locales, so the
Expand Down
3 changes: 1 addition & 2 deletions .sandcastle/lib/review-ledger.mts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export const OWNED_PATHS = [
".claude/rules/sandcastle-hand-off.md",
".claude/skills/README.md",
".security/README.md",
"LICENSE",
".editorconfig",
".gitignore",
".markdownlint-cli2.jsonc",
Expand All @@ -156,7 +155,7 @@ export const OWNED_PATHS = [
// local gate's .sandcastle/check.sh, whose watched paths and early exit decide
// whether the Sandcastle check runs at all, global.json, which sets the SDK and the msbuild-sdks a build runs,
// docs/PROCESS.md, which review-prompt.md tells a role to follow, and the gate chain (see OWNED_PATHS above).
// The other files that moved from Seed to Owned (LICENSE, CODE_OF_CONDUCT.md,
// The other files that moved from Seed to Owned (CODE_OF_CONDUCT.md,
// the READMEs under .security/ and .claude/skills/) are audited text that no
// role reads, and a wrong one only misleads a person, so they stay exempt.
// The exemption judges a path, never the bytes, since a repository has no copy
Expand Down
Loading
Loading