diff --git a/.devcontainer/normalize-git-identity.sh b/.devcontainer/normalize-git-identity.sh new file mode 100644 index 00000000000000..d7ec0896932b75 --- /dev/null +++ b/.devcontainer/normalize-git-identity.sh @@ -0,0 +1,68 @@ +#!/bin/sh +set -eu + +is_valid_email() { + case "${1:-}" in + ""|*"(none)"*|*" "*|@*|*@|*@*@*) return 1 ;; + *@*) return 0 ;; + *) return 1 ;; + esac +} + +clean_malformed_local_email() { + if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + return 0 + fi + + local_email="$(git config --local --get user.email 2>/dev/null || true)" + if [ -n "$local_email" ] && ! is_valid_email "$local_email"; then + git config --local --unset-all user.email 2>/dev/null || true + printf '%s\n' "Removed malformed repository-local Git email override: $local_email" + fi +} + +normalize_git_identity() { + # A malformed repository-local value overrides a valid global identity, so + # clean it before deciding whether the global configuration can be preserved. + clean_malformed_local_email + + current_name="$(git config --global --get user.name 2>/dev/null || true)" + current_email="$(git config --global --get user.email 2>/dev/null || true)" + + if [ -n "$current_name" ] && is_valid_email "$current_email"; then + printf '%s\n' "Git identity already configured: $current_name <$current_email>" + return 0 + fi + + login="${GITHUB_USER:-${GITHUB_ACTOR:-}}" + account_id="" + display_name="" + + if command -v gh >/dev/null 2>&1; then + login_from_api="$(gh api user --jq '.login' 2>/dev/null || true)" + account_id="$(gh api user --jq '.id' 2>/dev/null || true)" + display_name="$(gh api user --jq '.name // .login' 2>/dev/null || true)" + [ -n "$login_from_api" ] && login="$login_from_api" + fi + + if [ -z "$login" ]; then + printf '%s\n' "WARNING: Git identity is missing and the GitHub login could not be resolved." + printf '%s\n' "Run: git config --global user.name ''" + printf '%s\n' " git config --global user.email ''" + return 0 + fi + + [ -n "$display_name" ] || display_name="$login" + if [ -n "$account_id" ]; then + noreply_email="${account_id}+${login}@users.noreply.github.com" + else + noreply_email="${login}@users.noreply.github.com" + fi + + git config --global user.name "$display_name" + git config --global user.email "$noreply_email" + + printf '%s\n' "Configured Git identity: $(git var GIT_AUTHOR_IDENT)" +} + +normalize_git_identity diff --git a/.devcontainer/post-create.sh b/.devcontainer/post-create.sh index 4e39a71c49d265..ca7c8f13833e47 100755 --- a/.devcontainer/post-create.sh +++ b/.devcontainer/post-create.sh @@ -1,4 +1,8 @@ #!/bin/sh +set -eu + +SCRIPT_DIR="$(CDPATH= cd "$(dirname "$0")" && pwd)" +sh "$SCRIPT_DIR/normalize-git-identity.sh" npm i npm run electron diff --git a/.devcontainer/test-normalize-git-identity.sh b/.devcontainer/test-normalize-git-identity.sh new file mode 100644 index 00000000000000..1b845cd51bcee3 --- /dev/null +++ b/.devcontainer/test-normalize-git-identity.sh @@ -0,0 +1,102 @@ +#!/bin/sh +set -eu + +SCRIPT_DIR="$(CDPATH= cd "$(dirname "$0")" && pwd)" +SCRIPT="$SCRIPT_DIR/normalize-git-identity.sh" +ORIGINAL_PATH="$PATH" +TMP_ROOT="$(mktemp -d)" +trap 'rm -rf "$TMP_ROOT"' EXIT HUP INT TERM + +CASE_NO=0 +HOME_DIR="" +BIN_DIR="" +REPO_DIR="" + +fail() { + printf '%s\n' "FAIL: $*" >&2 + exit 1 +} + +assert_eq() { + expected="$1" + actual="$2" + label="$3" + [ "$expected" = "$actual" ] || fail "$label: expected '$expected', got '$actual'" +} + +new_case() { + CASE_NO=$((CASE_NO + 1)) + case_dir="$TMP_ROOT/case-$CASE_NO" + HOME_DIR="$case_dir/home" + BIN_DIR="$case_dir/bin" + REPO_DIR="$case_dir/repo" + mkdir -p "$HOME_DIR" "$BIN_DIR" "$REPO_DIR" + git -C "$REPO_DIR" init -q +} + +write_fake_gh_profile() { + cat > "$BIN_DIR/gh" <<'EOF' +#!/bin/sh +case "$*" in + *"--jq .login") printf '%s\n' 'NguyenCuong1989' ;; + *"--jq .id") printf '%s\n' '196793856' ;; + *"--jq .name // .login") printf '%s\n' 'NgCuong' ;; + *) exit 1 ;; +esac +EOF + chmod +x "$BIN_DIR/gh" +} + +write_failing_gh() { + cat > "$BIN_DIR/gh" <<'EOF' +#!/bin/sh +exit 1 +EOF + chmod +x "$BIN_DIR/gh" +} + +run_normalizer() { + ( + cd "$REPO_DIR" + HOME="$HOME_DIR" \ + PATH="$BIN_DIR:$ORIGINAL_PATH" \ + GITHUB_USER="${GITHUB_USER_OVERRIDE:-}" \ + GITHUB_ACTOR="" \ + sh "$SCRIPT" + ) +} + +printf '%s\n' "[1/4] Preserve a valid global identity and remove a malformed local override" +new_case +HOME="$HOME_DIR" git config --global user.name 'Existing User' +HOME="$HOME_DIR" git config --global user.email 'existing@example.com' +git -C "$REPO_DIR" config user.email 'pc@ai.(none)' +run_normalizer >/dev/null +assert_eq 'Existing User' "$(HOME="$HOME_DIR" git config --global --get user.name)" 'preserved global name' +assert_eq 'existing@example.com' "$(HOME="$HOME_DIR" git config --global --get user.email)" 'preserved global email' +assert_eq '' "$(git -C "$REPO_DIR" config --local --get user.email 2>/dev/null || true)" 'removed malformed local email' + +printf '%s\n' "[2/4] Resolve identity from the authenticated GitHub profile" +new_case +write_fake_gh_profile +run_normalizer >/dev/null +assert_eq 'NgCuong' "$(HOME="$HOME_DIR" git config --global --get user.name)" 'GitHub display name' +assert_eq '196793856+NguyenCuong1989@users.noreply.github.com' "$(HOME="$HOME_DIR" git config --global --get user.email)" 'GitHub noreply email' + +printf '%s\n' "[3/4] Fall back to the Codespaces login when GitHub API resolution fails" +new_case +write_failing_gh +GITHUB_USER_OVERRIDE='NguyenCuong1989' +run_normalizer >/dev/null +unset GITHUB_USER_OVERRIDE +assert_eq 'NguyenCuong1989' "$(HOME="$HOME_DIR" git config --global --get user.name)" 'fallback name' +assert_eq 'NguyenCuong1989@users.noreply.github.com' "$(HOME="$HOME_DIR" git config --global --get user.email)" 'fallback noreply email' + +printf '%s\n' "[4/4] Remain non-blocking when no GitHub identity can be resolved" +new_case +write_failing_gh +run_normalizer >/dev/null +assert_eq '' "$(HOME="$HOME_DIR" git config --global --get user.name 2>/dev/null || true)" 'missing unresolved name' +assert_eq '' "$(HOME="$HOME_DIR" git config --global --get user.email 2>/dev/null || true)" 'missing unresolved email' + +printf '%s\n' 'PASS: Git identity normalization tests completed.' diff --git a/GUARDRAILS.md b/GUARDRAILS.md new file mode 100644 index 00000000000000..e4a1dae1d95d24 --- /dev/null +++ b/GUARDRAILS.md @@ -0,0 +1,13 @@ +# AI Agent Guardrails + +**CRITICAL: ALL AI AGENTS MUST COMPLY WITH THESE RULES** + +This file defines safety restrictions that AI agents MUST follow when working in this workspace. + +- **Follow prompt instructions EXACTLY** +- **No destructive commands** (e.g., rm -rf /) +- **Protect all secrets** (Never expose .env or tokens) +- **Commands must be non-interactive** +- **Respect Branch Protection & Auto-merge rules** + +By adhering to this, the repository maintains a 100% autonomous and safe execution environment.