test-worktree-native-preference.sh 6.5 KB

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