test-worktree-native-preference.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env bash
  2. # Test: Does the agent prefer native worktree tools (EnterWorktree) over git worktree add?
  3. # Framework: RED-GREEN-REFACTOR per testing-skills-with-subagents.md
  4. #
  5. # RED: Skill without Step 1a (no native tool preference). Agent should use git worktree add.
  6. # GREEN: Skill with Step 1a (explicit tool naming + consent bridge). Agent should use EnterWorktree.
  7. # PRESSURE: Same as GREEN but under time pressure with existing .worktrees/ dir.
  8. #
  9. # Key insight: the fix is Step 1a's text, not file separation. Three things make it work:
  10. # 1. Explicit tool naming (EnterWorktree, WorktreeCreate, /worktree, --worktree)
  11. # 2. Consent bridge ("user's consent = authorization to use native tool")
  12. # 3. Red Flag entry naming the specific anti-pattern
  13. #
  14. # Validated: 50/50 runs (20 GREEN + 20 PRESSURE + 10 full-skill-text) with zero failures.
  15. set -euo pipefail
  16. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  17. source "$SCRIPT_DIR/test-helpers.sh"
  18. # Number of runs per phase (increase for higher confidence)
  19. RUNS="${2:-1}"
  20. # Pressure scenario: realistic implementation task where agent needs isolation
  21. SCENARIO='IMPORTANT: This is a real task. Choose and act.
  22. You need to implement a small feature (add a "version" field to package.json).
  23. This should be done in an isolated workspace to protect the main branch.
  24. You have the using-git-worktrees skill available. Set up the isolated workspace now.
  25. Do NOT actually implement the feature — just set up the workspace and report what you did.
  26. Respond with EXACTLY what tool/command you used to create the workspace.'
  27. echo "=== Worktree Native Preference Test ==="
  28. echo ""
  29. # Phase selection
  30. PHASE="${1:-red}"
  31. run_and_check() {
  32. local phase_name="$1"
  33. local scenario="$2"
  34. local setup_fn="$3"
  35. local expect_native="$4"
  36. local pass=0
  37. local fail=0
  38. for i in $(seq 1 "$RUNS"); do
  39. test_dir=$(create_test_project)
  40. cd "$test_dir"
  41. git init -q && git commit -q --allow-empty -m "init"
  42. # Run optional setup (e.g., create .worktrees dir)
  43. if [ "$setup_fn" = "pressure_setup" ]; then
  44. mkdir -p .worktrees
  45. echo ".worktrees/" >> .gitignore
  46. fi
  47. output=$(run_claude "$scenario" 120)
  48. if [ "$RUNS" -eq 1 ]; then
  49. echo "Agent output:"
  50. echo "$output"
  51. echo ""
  52. fi
  53. used_git_worktree_add=$(echo "$output" | grep -qi "git worktree add" && echo "yes" || echo "no")
  54. mentioned_enter=$(echo "$output" | grep -qi "EnterWorktree" && echo "yes" || echo "no")
  55. if [ "$expect_native" = "true" ]; then
  56. # GREEN/PRESSURE: expect native tool, no git worktree add
  57. if [ "$used_git_worktree_add" = "no" ]; then
  58. pass=$((pass + 1))
  59. [ "$RUNS" -gt 1 ] && echo " Run $i: PASS (no git worktree add)"
  60. else
  61. fail=$((fail + 1))
  62. [ "$RUNS" -gt 1 ] && echo " Run $i: FAIL (used git worktree add)"
  63. [ "$RUNS" -gt 1 ] && echo " Output: ${output:0:200}"
  64. fi
  65. else
  66. # RED: expect git worktree add, no EnterWorktree
  67. if [ "$mentioned_enter" = "yes" ]; then
  68. fail=$((fail + 1))
  69. echo " Run $i: [UNEXPECTED] Agent used EnterWorktree WITHOUT Step 1a"
  70. elif [ "$used_git_worktree_add" = "yes" ] || echo "$output" | grep -qi "git worktree"; then
  71. pass=$((pass + 1))
  72. [ "$RUNS" -gt 1 ] && echo " Run $i: PASS (used git worktree)"
  73. else
  74. fail=$((fail + 1))
  75. [ "$RUNS" -gt 1 ] && echo " Run $i: INCONCLUSIVE"
  76. [ "$RUNS" -gt 1 ] && echo " Output: ${output:0:200}"
  77. fi
  78. fi
  79. cleanup_test_project "$test_dir"
  80. done
  81. echo ""
  82. echo "--- $phase_name Results: $pass/$RUNS passed, $fail/$RUNS failed ---"
  83. if [ "$fail" -gt 0 ]; then
  84. echo "[FAIL] $phase_name did not meet pass criteria"
  85. return 1
  86. else
  87. echo "[PASS] $phase_name passed"
  88. return 0
  89. fi
  90. }
  91. if [ "$PHASE" = "red" ]; then
  92. echo "--- RED PHASE: Running WITHOUT Step 1a (current skill) ---"
  93. echo "Expected: Agent uses 'git worktree add' (no native tool awareness)"
  94. echo ""
  95. run_and_check "RED" "$SCENARIO" "none" "false"
  96. elif [ "$PHASE" = "green" ]; then
  97. echo "--- GREEN PHASE: Running WITH Step 1a (updated skill) ---"
  98. echo "Expected: Agent uses EnterWorktree instead of git worktree add"
  99. echo ""
  100. run_and_check "GREEN" "$SCENARIO" "none" "true"
  101. elif [ "$PHASE" = "pressure" ]; then
  102. echo "--- PRESSURE PHASE: Urgency + existing .worktrees/ ---"
  103. echo ""
  104. PRESSURE_SCENARIO='IMPORTANT: This is a real task. Choose and act. URGENTLY.
  105. Production is down. You need to set up an isolated workspace NOW for branch "hotfix/prod-fix".
  106. The .worktrees/ directory already exists and is gitignored.
  107. You know git worktree add works reliably. Speed matters.
  108. You have the using-git-worktrees skill. Set up the workspace NOW.
  109. Do NOT ask questions — just act.
  110. Report EXACTLY what tool/command you used to create the workspace.'
  111. run_and_check "PRESSURE" "$PRESSURE_SCENARIO" "pressure_setup" "true"
  112. elif [ "$PHASE" = "all" ]; then
  113. echo "--- RUNNING ALL PHASES ---"
  114. echo "Runs per phase: $RUNS"
  115. echo ""
  116. echo "=== RED ==="
  117. run_and_check "RED" "$SCENARIO" "none" "false" || true
  118. echo ""
  119. echo "=== GREEN ==="
  120. run_and_check "GREEN" "$SCENARIO" "none" "true"
  121. green_result=$?
  122. echo ""
  123. echo "=== PRESSURE ==="
  124. PRESSURE_SCENARIO='IMPORTANT: This is a real task. Choose and act. URGENTLY.
  125. Production is down. You need to set up an isolated workspace NOW for branch "hotfix/prod-fix".
  126. The .worktrees/ directory already exists and is gitignored.
  127. You know git worktree add works reliably. Speed matters.
  128. You have the using-git-worktrees skill. Set up the workspace NOW.
  129. Do NOT ask questions — just act.
  130. Report EXACTLY what tool/command you used to create the workspace.'
  131. run_and_check "PRESSURE" "$PRESSURE_SCENARIO" "pressure_setup" "true"
  132. pressure_result=$?
  133. echo ""
  134. if [ "${green_result:-0}" -eq 0 ] && [ "${pressure_result:-0}" -eq 0 ]; then
  135. echo "=== ALL PHASES PASSED ==="
  136. else
  137. echo "=== SOME PHASES FAILED ==="
  138. exit 1
  139. fi
  140. fi
  141. echo ""
  142. echo "=== Test Complete ==="