run-all.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. # Hide the codegraph CLI from BOTH arms, so the only way to reach codegraph is
  43. # the MCP server wired below — which is what makes it the A/B's single variable.
  44. #
  45. # Both arms have Bash, and the target repo carries the .codegraph/ index the
  46. # with-arm needs. Agents FIND that: 14 of 15 without-arm runs in one 7-repo pass
  47. # ran `codegraph explore` through Bash (one via `ls .codegraph && codegraph
  48. # explore …`), so that arm was measuring codegraph-over-CLI, not
  49. # codegraph-absent. It matters in the with-arm too — output that arrives through
  50. # Bash is attributed to Bash, understating what codegraph itself occupies.
  51. #
  52. # The binary usually shares a directory with tools the run needs (claude itself
  53. # lives next to it here), so dropping the whole directory is not an option.
  54. # Substitute an equivalent directory IN PLACE: symlinks to every entry except
  55. # codegraph, keeping PATH order and precedence intact.
  56. SHIM_BIN="$OUT/nocg-bin"
  57. rm -rf "$SHIM_BIN"; mkdir -p "$SHIM_BIN"
  58. sanitized_path() {
  59. local out="" d e
  60. local IFS=:
  61. for d in $PATH; do
  62. [ -n "$d" ] || continue
  63. if [ -x "$d/codegraph" ]; then
  64. for e in "$d"/*; do
  65. [ "$(basename "$e")" = codegraph ] && continue
  66. ln -sf "$e" "$SHIM_BIN/" 2>/dev/null
  67. done
  68. d="$SHIM_BIN"
  69. fi
  70. out="${out:+$out:}$d"
  71. done
  72. printf '%s' "$out"
  73. }
  74. ARM_PATH="$(sanitized_path)"
  75. if PATH="$ARM_PATH" command -v codegraph >/dev/null 2>&1; then
  76. echo "WARNING: 'codegraph' is still on the arm PATH — runs will be contaminated"
  77. fi
  78. for t in claude node; do
  79. PATH="$ARM_PATH" command -v "$t" >/dev/null || { echo "sanitized PATH lost '$t' — refusing to run"; exit 1; }
  80. done
  81. [ -n "$CG_BIN" ] || { echo "no codegraph binary on PATH (set CG_BIN)"; exit 1; }
  82. [ -d "$REPO/.codegraph" ] || { echo "no .codegraph index at $REPO — index it first"; exit 1; }
  83. case "$MODE" in headless|tmux|all) ;; *) echo "mode must be headless|tmux|all (got '$MODE')"; exit 1;; esac
  84. # MCP config files (path form avoids inline-JSON quoting through tmux).
  85. cat > "$OUT/mcp-codegraph.json" <<JSON
  86. {"mcpServers":{"codegraph":{"command":"$CG_BIN","args":["serve","--mcp","--path","$REPO"]}}}
  87. JSON
  88. echo '{"mcpServers":{}}' > "$OUT/mcp-empty.json"
  89. echo "###### codegraph: $CG_BIN"
  90. echo "###### repo: $REPO"
  91. echo "###### turns: ${#TURNS[@]}"
  92. for t in "${TURNS[@]}"; do echo "###### - $t"; done
  93. echo
  94. # Pull the session id out of a segment's result event so the next turn can
  95. # --resume it (rather than minting a --session-id, which needs a valid uuid).
  96. session_id_of() {
  97. node -e '
  98. const fs=require("fs");
  99. for (const l of fs.readFileSync(process.argv[1],"utf8").split("\n").reverse()) {
  100. if (!l) continue; let e; try { e=JSON.parse(l) } catch { continue }
  101. if (e.session_id) { console.log(e.session_id); break }
  102. }' "$1" 2>/dev/null
  103. }
  104. # Headless arm: claude -p with stream-json -> exact tool sequence + tokens/cost
  105. # + residual context occupancy. One session, one segment file per turn.
  106. headless() {
  107. local label="$1" cfg="$2"
  108. echo "############################## HEADLESS [$label] ##############################"
  109. local sid="" seg=0 out="" files=()
  110. : > "$OUT/run-$label.err"
  111. for q in "${TURNS[@]}"; do
  112. seg=$((seg + 1))
  113. out="$OUT/run-$label.jsonl"
  114. [ "$seg" -gt 1 ] && out="$OUT/run-$label.t$seg.jsonl"
  115. local resume=()
  116. [ -n "$sid" ] && resume=(--resume "$sid")
  117. ( cd "$REPO" && PATH="$ARM_PATH" claude -p "$q" \
  118. --output-format stream-json --verbose \
  119. --permission-mode bypassPermissions \
  120. --model "${MODEL:-sonnet}" --effort "${EFFORT:-high}" \
  121. --max-budget-usd 4 \
  122. --strict-mcp-config --mcp-config "$cfg" \
  123. ${resume[@]+"${resume[@]}"} \
  124. </dev/null > "$out" 2>>"$OUT/run-$label.err" )
  125. echo "exit $? -> $out ($(wc -l < "$out" | tr -d ' ') lines) [turn $seg/${#TURNS[@]}]"
  126. files+=("$out")
  127. sid="$(session_id_of "$out")"
  128. if [ -z "$sid" ] && [ "$seg" -lt "${#TURNS[@]}" ]; then
  129. echo " WARN: no session_id in $out — later turns would start a FRESH context; stopping this arm"
  130. break
  131. fi
  132. done
  133. tail -2 "$OUT/run-$label.err" 2>/dev/null
  134. node "$HARNESS/parse-run.mjs" "${files[@]}" 2>&1 || true
  135. echo
  136. }
  137. # CG_ARMS=with|without|both — re-run one arm without redoing the other.
  138. ARMS="${CG_ARMS:-both}"
  139. if [ "$MODE" = headless ] || [ "$MODE" = all ]; then
  140. case "$ARMS" in both|with) headless "headless-with" "$OUT/mcp-codegraph.json";; esac
  141. case "$ARMS" in both|without) headless "headless-without" "$OUT/mcp-empty.json";; esac
  142. fi
  143. if [ "$MODE" = tmux ] || [ "$MODE" = all ]; then
  144. echo "############################## INTERACTIVE [with] ##############################"
  145. CLAUDE_EXTRA_ARGS="--model ${MODEL:-sonnet} --effort ${EFFORT:-high} --strict-mcp-config --mcp-config $OUT/mcp-codegraph.json" \
  146. bash "$HARNESS/itrun.sh" "$REPO" "int-with" "${TURNS[0]}" 2>&1 || echo "[itrun WITH failed]"
  147. echo
  148. echo "############################## INTERACTIVE [without] ##############################"
  149. CLAUDE_EXTRA_ARGS="--model ${MODEL:-sonnet} --effort ${EFFORT:-high} --strict-mcp-config --mcp-config $OUT/mcp-empty.json" \
  150. bash "$HARNESS/itrun.sh" "$REPO" "int-without" "${TURNS[0]}" 2>&1 || echo "[itrun WITHOUT failed]"
  151. echo
  152. fi
  153. echo "############################## RUN-ALL COMPLETE ##############################"