diff --git a/.claude/README.md b/.claude/README.md index 1434d5d..3e9c2bd 100644 --- a/.claude/README.md +++ b/.claude/README.md @@ -21,12 +21,16 @@ Hooks run before tool execution. Defined in `settings.json`, scripts in `hooks/` ## Skills -| Skill | Invocation | Purpose | -| --------------- | ----------------------------------- | ----------------------------------------------------------------------------- | -| `work-on-issue` | `/work-on-issue ` | Full issue lifecycle: triage → worktree → plan mode → implement → verify → PR | +| Skill | Invocation | Purpose | +| --------------- | ------------------------------- | ----------------------------------------------------------------------------- | +| `work-on-issue` | `/work-on-issue ` | Full issue lifecycle: triage → worktree → plan mode → implement → verify → PR | Skill definitions live in `skills//SKILL.md`. +`work-on-issue` calls GitHub through an MCP server registered as `github` (tools are named `mcp__github__*`). The repo does not commit an `.mcp.json`, so register the server yourself, per user, for example with `claude mcp add` at user or local scope, under the name `github`. Without it the skill cannot run end to end. + +Every shell command a skill tells an agent to run must pass the hooks. `scripts/test-agent-hooks.sh` extracts the commands from `work-on-issue/SKILL.md`, feeds each through `prefer-github-mcp.sh`, and fails on any deny. CI runs it. GitHub reads and writes go through the GitHub MCP tools; the skill waits on CI with `gh run list` and `gh run watch`, which the hook does not block. + ## Worktrees All implementation work happens in `.claude/worktrees/` (gitignored). `EnterWorktree` creates them automatically. One worktree per unit of work. diff --git a/.claude/skills/work-on-issue/SKILL.md b/.claude/skills/work-on-issue/SKILL.md index 4c9f86e..3f72bfe 100644 --- a/.claude/skills/work-on-issue/SKILL.md +++ b/.claude/skills/work-on-issue/SKILL.md @@ -9,8 +9,8 @@ argument-hint: "[issue-number]" Complete workflow to take a GitHub issue from triage to pull request. -**Repository**: `The-Read-Onlys/cscs.dev` -**Issue number**: `$ARGUMENTS` (required — abort with a clear message if empty) +**Repository**: `brazostech/cscs.dev` (owner `brazostech`, repo `cscs.dev`). Use these literal values in every GitHub call below. +**Issue number**: `$ARGUMENTS` (required, a bare issue number such as `88` — abort with a clear message if empty) --- @@ -18,7 +18,7 @@ Complete workflow to take a GitHub issue from triage to pull request. Gather all issue details in parallel: -- `mcp__github__issue_read` with method `get`, owner `The-Read-Onlys`, repo `cscs.dev`, issue_number `$ARGUMENTS` +- `mcp__github__issue_read` with method `get`, owner `brazostech`, repo `cscs.dev`, issue_number `$ARGUMENTS` - `mcp__github__issue_read` with method `get_comments`, same owner/repo/issue_number - `mcp__github__issue_read` with method `get_labels`, same owner/repo/issue_number @@ -82,14 +82,14 @@ If build or lint fails, fix the errors and re-run from the failing step. Do not 2. Stage **specific files** (never `git add -A`) 3. Create a commit with a conventional commit message: -``` +```text : Closes #$ARGUMENTS -Co-Authored-By: Claude Opus 4.6 +Co-Authored-By: Claude Opus 5.5 ``` Use the appropriate type: `feat`, `fix`, `docs`, `style`, `refactor`, `test`, `chore`. @@ -100,7 +100,7 @@ Use the appropriate type: `feat`, `fix`, `docs`, `style`, `refactor`, `test`, `c 1. Push the branch: `git push -u origin HEAD` 2. Create the PR using `mcp__github__create_pull_request`: - - owner: `The-Read-Onlys` + - owner: `brazostech` - repo: `cscs.dev` - title: Short description (under 70 chars) - head: current branch name @@ -129,35 +129,46 @@ Generated with [Claude Code](https://claude.com/claude-code) ## Step 9 — Monitor CI -After the PR is created, wait for CI checks to complete and fix any failures. +After the PR is created, wait for CI to complete and fix any failures. -1. Watch for CI completion: +The repo's hook denies the `gh` subcommands for pull requests, issues and search, so read PR status through the GitHub MCP server. The `gh` subcommands for workflow runs are not blocked, so use them to wait. - ``` - gh pr checks --repo The-Read-Onlys/cscs.dev --watch +The GitHub Actions `build` job is a check run. `get_status` returns only legacy commit statuses (here, the Netlify deploy preview), so take the Actions result from `gh run watch --exit-status` and its logs from `gh run view`. + +1. Find the workflow run for the pushed commit. If the list is empty or its `headSha` differs from `git rev-parse HEAD`, wait a few seconds and list again, for up to 2 minutes; if no run for the current commit appears by then, stop and report to the user. A run with conclusion `cancelled` for an older commit was superseded by the concurrency group, so ignore it and look for the run for the current commit: + + ```bash + gh run list --repo brazostech/cscs.dev --branch "$(git branch --show-current)" --limit 1 --json databaseId,status,conclusion,headSha ``` -2. **If all checks pass** → proceed to Step 10. + If the run for the current commit itself ends `cancelled`, re-run it once with `gh run rerun --repo brazostech/cscs.dev`, then watch the new run. If it is cancelled again, report it to the user. -3. **If any check fails** (max 3 fix attempts): - a. Identify failures: +2. Wait for that run to finish. A run can outlast the Bash tool's timeout, so run the command with the longest timeout available, or in the background: - ``` - gh pr checks --repo The-Read-Onlys/cscs.dev --json name,state,bucket + ```bash + gh run watch --repo brazostech/cscs.dev --exit-status ``` - b. Get failure logs: + If the command times out before the run ends, list the run again and watch it again, or poll its state with `gh run view --repo brazostech/cscs.dev --json status,conclusion`. - ``` - gh run view --repo The-Read-Onlys/cscs.dev --log-failed +3. Read the external statuses, such as the Netlify deploy preview, which `gh run` does not show: call `mcp__github__pull_request_read` with method `get_status`, owner `brazostech`, repo `cscs.dev`, pullNumber ``. For check-run details, call the same tool with method `get_check_runs`. If a status is still pending, wait a minute and call it again, for at most 10 minutes. After that, report the pending status to the user instead of waiting longer. + +4. **If all checks pass** → proceed to Step 10. + +5. **If any check fails** (max 3 fix attempts): + a. Identify the failure: a failed `gh run watch` is the Actions `build` job; a failed or errored status from `get_status` is an external check such as Netlify. + b. For an Actions failure, get the logs: + + ```bash + gh run view --repo brazostech/cscs.dev --log-failed ``` c. Fix the failures in the worktree. d. Re-run Step 6 (Verify) locally to confirm the fix. e. Stage specific files, commit with a message like `fix: resolve CI failure in `, and push. - f. Loop back to watch CI again (step 9.1). + f. Loop back to find the new run (step 9.1) and watch it. -4. **If still failing after 3 attempts** → tell the user, provide the PR URL and failing check details, and proceed to Step 10. +6. **If still failing after 3 attempts** → tell the user, provide the PR URL and failing check details, and proceed to Step 10. --- @@ -165,7 +176,7 @@ After the PR is created, wait for CI checks to complete and fix any failures. Ask the user: "Should I close issue #$ARGUMENTS now, or let the PR merge close it automatically?" -- If close now → `mcp__github__issue_write` with method `update`, state `closed`, state_reason `completed` +- If close now → `mcp__github__issue_write` with method `update`, owner `brazostech`, repo `cscs.dev`, issue_number `$ARGUMENTS`, state `closed`, state_reason `completed` - If let PR close it → confirm the `Closes #$ARGUMENTS` reference is in the PR body (it is from Step 8) --- diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d756f1b..e670044 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -63,6 +63,9 @@ jobs: grep -Eq "^[[:space:]]*POCKETBASE_VERSION:[[:space:]]*\"?${pb//./\\.}\"?[[:space:]]*$" podman-compose.yaml \ || { echo "POCKETBASE_VERSION in podman-compose.yaml does not match Containerfile (${pb})"; exit 1; } + - name: Test agent hooks + run: scripts/test-agent-hooks.sh + - name: Install dependencies run: npm ci diff --git a/AGENTS.md b/AGENTS.md index b7297e5..1ee646f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,7 +6,7 @@ Instructions for AI coding agents. `CLAUDE.md` is a symlink to this file. - **Project**: CSCS community website — Astro 7 + React 19, Tailwind CSS v4, PocketBase backend, Catalyst UI Kit - **Worktree required**: All implementation work must happen in a git worktree. Use `EnterWorktree` before editing project files. A hook enforces this. -- **GitHub MCP tools**: Use MCP tools (not `gh` CLI) for PRs, issues, etc. A hook redirects `gh` commands. +- **GitHub MCP tools**: Use MCP tools (not `gh` CLI) for PRs, issues, etc. A hook redirects `gh pr`, `gh issue` and `gh search`. `gh run list`, `gh run watch` and `gh run view` are allowed for waiting on CI and reading logs. - **Package manager**: npm only (yarn/pnpm/bun are denied by settings) ## Commands @@ -23,6 +23,7 @@ npm run test:run # Vitest (single run) npm run test:pb # PocketBase API rule tests (downloads PocketBase v0.34.0) npm run test:coverage # Vitest with v8 coverage npm run build-storybook # Build Storybook +scripts/test-agent-hooks.sh # Checks the work-on-issue skill's commands pass the .claude hooks (needs jq) make -C backend pocketbase # Install the pinned PocketBase at backend/pocketbase (gitignored, not committed) ``` @@ -139,7 +140,7 @@ Store dates as text (`YYYY-MM-DD`). Parse with `new Date(year, month - 1, day)` - **Commits**: conventional commits — `feat:`, `fix:`, `chore:`, `docs:`. Brief subject line, body for "why". - **PRs**: create via GitHub MCP tools. Title under 70 chars. Body with `## Summary` and `## Test plan`. `main` takes changes only through PRs (ruleset, ADR 0012). - **Merging**: squash only. The PR title becomes the commit subject on `main`, so it must be a Conventional Commit; the body becomes the commit message. Branches delete on merge. -- **CI**: PRs run lint, format check, typecheck, tests, PocketBase API rule tests (`npm run test:pb`), build, Storybook build via GitHub Actions. The `build` job and Netlify's `deploy-preview` must both pass before merge. +- **CI**: PRs run lint, format check, typecheck, tests, PocketBase API rule tests (`npm run test:pb`), build, Storybook build, and the agent hook test via GitHub Actions. The `build` job and Netlify's `deploy-preview` must both pass before merge. ## Boundaries @@ -158,7 +159,7 @@ Store dates as text (`YYYY-MM-DD`). Parse with `new Date(year, month - 1, day)` - **Auth system details**: `doc/authentication.md` - **Project audit & known issues**: `doc/audit.md` - **Feature roadmap**: `doc/improvements.md` -- **CI pipeline**: `.github/workflows/ci.yml` (lint, format check, typecheck, test, PocketBase API rule tests, build, Storybook build) +- **CI pipeline**: `.github/workflows/ci.yml` (lint, format check, typecheck, test, PocketBase API rule tests, build, Storybook build, agent hook test) - **Hook & skill docs**: `.claude/README.md` ## Worktree Workflow diff --git a/doc/adr/0009-ci-cd-github-actions-and-netlify.md b/doc/adr/0009-ci-cd-github-actions-and-netlify.md index 9a168f6..da2707e 100644 --- a/doc/adr/0009-ci-cd-github-actions-and-netlify.md +++ b/doc/adr/0009-ci-cd-github-actions-and-netlify.md @@ -65,3 +65,8 @@ Netlify watches the same repository and deploys independently when it detects a - `check-latest` fails open: if setup-node cannot fetch the Node release manifest, it keeps the cached Node. The engines step then fails the job when that Node is too old, and the job needs a re-run. - The `Containerfile` and `netlify.toml` still state only the major version and float on `22`, as the #81 amendment notes. - Cost: when the cache is stale, setup-node downloads Node, which adds seconds to the run. + +## Amendment (2026-11-02, #88) + +- The `build` job runs a `Test agent hooks` step, before `npm ci`, that runs `scripts/test-agent-hooks.sh`. It extracts the shell commands in `.claude/skills/work-on-issue/SKILL.md` and fails if `.claude/hooks/prefer-github-mcp.sh` denies any of them. It needs `jq`, which ubuntu-latest provides. `permissions` is unchanged. +- The step gates merges: editing any `SKILL.md` under `.claude/skills/`, `.claude/settings.json` or the `prefer-github-mcp.sh` hook can now fail the required `build` check. diff --git a/scripts/test-agent-hooks.sh b/scripts/test-agent-hooks.sh new file mode 100755 index 0000000..d2ee073 --- /dev/null +++ b/scripts/test-agent-hooks.sh @@ -0,0 +1,250 @@ +#!/usr/bin/env bash +# test-agent-hooks.sh — checks that every skill under .claude/skills/ and the +# repo's PreToolUse hook agree. +# +# For each skill it extracts every shell command the skill tells an agent to +# run (fenced blocks, ``` or ~~~, other than markdown/text/json ones, plus +# inline `gh|git|npm|...` code spans), feeds each one to +# .claude/hooks/prefer-github-mcp.sh as PreToolUse JSON, and expects no deny. +# A second, looser scan fails on any `gh pr|issue|search` anywhere in the +# skill text (after `cd x &&`, an env prefix, `$(...)`), except on lines that +# say "never". The test guards against passing vacuously with: +# - negative controls (commands the hook must deny), +# - a self-test of the extractor against a fixture, +# - anchors for work-on-issue (commands that must be extracted), +# - a check that settings.json registers the hook under the Bash matcher. +# It does not fix gaps in the hook itself (see #138). +# +# Usage: scripts/test-agent-hooks.sh [path/to/SKILL.md ...] +set -euo pipefail + +cd "$(dirname "$0")/.." + +HOOK=.claude/hooks/prefer-github-mcp.sh +SETTINGS=.claude/settings.json + +command -v jq >/dev/null || { echo "jq is required" >&2; exit 2; } + +if [ $# -gt 0 ]; then + skills=("$@") +else + skills=() + for f in .claude/skills/*/SKILL.md; do + [ -f "$f" ] && skills+=("$f") + done +fi +[ "${#skills[@]}" -gt 0 ] || { echo "no skills found under .claude/skills/" >&2; exit 2; } +for f in "${skills[@]}"; do + [ -f "$f" ] || { echo "skill not found: $f" >&2; exit 2; } +done + +# Prints the hook's decision for a command: "deny", "ask", "allow", ... +decision() { + local out + if ! out=$(jq -n --arg c "$1" '{tool_name: "Bash", tool_input: {command: $c}}' | "$HOOK"); then + echo "error" # the hook crashed: report it as a failed check, not a silent abort + return 0 + fi + # The hook prints nothing when it has no objection. + if [ -z "$out" ]; then + echo allow + else + jq -r '.hookSpecificOutput.permissionDecision // "allow"' <<<"$out" + fi +} + +failures=0 +fail() { + echo "FAIL $*" + failures=$((failures + 1)) +} +check() { # check