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
12 changes: 12 additions & 0 deletions .claude/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,17 @@
"foresight@foresight-vendored": true,
"superpowers@foresight-vendored": false,
"mattpocock-skills@foresight-vendored": false
},
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/scripts/install-git-hooks.sh"
}
]
}
]
}
}
105 changes: 105 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env bash
# Fast checks on the staged change only: rustfmt on staged Rust files and
# ESLint on the staged JavaScript that `pnpm lint` covers. No build, no test
# suite and no network call: the CI workflow (.github/workflows/ci.yml) runs
# those.
# Written for bash 3.2, the bash macOS ships.
set -uo pipefail

cd "$(git rev-parse --show-toplevel)" || exit 1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
status=0

staged() {
git diff --cached --name-only -z --diff-filter=ACMR -- "$@"
}

# The rustfmt of the toolchain rust-toolchain.toml pins, found once. `rustup
# which` fails on a toolchain that is not installed, where the rustfmt proxy
# would download it.
find_rustfmt() {
if command -v rustup >/dev/null 2>&1; then
local channel
channel=$(sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml)
if ! rustup which --toolchain "$channel" rustfmt 2>/dev/null; then
echo "pre-commit: rustfmt of the pinned toolchain $channel is not installed. Run 'rustup show' in the checkout, which installs it, then commit again." >&2
return 1
fi
else
command -v rustfmt || { echo "pre-commit: rustfmt is not installed." >&2; return 1; }
fi
}

# rustfmt reads each staged blob on stdin, so a file whose working copy differs
# from what is staged is judged by what will be committed.
rust_files=()
while IFS= read -r -d '' file; do
rust_files+=("$file")
done < <(staged '*.rs')

