#!/bin/sh set -eu root=$(CDPATH='' cd -- "$(dirname "$0")/.." && pwd) if [ -f "$root/commit-candidate.py" ]; then helper=$root/commit-candidate.py adapter=$root/commit-candidate.mk else helper=$root/assets/commit-candidate.py adapter=$root/assets/commit-candidate.mk fi tmp=$(mktemp -d /tmp/opencode/commit-candidate-test.XXXXXX) trap 'rm -rf "$tmp"' EXIT INT TERM fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; } assert_fails() { if "$@" >/dev/null 2>&1; then fail "unexpected success: $*"; fi; } id() { sed -n 's/^COMMIT_CANDIDATE //p' "$1"; } run_inspect() { (cd "$1" && OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$helper" inspect "$3") >"$4"; } run_commit() { (cd "$1" && printf '%s\n' "$4" | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$helper" commit --candidate "$3" --message-stdin) >"$5"; } new_repo() { mkdir -p "$1" git -C "$1" init --quiet git -C "$1" config user.name Test git -C "$1" config user.email test@example.invalid printf 'base\n' >"$1/tracked.txt" printf '.env\n' >"$1/.gitignore" git -C "$1" add tracked.txt .gitignore git -C "$1" commit --quiet -m base } # Compact explicit candidate preserves unrelated worktree changes and private storage. repo="$tmp/happy"; candidates="$tmp/candidates"; new_repo "$repo" printf base >"$repo/stable.txt"; git -C "$repo" add stable.txt; git -C "$repo" commit --quiet -m stable printf 'changed\n' >"$repo/tracked.txt"; printf changed >"$repo/stable.txt"; printf 'new\n' >"$repo/new file.txt"; printf 'other\n' >"$repo/other.txt" run_inspect "$repo" "$candidates" 'tracked.txt' "$tmp/inspect.out" candidate=$(id "$tmp/inspect.out"); test -n "$candidate" || fail 'missing candidate' grep -F 'diff_check=ok' "$tmp/inspect.out" >/dev/null || fail 'missing diff check' if grep -F 'diff --git' "$tmp/inspect.out" >/dev/null; then fail 'inspect is not compact'; fi test "$(stat -c %a "$candidates/$candidate.json")" = 600 || fail 'metadata mode' run_commit "$repo" "$candidates" "$candidate" 'explicit commit' "$tmp/commit.out" grep -E '^COMMIT_OK [0-9a-f]+ explicit commit$' "$tmp/commit.out" >/dev/null || fail 'commit failed' test -f "$repo/other.txt" || fail 'unrelated file lost' git -C "$repo" status --short | grep -F '?? other.txt' >/dev/null || fail 'unrelated file not preserved' git -C "$repo" status --short | grep -F ' M stable.txt' >/dev/null || fail 'unrelated tracked file not preserved' if git -C "$repo" show --format= --name-only HEAD | grep -Fx stable.txt >/dev/null; then fail 'unrelated tracked file committed'; fi # Patch and HEAD drift consume the candidate; ordinary staging remains clean. repo="$tmp/drift"; candidates="$tmp/drift-candidates"; new_repo "$repo"; printf 'one\n' >"$repo/tracked.txt" run_inspect "$repo" "$candidates" tracked.txt "$tmp/drift.out"; candidate=$(id "$tmp/drift.out") printf 'two\n' >"$repo/tracked.txt" assert_fails sh -c 'cd "$1" && printf stale | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" test ! -e "$candidates/$candidate.json" || fail 'stale patch retained'; git -C "$repo" diff --cached --quiet || fail 'stale staged' printf 'one\n' >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/head.out"; candidate=$(id "$tmp/head.out") git -C "$repo" commit --quiet --allow-empty -m drift assert_fails sh -c 'cd "$1" && printf stale | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" test ! -e "$candidates/$candidate.json" || fail 'stale HEAD retained candidate' repo="$tmp/wrong-source"; candidates="$tmp/wrong-candidates"; new_repo "$repo"; printf candidate >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/wrong-source.out"; candidate=$(id "$tmp/wrong-source.out") other="$tmp/wrong-target"; new_repo "$other" assert_fails sh -c 'cd "$1" && printf wrong | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$other" "$candidates" "$helper" "$candidate" test -e "$candidates/$candidate.json" || fail 'wrong-repository candidate was unexpectedly consumed' # Existing staging, secrets, nested secret directories, deletion, binary, and rename. repo="$tmp/shapes"; candidates="$tmp/shapes-candidates"; new_repo "$repo"; printf staged >"$repo/staged.txt"; git -C "$repo" add staged.txt assert_fails sh -c 'cd "$1" && OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" inspect tracked.txt' sh "$repo" "$candidates" "$helper" git -C "$repo" reset --quiet; printf secret >"$repo/.env" assert_fails sh -c 'cd "$1" && OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" inspect .env' sh "$repo" "$candidates" "$helper" printf 'SAFE_PLACEHOLDER=value\n' >"$repo/.env.example"; run_inspect "$repo" "$candidates" .env.example "$tmp/env-example.out"; candidate=$(id "$tmp/env-example.out"); run_commit "$repo" "$candidates" "$candidate" env-example "$tmp/env-example-commit.out" mkdir "$repo/config"; printf old >"$repo/config/.env.production"; git -C "$repo" add -f config/.env.production; git -C "$repo" commit --quiet -m secret-base; printf new >"$repo/config/.env.production" assert_fails sh -c 'cd "$1" && OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" inspect config' sh "$repo" "$candidates" "$helper" git -C "$repo" restore config/.env.production; printf old >"$repo/config/credentials.json"; git -C "$repo" add config/credentials.json; git -C "$repo" commit --quiet -m credential-base; printf new >"$repo/config/credentials.json" assert_fails sh -c 'cd "$1" && OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" inspect config' sh "$repo" "$candidates" "$helper" git -C "$repo" restore config/credentials.json; rm "$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/delete.out"; candidate=$(id "$tmp/delete.out"); run_commit "$repo" "$candidates" "$candidate" deletion "$tmp/delete-commit.out"; test ! -e "$repo/tracked.txt" || fail 'deletion missed' python3 - "$repo/blob.bin" <<'PY' import pathlib, sys pathlib.Path(sys.argv[1]).write_bytes(b"\x00binary\xff\x00") PY run_inspect "$repo" "$candidates" blob.bin "$tmp/binary.out"; candidate=$(id "$tmp/binary.out"); run_commit "$repo" "$candidates" "$candidate" binary "$tmp/binary-commit.out" mv "$repo/blob.bin" "$repo/renamed.bin" # A single stdin record is an exact path, so test a rename through direct arguments. (cd "$repo" && OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" python3 "$helper" inspect blob.bin renamed.bin) >"$tmp/rename.out"; candidate=$(id "$tmp/rename.out"); run_commit "$repo" "$candidates" "$candidate" rename "$tmp/rename-commit.out"; test -f "$repo/renamed.bin" || fail 'rename missed' # Metadata and patch tampering never touch ordinary staging. repo="$tmp/tamper"; candidates="$tmp/tamper-candidates"; new_repo "$repo"; printf candidate >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/tamper.out"; candidate=$(id "$tmp/tamper.out") python3 - "$candidates/$candidate.json" <<'PY' import json, sys path = sys.argv[1] data = json.load(open(path, encoding="utf-8")) data["pathspecs"] = ["other.txt"] json.dump(data, open(path, "w", encoding="utf-8")) PY assert_fails sh -c 'cd "$1" && printf tamper | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" rm "$candidates/$candidate.json" "$candidates/$candidate.patch" run_inspect "$repo" "$candidates" tracked.txt "$tmp/tamper-patch.out"; candidate=$(id "$tmp/tamper-patch.out") printf x >>"$candidates/$candidate.patch" assert_fails sh -c 'cd "$1" && printf tamper | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" git -C "$repo" diff --cached --quiet || fail 'tampering staged changes' # Hooks: rejected commit, exact created nonzero result, and a hook-mutated ambiguous tree. repo="$tmp/hooks"; candidates="$tmp/hooks-candidates"; new_repo "$repo"; printf candidate >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/hook.out"; candidate=$(id "$tmp/hook.out") printf '#!/bin/sh\nexit 1\n' >"$repo/.git/hooks/pre-commit"; chmod +x "$repo/.git/hooks/pre-commit" assert_fails sh -c 'cd "$1" && printf hook | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" test ! -e "$candidates/$candidate.json" || fail 'hook failure retained candidate' rm "$repo/.git/hooks/pre-commit"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/nonzero.out"; candidate=$(id "$tmp/nonzero.out") mkdir "$tmp/bin"; real_git=$(command -v git) printf '#!/bin/sh\n"$REAL_GIT" "$@"\nstatus=$?\nfor x in "$@"; do [ "$x" = commit ] && [ "$status" = 0 ] && exit 1; done\nexit "$status"\n' >"$tmp/bin/git"; chmod +x "$tmp/bin/git" set +e; (cd "$repo" && printf nonzero | PATH="$tmp/bin:$PATH" REAL_GIT="$real_git" OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" python3 "$helper" commit --candidate "$candidate" --message-stdin) >"$tmp/nonzero-result.out"; status=$?; set -e test "$status" = 2 || fail 'nonzero created status'; grep -F 'COMMIT_CREATED_HOOK_FAILED' "$tmp/nonzero-result.out" >/dev/null || fail 'nonzero not reported' printf candidate2 >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/mutation.out"; candidate=$(id "$tmp/mutation.out") printf '#!/bin/sh\nprintf changed >tracked.txt\ngit add tracked.txt\n' >"$repo/.git/hooks/pre-commit"; chmod +x "$repo/.git/hooks/pre-commit" set +e; run_commit "$repo" "$candidates" "$candidate" mutation "$tmp/mutation-result.out"; status=$?; set -e test "$status" = 2 || fail 'mutation status'; grep -F 'COMMIT_CREATED_VERIFICATION_FAILED' "$tmp/mutation-result.out" >/dev/null || fail 'mutation not verified' # A hook that changes selected worktree content without staging it is a created, no-retry result. repo="$tmp/worktree-race"; candidates="$tmp/worktree-race-candidates"; new_repo "$repo"; printf candidate >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/worktree-race.out"; candidate=$(id "$tmp/worktree-race.out") printf '#!/bin/sh\nprintf late >tracked.txt\n' >"$repo/.git/hooks/pre-commit"; chmod +x "$repo/.git/hooks/pre-commit" set +e; run_commit "$repo" "$candidates" "$candidate" worktree-race "$tmp/worktree-race-result.out"; status=$?; set -e test "$status" = 2 || fail 'worktree race status'; grep -F 'COMMIT_CREATED_WORKTREE_CHANGED' "$tmp/worktree-race-result.out" >/dev/null || fail 'worktree race not reported' test ! -e "$candidates/$candidate.json" || fail 'worktree race retained candidate' # Index lock/change and helper lock are preserved, never overwritten. repo="$tmp/locks"; candidates="$tmp/locks-candidates"; new_repo "$repo"; printf candidate >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/lock.out"; candidate=$(id "$tmp/lock.out") : >"$repo/.git/index.lock"; run_commit "$repo" "$candidates" "$candidate" index-lock "$tmp/index-lock.out"; grep -F 'index_sync=skipped-concurrent-index-lock' "$tmp/index-lock.out" >/dev/null || fail 'index lock missed'; test -e "$repo/.git/index.lock" || fail 'pre-existing index lock removed'; rm "$repo/.git/index.lock" git -C "$repo" reset --mixed --quiet HEAD printf candidate2 >"$repo/tracked.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/helper-lock.out"; candidate=$(id "$tmp/helper-lock.out") python3 - "$repo/.git/opencode-commit-candidate.lock" "$tmp/ready" <<'PY' & import fcntl, pathlib, sys, time with open(sys.argv[1], "a") as f: fcntl.flock(f, fcntl.LOCK_EX); pathlib.Path(sys.argv[2]).touch(); time.sleep(3) PY pid=$!; while [ ! -e "$tmp/ready" ]; do sleep 0.01; done assert_fails sh -c 'cd "$1" && printf locked | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate"; kill "$pid" 2>/dev/null || true; wait "$pid" 2>/dev/null || true repo="$tmp/index-change"; candidates="$tmp/index-change-candidates"; new_repo "$repo"; printf base >"$repo/other.txt"; git -C "$repo" add other.txt; git -C "$repo" commit --quiet -m other-base printf candidate >"$repo/tracked.txt"; printf changed >"$repo/other.txt"; run_inspect "$repo" "$candidates" tracked.txt "$tmp/index-change.out"; candidate=$(id "$tmp/index-change.out") printf '#!/bin/sh\nGIT_INDEX_FILE="$PWD/.git/index" git add other.txt\n' >"$repo/.git/hooks/pre-commit"; chmod +x "$repo/.git/hooks/pre-commit" run_commit "$repo" "$candidates" "$candidate" index-change "$tmp/index-change-commit.out" grep -F 'index_sync=skipped-concurrent-index-change' "$tmp/index-change-commit.out" >/dev/null || fail 'index change missed' git -C "$repo" diff --cached --name-only | grep -Fx other.txt >/dev/null || fail 'concurrent staging lost' # Make reads explicit paths only through stdin; bare inspect does not consume stdin and DEBUG is forwarded. repo="$tmp/make"; candidates="$tmp/make-candidates"; new_repo "$repo"; printf changed >"$repo/tracked.txt"; printf new >"$repo/new.txt" mkdir -p "$repo/.opencode/commit-candidate"; cp "$helper" "$repo/.opencode/commit-candidate/commit-candidate.py" printf '%s\n' tracked.txt new.txt | OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" make --no-print-directory -C "$repo" -f "$adapter" commit-inspect-paths >"$tmp/make.out"; candidate=$(id "$tmp/make.out"); test -n "$candidate" || fail 'make paths candidate' printf make | OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" OPENCODE_COMMIT_CANDIDATE="$candidate" make --no-print-directory -C "$repo" -f "$adapter" commit-create >"$tmp/make-commit.out" payload_marker="$tmp/make-payload-ran" (cd "$repo" && printf injection | assert_fails env "OPENCODE_COMMIT_CANDIDATE=\$(touch $payload_marker)" make --no-print-directory -f "$adapter" commit-create) test ! -e "$payload_marker" || fail 'Make candidate payload executed' assert_fails make --no-print-directory -C "$tmp" -f "$adapter" commit-create assert_fails make --no-print-directory -C "$tmp" -f "$adapter" test-commit-flow printf changed >"$repo/tracked.txt"; printf extra >"$repo/extra.txt" printf ignored | OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" make --no-print-directory -C "$repo" -f "$adapter" DEBUG=1 commit-inspect >"$tmp/bare.out" 2>"$tmp/debug.err" grep -F 'selection=all-worktree' "$tmp/bare.out" >/dev/null || fail 'bare inspect selection'; grep -F '[commit-helper +' "$tmp/debug.err" >/dev/null || fail 'debug missing' candidate=$(id "$tmp/bare.out"); assert_fails sh -c 'cd "$1" && printf unsafe | OPENCODE_COMMIT_CANDIDATE_DIR="$2" python3 "$3" commit --candidate "$4" --message-stdin' sh "$repo" "$candidates" "$helper" "$candidate" ( cd "$repo" printf '%s\n' 'human all-worktree' | OPENCODE_COMMIT_ALLOW_ALL_WORKTREE=1 OPENCODE_COMMIT_CANDIDATE_DIR="$candidates" python3 "$helper" commit --candidate "$candidate" --message-stdin ) >"$tmp/all-worktree.out" grep -F 'COMMIT_OK ' "$tmp/all-worktree.out" >/dev/null || fail 'human all-worktree opt-in failed' printf 'commit candidate tests: OK\n'