run-all.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env bash
  2. # With/without A/B (and optional interactive) eval for a codegraph version on a
  3. # repo. Codegraph is the ONLY variable: both arms launch claude with
  4. # --strict-mcp-config — with = codegraph-only MCP (pointed at $CG_BIN),
  5. # without = empty MCP. Built-in Read/Grep/Bash stay available in both arms.
  6. #
  7. # Usage: run-all.sh <repo-path> "<question>" [headless|tmux|all]
  8. #
  9. # MULTI-TURN: separate questions with "||" to run them as ONE session —
  10. # run-all.sh <repo> "How does X work?||Where is Y handled in that path?"
  11. # Turn 1 runs normally; every later turn `--resume`s the same session, so the
  12. # earlier turns' tool output is still in the window (that is the whole point:
  13. # residual context occupancy, the cost a single-question run cannot see).
  14. # Segments land in run-<label>.jsonl, run-<label>.t2.jsonl, … and parse-run.mjs
  15. # stitches them back into one session.
  16. #
  17. # Env: CG_BIN codegraph binary (default: command -v codegraph)
  18. # AGENT_EVAL_OUT output dir (default: /tmp/agent-eval)
  19. # MODEL / EFFORT claude model/effort (default: sonnet / high — the
  20. # standing A/B policy; see CLAUDE.md, don't raise)
  21. set -uo pipefail
  22. REPO="${1:?usage: run-all.sh <repo-path> \"<question>\" [headless|tmux|all]}"
  23. Q="${2:?question required}"
  24. MODE="${3:-headless}"
  25. # Split "Q1||Q2||Q3" into turns (kept bash-3.2-safe: macOS ships 3.2).
  26. TURNS=()
  27. rest="$Q"
  28. while [ "$rest" != "${rest#*||}" ]; do
  29. TURNS+=("${rest%%||*}")
  30. rest="${rest#*||}"
  31. done
  32. TURNS+=("$rest")
  33. CG_BIN="${CG_BIN:-$(command -v codegraph)}"
  34. OUT="${AGENT_EVAL_OUT:-/tmp/agent-eval}"
  35. HARNESS="$(cd "$(dirname "$0")" && pwd)"
  36. mkdir -p "$OUT"
  37. # Neutralize any ambient CodeGraph prompt-hook (~/.claude) in BOTH arms:
  38. # the hook injects codegraph context into every prompt, which contaminates
  39. # the without-arm (free structural context) and double-counts the with-arm.
  40. # The A/B's only variable must be the MCP server wired below.
  41. export CODEGRAPH_NO_PROMPT_HOOK=1
  42. [ -n "$CG_BIN" ] || { echo "no codegraph binary on PATH (set CG_BIN)"; exit 1; }
  43. [ -d "$REPO/.codegraph" ] || { echo "no .codegraph index at $REPO — index it first"; exit 1; }
  44. case "$MODE" in headless|tmux|all) ;; *) echo "mode must be headless|tmux|all (got '$MODE')"; exit 1;; esac
  45. # MCP config files (path form avoids inline-JSON quoting through tmux).
  46. cat > "$OUT/mcp-codegraph.json" <<JSON
  47. {"mcpServers":{"codegraph":{"command":"$CG_BIN","args":["serve","--mcp","--path","$REPO"]}}}
  48. JSON
  49. echo '{"mcpServers":{}}' > "$OUT/mcp-empty.json"
  50. echo "###### codegraph: $CG_BIN"
  51. echo "###### repo: $REPO"
  52. echo "###### turns: ${#TURNS[@]}"
  53. for t in "${TURNS[@]}"; do echo "###### - $t"; done
  54. echo
  55. # Pull the session id out of a segment's result event so the next turn can
  56. # --resume it (rather than minting a --session-id, which needs a valid uuid).
  57. session_id_of() {
  58. node -e '
  59. const fs=require("fs");
  60. for (const l of fs.readFileSync(process.argv[1],"utf8").split("\n").reverse()) {
  61. if (!l) continue; let e; try { e=JSON.parse(l) } catch { continue }
  62. if (e.session_id) { console.log(e.session_id); break }
  63. }' "$1" 2>/dev/null
  64. }
  65. # Headless arm: claude -p with stream-json -> exact tool sequence + tokens/cost
  66. # + residual context occupancy. One session, one segment file per turn.
  67. headless() {
  68. local label="$1" cfg="$2"
  69. echo "############################## HEADLESS [$label] ##############################"
  70. local sid="" seg=0 out="" files=()
  71. : > "$OUT/run-$label.err"
  72. for q in "${TURNS[@]}"; do
  73. seg=$((seg + 1))
  74. out="$OUT/run-$label.jsonl"
  75. [ "$seg" -gt 1 ] && out="$OUT/run-$label.t$seg.jsonl"
  76. local resume=()
  77. [ -n "$sid" ] && resume=(--resume "$sid")
  78. ( cd "$REPO" && claude -p "$q" \
  79. --output-format stream-json --verbose \
  80. --permission-mode bypassPermissions \
  81. --model "${MODEL:-sonnet}" --effort "${EFFORT:-high}" \
  82. --max-budget-usd 4 \
  83. --strict-mcp-config --mcp-config "$cfg" \
  84. ${resume[@]+"${resume[@]}"} \
  85. </dev/null > "$out" 2>>"$OUT/run-$label.err" )
  86. echo "exit $? -> $out ($(wc -l < "$out" | tr -d ' ') lines) [turn $seg/${#TURNS[@]}]"
  87. files+=("$out")
  88. sid="$(session_id_of "$out")"
  89. if [ -z "$sid" ] && [ "$seg" -lt "${#TURNS[@]}" ]; then
  90. echo " WARN: no session_id in $out — later turns would start a FRESH context; stopping this arm"
  91. break
  92. fi
  93. done
  94. tail -2 "$OUT/run-$label.err" 2>/dev/null
  95. node "$HARNESS/parse-run.mjs" "${files[@]}" 2>&1 || true
  96. echo
  97. }
  98. if [ "$MODE" = headless ] || [ "$MODE" = all ]; then
  99. headless "headless-with" "$OUT/mcp-codegraph.json"
  100. headless "headless-without" "$OUT/mcp-empty.json"
  101. fi
  102. if [ "$MODE" = tmux ] || [ "$MODE" = all ]; then
  103. echo "############################## INTERACTIVE [with] ##############################"
  104. CLAUDE_EXTRA_ARGS="--model ${MODEL:-sonnet} --effort ${EFFORT:-high} --strict-mcp-config --mcp-config $OUT/mcp-codegraph.json" \
  105. bash "$HARNESS/itrun.sh" "$REPO" "int-with" "${TURNS[0]}" 2>&1 || echo "[itrun WITH failed]"
  106. echo
  107. echo "############################## INTERACTIVE [without] ##############################"
  108. CLAUDE_EXTRA_ARGS="--model ${MODEL:-sonnet} --effort ${EFFORT:-high} --strict-mcp-config --mcp-config $OUT/mcp-empty.json" \
  109. bash "$HARNESS/itrun.sh" "$REPO" "int-without" "${TURNS[0]}" 2>&1 || echo "[itrun WITHOUT failed]"
  110. echo
  111. fi
  112. echo "############################## RUN-ALL COMPLETE ##############################"