if [ ${#rust_files[@]} -gt 0 ]; then
if ! rustfmt=$(find_rustfmt); then
status=1
else
edition=$(sed -n 's/^edition *= *"\(.*\)"/\1/p' Cargo.toml | head -n 1)
unformatted=()
for file in "${rust_files[@]}"; do
git show ":$file" >"$tmp/in"
if ! "$rustfmt" --edition "$edition" --emit stdout <"$tmp/in" >"$tmp/out" 2>"$tmp/err"; then
echo "pre-commit: rustfmt could not check $file:" >&2
cat "$tmp/err" >&2
status=1
elif ! cmp -s "$tmp/in" "$tmp/out"; then
unformatted+=("$file")
fi
done
if [ ${#unformatted[@]} -gt 0 ]; then
echo "pre-commit: these staged Rust files are not formatted:" >&2
printf ' %s\n' "${unformatted[@]}" >&2
echo "Run 'cargo fmt --all', then 'git add' them again." >&2
status=1
fi
fi
fi

# The paths the packages' lint scripts give ESLint, which `pnpm lint` runs.
# Other tracked JavaScript is not lint-clean, so the hook leaves it alone too.
# A file whose working copy matches the index is linted in one run; a partly
# staged file is linted from its staged blob on stdin.
js_whole=()
js_partial=()
while IFS= read -r -d '' file; do
if git diff --quiet -- "$file"; then
js_whole+=("$file")
else
js_partial+=("$file")
fi
done < <(staged \
':(glob)packages/*/src/**/*.js' ':(glob)packages/*/src/**/*.mjs' ':(glob)packages/*/src/**/*.cjs' \
':(glob)packages/*/test/**/*.js' ':(glob)packages/*/test/**/*.mjs' ':(glob)packages/*/test/**/*.cjs' \
':(glob)examples/demo-integrator/*.js' ':(glob)examples/demo-integrator/site/*.js')

if [ $((${#js_whole[@]} + ${#js_partial[@]})) -gt 0 ]; then
eslint=node_modules/.bin/eslint
if [ ! -x "$eslint" ]; then
echo "pre-commit: ESLint skipped, no node_modules here; run 'pnpm install' to lint staged JavaScript. CI runs 'pnpm lint'." >&2
else
eslint_failed=0
if [ ${#js_whole[@]} -gt 0 ]; then
"$eslint" --no-warn-ignored -- "${js_whole[@]}" >&2 || eslint_failed=1
fi
for file in ${js_partial[@]+"${js_partial[@]}"}; do
git show ":$file" | "$eslint" --no-warn-ignored --stdin --stdin-filename "$file" >&2 || eslint_failed=1
done
if [ $eslint_failed -ne 0 ]; then
status=1
echo "Fix what ESLint reports ('pnpm lint' lints the same files), then 'git add' them again." >&2
fi
fi
fi

if [ $status -ne 0 ]; then
echo "pre-commit: commit refused. 'git commit --no-verify' skips this hook." >&2
fi
exit $status
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
- run: pnpm lint
- run: pnpm test
- run: rustup show
- name: Pre-commit hook tests
if: runner.os == 'Linux'
run: python3 -m unittest discover -s scripts -p 'test_git_hooks.py'
- if: runner.os == 'Linux'
run: sudo apt-get update && sudo apt-get install -y bubblewrap
- run: cargo fmt --all -- --check
Expand Down
16 changes: 16 additions & 0 deletions docs/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ pnpm lint
pnpm test
```

### The pre-commit hook

A tracked pre-commit hook, `.githooks/pre-commit`, runs fast checks on what you stage. It refuses a commit when a staged Rust file is not formatted as `cargo fmt` would format it, and when a staged JavaScript file that `pnpm lint` covers fails ESLint: the packages' `src` and `test` folders and the demo Integrator. It checks the staged copy of each file, not the one in your working tree. When `node_modules` is missing it skips ESLint and says so. It finishes in well under a second. It runs no build, no test suite and no network call. Clippy, the test suites and the Gateway's runtime test run in the [CI workflow](https://github.com/zeroae/countersign/blob/main/.github/workflows/ci.yml) and the end-to-end workflow.

A Claude session installs the hook when it starts, through the `SessionStart` hook in `.claude/settings.json`. Without Claude, install it once per clone from the checkout root:

```sh
scripts/install-git-hooks.sh
```

The script sets `core.hooksPath` to `.githooks`. That setting covers every worktree of the clone, and each worktree runs its own tracked copy of the hook. If `core.hooksPath` already points elsewhere, or the clone's `.git/hooks` folder holds hooks of its own, which `core.hooksPath` would stop running, the script changes nothing and says so. To skip the hook for one commit, run `git commit --no-verify`. The tests for the hook and the script are in `scripts/test_git_hooks.py`:

```sh
python3 -m unittest discover -s scripts -p 'test_git_hooks.py'
```

To run the Gateway's tests under the filter sandbox on a host that refuses namespaces, run `scripts/filter-sandbox-container.sh`, which needs Docker. It runs the containment test and the runtime test in a container under Docker's default security profile, as the end-to-end workflow's `gateway-filter-sandbox` job does. Its two last steps use a profile with one quirk each: where the host refuses the allow-list, the filter sandbox has to fail at startup by name, and where the kernel has no `close_range`, a grant worker still has to start with no descriptor of the supervisor's.

That container has the CI runner's kernel. To run the same two tests on real ECS Fargate 1.4.0, x86-64 and arm64, use the qualification harness, `qualification/fargate/run.sh`. It needs our AWS account, and its [README](https://github.com/zeroae/countersign/blob/main/qualification/fargate/README.md) says how to deploy, run and tear it down. Run it for every release that touches the grant worker.
Expand Down
30 changes: 30 additions & 0 deletions scripts/install-git-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Points this clone at the tracked hooks in .githooks/. core.hooksPath lives in
# the clone's shared config, so one run covers every worktree, and a relative
# path makes each worktree run its own tracked copy of the hooks.
# A Claude session runs this at start (.claude/settings.json). It prints nothing
# when the hooks are already installed. It never replaces another hooks path,
# and never hides hooks already in the clone's hooks folder, which core.hooksPath
# would stop running.
set -euo pipefail

cd "$(dirname "$0")/.."
git rev-parse --git-dir >/dev/null 2>&1 || exit 0

current=$(git config --get core.hooksPath || true)
case "$current" in
.githooks) ;;
"")
hooks=$(git rev-parse --path-format=absolute --git-common-dir)/hooks
existing=$(find "$hooks/" -maxdepth 1 ! -type d ! -name '*.sample' -exec basename {} \; 2>/dev/null | sort | tr '\n' ' ')
if [ -n "$existing" ]; then
echo "$hooks holds hooks (${existing% }) that core.hooksPath would stop running, so the pre-commit hook in .githooks/ is not installed. Move them into .githooks/ or elsewhere, then run scripts/install-git-hooks.sh." >&2
exit 0
fi
git config core.hooksPath .githooks
echo "Installed the pre-commit hook: core.hooksPath is now .githooks."
;;
*)
echo "core.hooksPath is already '$current', so the pre-commit hook in .githooks/ is not installed. To use it: git config core.hooksPath .githooks" >&2
;;
esac
Loading
Loading