From f8ae9cb5b39cdb911741f148ae4eb2a717d3b1a9 Mon Sep 17 00:00:00 2001 From: Zhe Li Date: Thu, 12 Nov 2026 15:00:14 +0000 Subject: [PATCH 1/3] ci: add Cloud Agent commit-msg hooks Strip Cursor's initiator Co-authored-by trailer after it is injected, and gate git commit via .cursor/hooks.json so AI authorship fails before the commit-authorship CI check. --- .cursor/hooks.json | 13 +++ .cursor/hooks/check-git-commit.sh | 85 +++++++++++++++ .githooks/README.md | 26 +++++ .githooks/commit-msg | 10 ++ .githooks/strip-coauthored-by.sh | 25 +++++ .gitignore | 5 +- CONTRIBUTING.md | 5 + scripts/ci/commit-hooks_test.sh | 130 +++++++++++++++++++++++ scripts/ci/ensure-cursor-commit-hooks.sh | 68 ++++++++++++ 9 files changed, 366 insertions(+), 1 deletion(-) create mode 100644 .cursor/hooks.json create mode 100755 .cursor/hooks/check-git-commit.sh create mode 100755 .githooks/README.md create mode 100755 .githooks/commit-msg create mode 100755 .githooks/strip-coauthored-by.sh create mode 100755 scripts/ci/commit-hooks_test.sh create mode 100755 scripts/ci/ensure-cursor-commit-hooks.sh diff --git a/.cursor/hooks.json b/.cursor/hooks.json new file mode 100644 index 00000000..ae88c1ae --- /dev/null +++ b/.cursor/hooks.json @@ -0,0 +1,13 @@ +{ + "version": 1, + "hooks": { + "beforeShellExecution": [ + { + "command": ".cursor/hooks/check-git-commit.sh", + "matcher": "git[[:space:]]+commit", + "failClosed": true, + "timeout": 30 + } + ] + } +} diff --git a/.cursor/hooks/check-git-commit.sh b/.cursor/hooks/check-git-commit.sh new file mode 100755 index 00000000..de65bbf9 --- /dev/null +++ b/.cursor/hooks/check-git-commit.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# beforeShellExecution: gate git commit for this repo. +# - Ensures Cursor commit-msg strip hooks are installed +# - Denies AI-tool author/committer identities (same idea as +# scripts/ci/check-authorship.sh) +# - Denies an explicit initiator Co-authored-by in the command text +# +# Stdin: JSON { command, cwd, ... } +# Stdout: JSON { permission, agent_message?, user_message? } +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" + +input="$(cat)" +command="$(printf '%s' "${input}" | jq -r '.command // empty')" + +allow() { + jq -nc --arg msg "${1:-}" \ + '{permission:"allow"} + (if $msg == "" then {} else {agent_message:$msg} end)' + exit 0 +} + +deny() { + local msg="$1" + jq -nc --arg msg "${msg}" '{permission:"deny",agent_message:$msg,user_message:$msg}' + exit 0 +} + +# Only care about git commit (including amend). Matcher already filters, but +# be defensive for hooks.json edits. +if ! printf '%s' "${command}" | grep -Eq '(^|[[:space:];|&])git([[:space:]]+|-c[[:space:]]|-)([^;|&]*[[:space:]])?commit([[:space:]]|$)'; then + allow +fi + +# Keep the strip hook after Cursor's co-author hook. +if [ -x "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" ]; then + bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null 2>&1 || true +fi + +# Same boundary-aware pattern as scripts/ci/check-authorship.sh. +AI_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' + +identity_is_ai() { + printf '%s' "$1" | grep -Eiq "${AI_PATTERN}" +} + +author_name="${GIT_AUTHOR_NAME:-}" +author_email="${GIT_AUTHOR_EMAIL:-}" +committer_name="${GIT_COMMITTER_NAME:-}" +committer_email="${GIT_COMMITTER_EMAIL:-}" + +# Fall back to git config when env overrides are absent. +if [ -z "${author_name}" ]; then + author_name="$(git -C "${repo_root}" config --get user.name 2>/dev/null || true)" +fi +if [ -z "${author_email}" ]; then + author_email="$(git -C "${repo_root}" config --get user.email 2>/dev/null || true)" +fi +if [ -z "${committer_name}" ]; then + committer_name="$(git -C "${repo_root}" config --get user.name 2>/dev/null || true)" +fi +if [ -z "${committer_email}" ]; then + committer_email="$(git -C "${repo_root}" config --get user.email 2>/dev/null || true)" +fi + +author="${author_name} <${author_email}>" +committer="${committer_name} <${committer_email}>" + +if identity_is_ai "${author}" || identity_is_ai "${committer}"; then + deny "git commit blocked: author/committer looks like an AI tool (${author} / ${committer}). Re-run with human GIT_AUTHOR_* and GIT_COMMITTER_* (and no Co-authored-by: Zhe Li trailer). Example: +GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \\ +GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \\ +git commit ..." +fi + +if printf '%s' "${command}" | grep -Eiq 'Co-authored-by:[[:space:]]*Zhe Li[[:space:]]*'; then + deny "git commit blocked: do not put 'Co-authored-by: Zhe Li ' in the commit message. Use human author/committer instead; the repo hooks strip Cursor's injected copy of that trailer." +fi + +if printf '%s' "${command}" | grep -Eq -- '--no-verify|--no-v\b'; then + deny "git commit blocked: --no-verify skips the commit-msg hook that strips Cursor's Co-authored-by trailer and would fail Check commit authorship. Omit --no-verify." +fi + +allow "Commit authorship looks human. Repo hooks will strip Co-authored-by: Zhe Li if Cursor re-injects it." diff --git a/.githooks/README.md b/.githooks/README.md new file mode 100755 index 00000000..e2d2a0e6 --- /dev/null +++ b/.githooks/README.md @@ -0,0 +1,26 @@ +# Git hooks + +Repo hooks for Cloud Agents and local commits. + +| Hook | Purpose | +| --- | --- | +| `commit-msg` | Strip `Co-authored-by: Zhe Li ` (Cursor Cloud initiator trailer) | +| `strip-coauthored-by.sh` | Shared implementation | + +## Why the ensure script exists + +Cursor Cloud sets `core.hooksPath` to a managed dispatcher under +`~/.cursor/agent-hooks/…`. That dispatcher: + +1. Runs hooks from `.cursor-original-hooks-path` (we point this at `.githooks/`) +2. Then runs Cursor’s own `commit-msg.cursor.co-author`, which **re-adds** the trailer + +So we also install `commit-msg.cursor.zz-strip-coauthor` into the managed +hooks dir so it runs **after** Cursor’s co-author hook. + +```bash +bash scripts/ci/ensure-cursor-commit-hooks.sh +``` + +The Cursor Agent hook `.cursor/hooks/check-git-commit.sh` runs that ensure +step before every `git commit`. diff --git a/.githooks/commit-msg b/.githooks/commit-msg new file mode 100755 index 00000000..6064bb3a --- /dev/null +++ b/.githooks/commit-msg @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Repo commit-msg hook: strip the Cloud Agent initiator Co-authored-by +# trailer. When Cursor manages core.hooksPath, this runs via the +# dispatcher *before* commit-msg.cursor.co-author re-adds the trailer; +# scripts/ci/ensure-cursor-commit-hooks.sh also installs a later +# commit-msg.cursor.zz-strip-coauthor that strips it again. +set -euo pipefail + +hook_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +bash "${hook_dir}/strip-coauthored-by.sh" "$1" diff --git a/.githooks/strip-coauthored-by.sh b/.githooks/strip-coauthored-by.sh new file mode 100755 index 00000000..2b492c6f --- /dev/null +++ b/.githooks/strip-coauthored-by.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Strip the Cloud Agent initiator Co-authored-by trailer from a commit +# message file ($1). Shared by .githooks/commit-msg and the Cursor +# agent-hooks post-hook installed by scripts/ci/ensure-cursor-commit-hooks.sh. +# +# Cursor's commit-msg.cursor.co-author appends: +# Co-authored-by: Zhe Li +# after repo hooks run. A later *.cursor.zz-* hook (or a local commit-msg +# when Cursor isn't in the chain) removes it again. +set -euo pipefail + +msg_file="${1:-}" +if [ -z "${msg_file}" ] || [ ! -f "${msg_file}" ]; then + exit 0 +fi + +# Exact initiator identity Cursor injects for this workspace's owner. +# Match case-insensitively; keep any other Co-authored-by lines. +tmp="$(mktemp)" +trap 'rm -f "${tmp}"' EXIT + +grep -viE '^[[:space:]]*Co-authored-by:[[:space:]]*Zhe Li[[:space:]]*[[:space:]]*$' \ + "${msg_file}" > "${tmp}" || true +mv "${tmp}" "${msg_file}" +trap - EXIT diff --git a/.gitignore b/.gitignore index fd6fabe9..d256a498 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,10 @@ tmp/ .vscode/ # AI files -.cursor/ +.cursor/* +!.cursor/hooks.json +!.cursor/hooks/ +!.cursor/hooks/** .claude/ AGENTS.md CLAUDE.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e225ae06..a4c17d76 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -144,6 +144,11 @@ Rules: - Linters enabled: `govet`, `ineffassign`, `staticcheck`, `unused` - Formatters enabled: `gofmt`, `goimports` - Pre-commit hook available: install with `uvx pre-commit install` +- Cloud Agent commit hooks (strip Cursor’s initiator `Co-authored-by`, + block AI authorship on `git commit`): see [`.githooks/README.md`](.githooks/README.md) + and [`.cursor/hooks.json`](.cursor/hooks.json). Run + `bash scripts/ci/ensure-cursor-commit-hooks.sh` once per Cloud Agent + boot if commits happen before the Agent hook fires. ### Build diff --git a/scripts/ci/commit-hooks_test.sh b/scripts/ci/commit-hooks_test.sh new file mode 100755 index 00000000..3265bf3b --- /dev/null +++ b/scripts/ci/commit-hooks_test.sh @@ -0,0 +1,130 @@ +#!/usr/bin/env bash +# Regression checks for .githooks strip + Cursor beforeShellExecution gate. +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" +tmp="$(mktemp -d)" +trap 'rm -rf "${tmp}"' EXIT + +assert_eq() { + local name="$1" got="$2" exp="$3" + if [ "${got}" != "${exp}" ]; then + echo "FAIL: ${name}" >&2 + echo " got: ${got}" >&2 + echo " expected: ${exp}" >&2 + exit 1 + fi +} + +assert_file_lacks() { + local name="$1" file="$2" needle="$3" + if grep -Fiq "${needle}" "${file}"; then + echo "FAIL: ${name} — ${file} still contains: ${needle}" >&2 + exit 1 + fi +} + +assert_file_contains() { + local name="$1" file="$2" needle="$3" + if ! grep -Fq "${needle}" "${file}"; then + echo "FAIL: ${name} — ${file} missing: ${needle}" >&2 + exit 1 + fi +} + +# --- strip-coauthored-by.sh ---------------------------------------------- +msg="${tmp}/msg" +cat > "${msg}" <<'EOF' +docs: example + +Body text. + +Co-authored-by: Zhe Li +Co-authored-by: Someone Else +EOF + +bash "${repo_root}/.githooks/strip-coauthored-by.sh" "${msg}" +assert_file_lacks "strip initiator" "${msg}" "Zhe Li" +assert_file_contains "keep other" "${msg}" "Co-authored-by: Someone Else " +assert_file_contains "keep body" "${msg}" "Body text." + +# Case-insensitive strip +cat > "${msg}" <<'EOF' +subject + +co-authored-by: zhe li +EOF +bash "${repo_root}/.githooks/strip-coauthored-by.sh" "${msg}" +assert_file_lacks "case fold" "${msg}" "linuxcity" + +# --- beforeShellExecution gate ------------------------------------------- +hook="${repo_root}/.cursor/hooks/check-git-commit.sh" +chmod +x "${hook}" "${repo_root}/.githooks/"* "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" + +# Env vars must prefix `bash` (not `printf`) so the hook process sees them. +# AI author -> deny +out="$( + printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Cursor Agent' GIT_AUTHOR_EMAIL='cursoragent@cursor.com' \ + GIT_COMMITTER_NAME='Cursor Agent' GIT_COMMITTER_EMAIL='cursoragent@cursor.com' \ + bash "${hook}" +)" +assert_eq "deny ai author permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +# Human author -> allow +out="$( + printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "allow human author permission" "$(printf '%s' "${out}" | jq -r .permission)" "allow" + +# Explicit Zhe Co-authored-by in command -> deny +out="$( + printf '%s' '{"command":"git commit -m \"msg\n\nCo-authored-by: Zhe Li \"","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "deny explicit trailer permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +# --no-verify -> deny +out="$( + printf '%s' '{"command":"git commit --no-verify -m test","cwd":"'"${repo_root}"'"}' | \ + GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ + GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \ + bash "${hook}" +)" +assert_eq "deny no-verify permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" + +# --- ensure script against a fake Cursor hooks dir ----------------------- +fake="${tmp}/agent-hooks" +mkdir -p "${fake}" +printf '%s' '/tmp/original' > "${fake}/.cursor-original-hooks-path" +touch "${fake}/.dispatcher" +chmod +x "${fake}/.dispatcher" +# Pretend git uses this hooksPath for a nested temp repo? ensure reads +# git config from repo_root — so temporarily set and restore. +old_hooks="$(git -C "${repo_root}" config --get core.hooksPath || true)" +git -C "${repo_root}" config core.hooksPath "${fake}" +bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null +assert_file_contains "original path" "${fake}/.cursor-original-hooks-path" "${repo_root}/.githooks" +[ -x "${fake}/commit-msg.cursor.zz-strip-coauthor" ] || { + echo "FAIL: zz-strip hook not installed" >&2 + exit 1 +} +# Restore prior hooksPath (Cursor managed path, or unset). +if [ -n "${old_hooks}" ]; then + git -C "${repo_root}" config core.hooksPath "${old_hooks}" +else + git -C "${repo_root}" config --unset core.hooksPath || true +fi + +bash -n "${repo_root}/.githooks/commit-msg" +bash -n "${repo_root}/.githooks/strip-coauthored-by.sh" +bash -n "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" +bash -n "${hook}" + +echo "OK: commit-hooks" diff --git a/scripts/ci/ensure-cursor-commit-hooks.sh b/scripts/ci/ensure-cursor-commit-hooks.sh new file mode 100755 index 00000000..754a7261 --- /dev/null +++ b/scripts/ci/ensure-cursor-commit-hooks.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# +# Wire repo commit-msg hooks into Cursor Cloud's managed hooksPath so the +# initiator Co-authored-by trailer is stripped after Cursor re-adds it. +# +# Idempotent. Safe to run from Agent beforeShellExecution, environment +# start, or manually. +# +# What it does: +# 1. Points the Cursor dispatcher's .cursor-original-hooks-path at +# /.githooks so our commit-msg runs in the chain. +# 2. Installs commit-msg.cursor.zz-strip-coauthor into the managed +# hooks dir (alphabetically after commit-msg.cursor.co-author) that +# strips Co-authored-by: Zhe Li . +# +set -euo pipefail + +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +repo_root="$(cd "${script_dir}/../.." && pwd)" +githooks="${repo_root}/.githooks" +strip_src="${githooks}/strip-coauthored-by.sh" + +if [ ! -x "${githooks}/commit-msg" ] || [ ! -f "${strip_src}" ]; then + echo "error: missing executable .githooks/commit-msg or strip-coauthored-by.sh" >&2 + exit 1 +fi + +chmod +x "${githooks}/commit-msg" "${strip_src}" 2>/dev/null || true + +hooks_path="$(git -C "${repo_root}" config --get core.hooksPath || true)" +if [ -z "${hooks_path}" ]; then + # Local / non-Cursor: use .githooks directly. + git -C "${repo_root}" config core.hooksPath "${githooks}" + echo "set core.hooksPath=${githooks}" + exit 0 +fi + +# Resolve relative hooksPath against the repo. +case "${hooks_path}" in + /*) managed="${hooks_path}" ;; + *) managed="${repo_root}/${hooks_path}" ;; +esac + +if [ ! -d "${managed}" ]; then + echo "warning: core.hooksPath=${hooks_path} is not a directory; skipping Cursor wire-up" >&2 + exit 0 +fi + +# Only touch Cursor-managed dispatcher trees. +if [ ! -f "${managed}/.dispatcher" ] && [ ! -f "${managed}/.cursor-original-hooks-path" ]; then + echo "core.hooksPath=${hooks_path} is not a Cursor agent-hooks dir; leaving as-is" + exit 0 +fi + +printf '%s\n' "${githooks}" > "${managed}/.cursor-original-hooks-path" +echo "pointed .cursor-original-hooks-path -> ${githooks}" + +# Post-hook: must sort after commit-msg.cursor.co-author. +zz_hook="${managed}/commit-msg.cursor.zz-strip-coauthor" +cat > "${zz_hook}" < Date: Wed, 18 Nov 2026 16:13:14 +0000 Subject: [PATCH 2/3] ci: strip narrating comments and fix commit gate Fix the beforeShellExecution matcher for JS RegExp, share the AI identity pattern, read GIT_* overrides from the command text, and restore hooksPath on test failure. --- .cursor/hooks.json | 2 +- .cursor/hooks/check-git-commit.sh | 62 +++++++++++------------- .githooks/commit-msg | 5 -- .githooks/strip-coauthored-by.sh | 10 ---- scripts/ci/ai-tool-identity.sh | 15 ++++++ scripts/ci/check-authorship.sh | 7 +-- scripts/ci/commit-hooks_test.sh | 41 ++++++++++------ scripts/ci/ensure-cursor-commit-hooks.sh | 19 -------- 8 files changed, 76 insertions(+), 85 deletions(-) create mode 100755 scripts/ci/ai-tool-identity.sh diff --git a/.cursor/hooks.json b/.cursor/hooks.json index ae88c1ae..46f601d9 100644 --- a/.cursor/hooks.json +++ b/.cursor/hooks.json @@ -4,7 +4,7 @@ "beforeShellExecution": [ { "command": ".cursor/hooks/check-git-commit.sh", - "matcher": "git[[:space:]]+commit", + "matcher": "git\\s+commit", "failClosed": true, "timeout": 30 } diff --git a/.cursor/hooks/check-git-commit.sh b/.cursor/hooks/check-git-commit.sh index de65bbf9..b5683630 100755 --- a/.cursor/hooks/check-git-commit.sh +++ b/.cursor/hooks/check-git-commit.sh @@ -1,16 +1,10 @@ #!/usr/bin/env bash -# beforeShellExecution: gate git commit for this repo. -# - Ensures Cursor commit-msg strip hooks are installed -# - Denies AI-tool author/committer identities (same idea as -# scripts/ci/check-authorship.sh) -# - Denies an explicit initiator Co-authored-by in the command text -# -# Stdin: JSON { command, cwd, ... } -# Stdout: JSON { permission, agent_message?, user_message? } set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "${script_dir}/../.." && pwd)" +# shellcheck source=../../scripts/ci/ai-tool-identity.sh +source "${repo_root}/scripts/ci/ai-tool-identity.sh" input="$(cat)" command="$(printf '%s' "${input}" | jq -r '.command // empty')" @@ -27,48 +21,50 @@ deny() { exit 0 } -# Only care about git commit (including amend). Matcher already filters, but -# be defensive for hooks.json edits. if ! printf '%s' "${command}" | grep -Eq '(^|[[:space:];|&])git([[:space:]]+|-c[[:space:]]|-)([^;|&]*[[:space:]])?commit([[:space:]]|$)'; then allow fi -# Keep the strip hook after Cursor's co-author hook. if [ -x "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" ]; then bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null 2>&1 || true fi -# Same boundary-aware pattern as scripts/ci/check-authorship.sh. -AI_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' - identity_is_ai() { - printf '%s' "$1" | grep -Eiq "${AI_PATTERN}" + printf '%s' "$1" | grep -Eiq "${AI_TOOL_IDENTITY_PATTERN}" } -author_name="${GIT_AUTHOR_NAME:-}" -author_email="${GIT_AUTHOR_EMAIL:-}" -committer_name="${GIT_COMMITTER_NAME:-}" -committer_email="${GIT_COMMITTER_EMAIL:-}" +resolve_identity_part() { + local var="$1" env_val="$2" + local from_cmd + from_cmd="$(ai_tool_identity_from_command "${var}" "${command}")" + if [ -n "${from_cmd}" ]; then + printf '%s' "${from_cmd}" + return + fi + if [ -n "${env_val}" ]; then + printf '%s' "${env_val}" + return + fi + case "${var}" in + GIT_AUTHOR_NAME | GIT_COMMITTER_NAME) + git -C "${repo_root}" config --get user.name 2>/dev/null || true + ;; + GIT_AUTHOR_EMAIL | GIT_COMMITTER_EMAIL) + git -C "${repo_root}" config --get user.email 2>/dev/null || true + ;; + esac +} -# Fall back to git config when env overrides are absent. -if [ -z "${author_name}" ]; then - author_name="$(git -C "${repo_root}" config --get user.name 2>/dev/null || true)" -fi -if [ -z "${author_email}" ]; then - author_email="$(git -C "${repo_root}" config --get user.email 2>/dev/null || true)" -fi -if [ -z "${committer_name}" ]; then - committer_name="$(git -C "${repo_root}" config --get user.name 2>/dev/null || true)" -fi -if [ -z "${committer_email}" ]; then - committer_email="$(git -C "${repo_root}" config --get user.email 2>/dev/null || true)" -fi +author_name="$(resolve_identity_part GIT_AUTHOR_NAME "${GIT_AUTHOR_NAME:-}")" +author_email="$(resolve_identity_part GIT_AUTHOR_EMAIL "${GIT_AUTHOR_EMAIL:-}")" +committer_name="$(resolve_identity_part GIT_COMMITTER_NAME "${GIT_COMMITTER_NAME:-}")" +committer_email="$(resolve_identity_part GIT_COMMITTER_EMAIL "${GIT_COMMITTER_EMAIL:-}")" author="${author_name} <${author_email}>" committer="${committer_name} <${committer_email}>" if identity_is_ai "${author}" || identity_is_ai "${committer}"; then - deny "git commit blocked: author/committer looks like an AI tool (${author} / ${committer}). Re-run with human GIT_AUTHOR_* and GIT_COMMITTER_* (and no Co-authored-by: Zhe Li trailer). Example: + deny "git commit blocked: author/committer looks like an AI tool (${author} / ${committer}). Prefix the same shell command with human GIT_AUTHOR_* and GIT_COMMITTER_* (the gate reads those from the command text). Example: GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \\ GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \\ git commit ..." diff --git a/.githooks/commit-msg b/.githooks/commit-msg index 6064bb3a..9d15ab4d 100755 --- a/.githooks/commit-msg +++ b/.githooks/commit-msg @@ -1,9 +1,4 @@ #!/usr/bin/env bash -# Repo commit-msg hook: strip the Cloud Agent initiator Co-authored-by -# trailer. When Cursor manages core.hooksPath, this runs via the -# dispatcher *before* commit-msg.cursor.co-author re-adds the trailer; -# scripts/ci/ensure-cursor-commit-hooks.sh also installs a later -# commit-msg.cursor.zz-strip-coauthor that strips it again. set -euo pipefail hook_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" diff --git a/.githooks/strip-coauthored-by.sh b/.githooks/strip-coauthored-by.sh index 2b492c6f..bd548a85 100755 --- a/.githooks/strip-coauthored-by.sh +++ b/.githooks/strip-coauthored-by.sh @@ -1,12 +1,4 @@ #!/usr/bin/env bash -# Strip the Cloud Agent initiator Co-authored-by trailer from a commit -# message file ($1). Shared by .githooks/commit-msg and the Cursor -# agent-hooks post-hook installed by scripts/ci/ensure-cursor-commit-hooks.sh. -# -# Cursor's commit-msg.cursor.co-author appends: -# Co-authored-by: Zhe Li -# after repo hooks run. A later *.cursor.zz-* hook (or a local commit-msg -# when Cursor isn't in the chain) removes it again. set -euo pipefail msg_file="${1:-}" @@ -14,8 +6,6 @@ if [ -z "${msg_file}" ] || [ ! -f "${msg_file}" ]; then exit 0 fi -# Exact initiator identity Cursor injects for this workspace's owner. -# Match case-insensitively; keep any other Co-authored-by lines. tmp="$(mktemp)" trap 'rm -f "${tmp}"' EXIT diff --git a/scripts/ci/ai-tool-identity.sh b/scripts/ci/ai-tool-identity.sh new file mode 100755 index 00000000..ff173beb --- /dev/null +++ b/scripts/ci/ai-tool-identity.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# shellcheck shell=bash + +AI_TOOL_IDENTITY_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' + +ai_tool_identity_from_command() { + local var="$1" text="$2" + if [[ "${text}" =~ (^|[[:space:]])${var}=\'([^\']*)\' ]]; then + printf '%s' "${BASH_REMATCH[2]}" + elif [[ "${text}" =~ (^|[[:space:]])${var}=\"([^\"]*)\" ]]; then + printf '%s' "${BASH_REMATCH[2]}" + elif [[ "${text}" =~ (^|[[:space:]])${var}=([^[:space:]]+) ]]; then + printf '%s' "${BASH_REMATCH[2]}" + fi +} diff --git a/scripts/ci/check-authorship.sh b/scripts/ci/check-authorship.sh index a91d543b..d8c74661 100755 --- a/scripts/ci/check-authorship.sh +++ b/scripts/ci/check-authorship.sh @@ -8,9 +8,10 @@ set -euo pipefail -# Matched case-insensitively against a whole "Name " identity, with -# non-alphanumeric boundaries so names like "Claudia" or "precursor" pass. -PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' +_ci_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=ai-tool-identity.sh +source "${_ci_dir}/ai-tool-identity.sh" +PATTERN="${AI_TOOL_IDENTITY_PATTERN}" usage() { echo "usage: $0 [head-ref]" >&2 diff --git a/scripts/ci/commit-hooks_test.sh b/scripts/ci/commit-hooks_test.sh index 3265bf3b..66d791bb 100755 --- a/scripts/ci/commit-hooks_test.sh +++ b/scripts/ci/commit-hooks_test.sh @@ -1,11 +1,23 @@ #!/usr/bin/env bash -# Regression checks for .githooks strip + Cursor beforeShellExecution gate. set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "${script_dir}/../.." && pwd)" tmp="$(mktemp -d)" -trap 'rm -rf "${tmp}"' EXIT +hooks_mutated=0 +old_hooks="" + +cleanup() { + rm -rf "${tmp}" + if [ "${hooks_mutated}" = 1 ]; then + if [ -n "${old_hooks}" ]; then + git -C "${repo_root}" config core.hooksPath "${old_hooks}" + else + git -C "${repo_root}" config --unset core.hooksPath || true + fi + fi +} +trap cleanup EXIT assert_eq() { local name="$1" got="$2" exp="$3" @@ -33,7 +45,6 @@ assert_file_contains() { fi } -# --- strip-coauthored-by.sh ---------------------------------------------- msg="${tmp}/msg" cat > "${msg}" <<'EOF' docs: example @@ -49,7 +60,6 @@ assert_file_lacks "strip initiator" "${msg}" "Zhe Li" assert_file_contains "keep other" "${msg}" "Co-authored-by: Someone Else " assert_file_contains "keep body" "${msg}" "Body text." -# Case-insensitive strip cat > "${msg}" <<'EOF' subject @@ -58,12 +68,12 @@ EOF bash "${repo_root}/.githooks/strip-coauthored-by.sh" "${msg}" assert_file_lacks "case fold" "${msg}" "linuxcity" -# --- beforeShellExecution gate ------------------------------------------- hook="${repo_root}/.cursor/hooks/check-git-commit.sh" chmod +x "${hook}" "${repo_root}/.githooks/"* "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" -# Env vars must prefix `bash` (not `printf`) so the hook process sees them. -# AI author -> deny +node -e "process.exit(new RegExp('git\\\\s+commit').test('git commit -m test') ? 0 : 1)" \ + || { echo "FAIL: hooks.json matcher must be a JS RegExp that matches git commit" >&2; exit 1; } + out="$( printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ GIT_AUTHOR_NAME='Cursor Agent' GIT_AUTHOR_EMAIL='cursoragent@cursor.com' \ @@ -72,7 +82,6 @@ out="$( )" assert_eq "deny ai author permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" -# Human author -> allow out="$( printf '%s' '{"command":"git commit -m test","cwd":"'"${repo_root}"'"}' | \ GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ @@ -81,7 +90,13 @@ out="$( )" assert_eq "allow human author permission" "$(printf '%s' "${out}" | jq -r .permission)" "allow" -# Explicit Zhe Co-authored-by in command -> deny +out="$( + printf '%s' '{"command":"GIT_AUTHOR_NAME='\''Zhe Li'\'' GIT_AUTHOR_EMAIL='\''linuxcity.jn@gmail.com'\'' GIT_COMMITTER_NAME='\''Zhe Li'\'' GIT_COMMITTER_EMAIL='\''linuxcity.jn@gmail.com'\'' git commit -m test","cwd":"'"${repo_root}"'"}' | \ + env -u GIT_AUTHOR_NAME -u GIT_AUTHOR_EMAIL -u GIT_COMMITTER_NAME -u GIT_COMMITTER_EMAIL \ + bash "${hook}" +)" +assert_eq "allow human author from command text" "$(printf '%s' "${out}" | jq -r .permission)" "allow" + out="$( printf '%s' '{"command":"git commit -m \"msg\n\nCo-authored-by: Zhe Li \"","cwd":"'"${repo_root}"'"}' | \ GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ @@ -90,7 +105,6 @@ out="$( )" assert_eq "deny explicit trailer permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" -# --no-verify -> deny out="$( printf '%s' '{"command":"git commit --no-verify -m test","cwd":"'"${repo_root}"'"}' | \ GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \ @@ -99,15 +113,13 @@ out="$( )" assert_eq "deny no-verify permission" "$(printf '%s' "${out}" | jq -r .permission)" "deny" -# --- ensure script against a fake Cursor hooks dir ----------------------- fake="${tmp}/agent-hooks" mkdir -p "${fake}" printf '%s' '/tmp/original' > "${fake}/.cursor-original-hooks-path" touch "${fake}/.dispatcher" chmod +x "${fake}/.dispatcher" -# Pretend git uses this hooksPath for a nested temp repo? ensure reads -# git config from repo_root — so temporarily set and restore. old_hooks="$(git -C "${repo_root}" config --get core.hooksPath || true)" +hooks_mutated=1 git -C "${repo_root}" config core.hooksPath "${fake}" bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null assert_file_contains "original path" "${fake}/.cursor-original-hooks-path" "${repo_root}/.githooks" @@ -115,16 +127,17 @@ assert_file_contains "original path" "${fake}/.cursor-original-hooks-path" "${re echo "FAIL: zz-strip hook not installed" >&2 exit 1 } -# Restore prior hooksPath (Cursor managed path, or unset). if [ -n "${old_hooks}" ]; then git -C "${repo_root}" config core.hooksPath "${old_hooks}" else git -C "${repo_root}" config --unset core.hooksPath || true fi +hooks_mutated=0 bash -n "${repo_root}/.githooks/commit-msg" bash -n "${repo_root}/.githooks/strip-coauthored-by.sh" bash -n "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" +bash -n "${repo_root}/scripts/ci/ai-tool-identity.sh" bash -n "${hook}" echo "OK: commit-hooks" diff --git a/scripts/ci/ensure-cursor-commit-hooks.sh b/scripts/ci/ensure-cursor-commit-hooks.sh index 754a7261..73b225b7 100755 --- a/scripts/ci/ensure-cursor-commit-hooks.sh +++ b/scripts/ci/ensure-cursor-commit-hooks.sh @@ -1,18 +1,4 @@ #!/usr/bin/env bash -# -# Wire repo commit-msg hooks into Cursor Cloud's managed hooksPath so the -# initiator Co-authored-by trailer is stripped after Cursor re-adds it. -# -# Idempotent. Safe to run from Agent beforeShellExecution, environment -# start, or manually. -# -# What it does: -# 1. Points the Cursor dispatcher's .cursor-original-hooks-path at -# /.githooks so our commit-msg runs in the chain. -# 2. Installs commit-msg.cursor.zz-strip-coauthor into the managed -# hooks dir (alphabetically after commit-msg.cursor.co-author) that -# strips Co-authored-by: Zhe Li . -# set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -29,13 +15,11 @@ chmod +x "${githooks}/commit-msg" "${strip_src}" 2>/dev/null || true hooks_path="$(git -C "${repo_root}" config --get core.hooksPath || true)" if [ -z "${hooks_path}" ]; then - # Local / non-Cursor: use .githooks directly. git -C "${repo_root}" config core.hooksPath "${githooks}" echo "set core.hooksPath=${githooks}" exit 0 fi -# Resolve relative hooksPath against the repo. case "${hooks_path}" in /*) managed="${hooks_path}" ;; *) managed="${repo_root}/${hooks_path}" ;; @@ -46,7 +30,6 @@ if [ ! -d "${managed}" ]; then exit 0 fi -# Only touch Cursor-managed dispatcher trees. if [ ! -f "${managed}/.dispatcher" ] && [ ! -f "${managed}/.cursor-original-hooks-path" ]; then echo "core.hooksPath=${hooks_path} is not a Cursor agent-hooks dir; leaving as-is" exit 0 @@ -59,8 +42,6 @@ echo "pointed .cursor-original-hooks-path -> ${githooks}" zz_hook="${managed}/commit-msg.cursor.zz-strip-coauthor" cat > "${zz_hook}" < Date: Wed, 18 Nov 2026 16:17:55 +0000 Subject: [PATCH 3/3] ci: rename ai-tool-identity.sh to coding-tool-identity.sh --- .cursor/hooks/check-git-commit.sh | 14 +++++++------- scripts/ci/check-authorship.sh | 6 +++--- ...ai-tool-identity.sh => coding-tool-identity.sh} | 4 ++-- scripts/ci/commit-hooks_test.sh | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) rename scripts/ci/{ai-tool-identity.sh => coding-tool-identity.sh} (75%) diff --git a/.cursor/hooks/check-git-commit.sh b/.cursor/hooks/check-git-commit.sh index b5683630..2b2c727f 100755 --- a/.cursor/hooks/check-git-commit.sh +++ b/.cursor/hooks/check-git-commit.sh @@ -3,8 +3,8 @@ set -euo pipefail script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "${script_dir}/../.." && pwd)" -# shellcheck source=../../scripts/ci/ai-tool-identity.sh -source "${repo_root}/scripts/ci/ai-tool-identity.sh" +# shellcheck source=../../scripts/ci/coding-tool-identity.sh +source "${repo_root}/scripts/ci/coding-tool-identity.sh" input="$(cat)" command="$(printf '%s' "${input}" | jq -r '.command // empty')" @@ -29,14 +29,14 @@ if [ -x "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" ]; then bash "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" >/dev/null 2>&1 || true fi -identity_is_ai() { - printf '%s' "$1" | grep -Eiq "${AI_TOOL_IDENTITY_PATTERN}" +identity_is_coding_tool() { + printf '%s' "$1" | grep -Eiq "${CODING_TOOL_IDENTITY_PATTERN}" } resolve_identity_part() { local var="$1" env_val="$2" local from_cmd - from_cmd="$(ai_tool_identity_from_command "${var}" "${command}")" + from_cmd="$(env_value_from_command "${var}" "${command}")" if [ -n "${from_cmd}" ]; then printf '%s' "${from_cmd}" return @@ -63,8 +63,8 @@ committer_email="$(resolve_identity_part GIT_COMMITTER_EMAIL "${GIT_COMMITTER_EM author="${author_name} <${author_email}>" committer="${committer_name} <${committer_email}>" -if identity_is_ai "${author}" || identity_is_ai "${committer}"; then - deny "git commit blocked: author/committer looks like an AI tool (${author} / ${committer}). Prefix the same shell command with human GIT_AUTHOR_* and GIT_COMMITTER_* (the gate reads those from the command text). Example: +if identity_is_coding_tool "${author}" || identity_is_coding_tool "${committer}"; then + deny "git commit blocked: author/committer looks like a coding tool (${author} / ${committer}). Prefix the same shell command with human GIT_AUTHOR_* and GIT_COMMITTER_* (the gate reads those from the command text). Example: GIT_AUTHOR_NAME='Zhe Li' GIT_AUTHOR_EMAIL='linuxcity.jn@gmail.com' \\ GIT_COMMITTER_NAME='Zhe Li' GIT_COMMITTER_EMAIL='linuxcity.jn@gmail.com' \\ git commit ..." diff --git a/scripts/ci/check-authorship.sh b/scripts/ci/check-authorship.sh index d8c74661..2afe0b89 100755 --- a/scripts/ci/check-authorship.sh +++ b/scripts/ci/check-authorship.sh @@ -9,9 +9,9 @@ set -euo pipefail _ci_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -# shellcheck source=ai-tool-identity.sh -source "${_ci_dir}/ai-tool-identity.sh" -PATTERN="${AI_TOOL_IDENTITY_PATTERN}" +# shellcheck source=coding-tool-identity.sh +source "${_ci_dir}/coding-tool-identity.sh" +PATTERN="${CODING_TOOL_IDENTITY_PATTERN}" usage() { echo "usage: $0 [head-ref]" >&2 diff --git a/scripts/ci/ai-tool-identity.sh b/scripts/ci/coding-tool-identity.sh similarity index 75% rename from scripts/ci/ai-tool-identity.sh rename to scripts/ci/coding-tool-identity.sh index ff173beb..b96ca15d 100755 --- a/scripts/ci/ai-tool-identity.sh +++ b/scripts/ci/coding-tool-identity.sh @@ -1,9 +1,9 @@ #!/usr/bin/env bash # shellcheck shell=bash -AI_TOOL_IDENTITY_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' +CODING_TOOL_IDENTITY_PATTERN='(^|[^[:alnum:]])(claude|anthropic|cursor|cursoragent)([^[:alnum:]]|$)' -ai_tool_identity_from_command() { +env_value_from_command() { local var="$1" text="$2" if [[ "${text}" =~ (^|[[:space:]])${var}=\'([^\']*)\' ]]; then printf '%s' "${BASH_REMATCH[2]}" diff --git a/scripts/ci/commit-hooks_test.sh b/scripts/ci/commit-hooks_test.sh index 66d791bb..fed1d6c4 100755 --- a/scripts/ci/commit-hooks_test.sh +++ b/scripts/ci/commit-hooks_test.sh @@ -137,7 +137,7 @@ hooks_mutated=0 bash -n "${repo_root}/.githooks/commit-msg" bash -n "${repo_root}/.githooks/strip-coauthored-by.sh" bash -n "${repo_root}/scripts/ci/ensure-cursor-commit-hooks.sh" -bash -n "${repo_root}/scripts/ci/ai-tool-identity.sh" +bash -n "${repo_root}/scripts/ci/coding-tool-identity.sh" bash -n "${hook}" echo "OK: commit-hooks"