ab-new-vs-baseline.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. # A/B a codegraph retrieval/steering change: the NEW build (current HEAD) vs a
  3. # BASELINE build (a git ref) — BOTH with codegraph attached — on the same
  4. # implementation task. ISOLATES the change (unlike run-all.sh's
  5. # with-vs-without). The agent works on a throwaway copy of the target, so your
  6. # repos are never touched.
  7. #
  8. # Each run reports the tool mix AND the three feedback metrics; compare-arms.mjs
  9. # then puts both arms side by side:
  10. # residual context occupancy (CG-7) window still held by the arm's retrieval
  11. # explore sufficiency (CG-8) was the response ENOUGH — the agent's next act
  12. # allocation efficiency (CG-9) share of returned bytes the answer cited
  13. # This is the harness those metrics were built for: both arms are codegraph-on,
  14. # so every one of them is measuring the retrieval change rather than adoption.
  15. # docs/benchmarks/agent-eval-feedback-metrics.md is the entry point; the three
  16. # per-metric docs it links carry the caveats — in particular that allocation
  17. # efficiency is RELATIVE (attribution is by citation), which is exactly why
  18. # same-question new-vs-baseline is the comparison it is valid for.
  19. #
  20. # Both arms also run with the codegraph CLI blocked (no-cli-shim.sh). Here that
  21. # is not a with/without leak but an ATTRIBUTION one: an explore issued through
  22. # Bash is charged to Bash in the occupancy table and never reaches the
  23. # sufficiency or allocation parse at all, so a run that shells out silently
  24. # drops calls from all three metrics.
  25. #
  26. # Reliable attach (works even when this is itself run nested inside a Claude
  27. # session): each arm PRE-WARMS a persistent codegraph daemon for its target so
  28. # claude connects to an already-bound, index-loaded daemon instantly — before
  29. # the agent's first turn — and SKIPS codegraph's startup re-exec via
  30. # CODEGRAPH_WASM_RELAUNCHED=1. Without this, on a multi-step task the agent
  31. # dives into Read/grep before codegraph finishes its ~2-3s startup (worse under
  32. # the CPU contention of a nested run) and runs with NO codegraph.
  33. #
  34. # Gotcha: claude's `system/init` snapshot can read status:"pending" / 0 tools
  35. # even when the server then connects fine — judge by ACTUAL codegraph usage in
  36. # parse-run.mjs's "by type", not the init line.
  37. #
  38. # Usage: ab-new-vs-baseline.sh <indexed-repo> "<task>" [baseline-ref]
  39. # <indexed-repo> a repo with a .codegraph index (copied per arm)
  40. # "<task>" an implementation task, e.g. "Add X to Y and wire it through"
  41. # [baseline-ref] git ref for the BEFORE build (default: HEAD~1)
  42. # Env:
  43. # AGENT_EVAL_OUT output dir (default: /tmp/ab-new-vs-baseline)
  44. # RUNS runs per arm (default 1). Run-to-run variance is large —
  45. # use >=2 and report the range, never a single run. Both arms
  46. # build/index ONCE and then run RUNS times, so raising this is
  47. # far cheaper than re-invoking the script.
  48. # MODEL / EFFORT default sonnet / high. Never raise without a reason: sonnet
  49. # is the deliberate floor model (see CLAUDE.md).
  50. #
  51. # Both arms run with CODEGRAPH_NO_PROMPT_HOOK=1: the machine's ambient
  52. # UserPromptSubmit front-load hook resolves to whichever build is currently in
  53. # dist/, so leaving it on injects context through a second, uncontrolled channel
  54. # and confounds the tool-call counts this script exists to compare.
  55. set -uo pipefail
  56. TARGET="${1:?usage: ab-new-vs-baseline.sh <indexed-repo> \"<task>\" [baseline-ref]}"
  57. TASK="${2:?task required}"
  58. BASE_REF="${3:-HEAD~1}"
  59. HARNESS="$(cd "$(dirname "$0")" && pwd)"
  60. ENGINE="$(cd "$HARNESS/../.." && pwd)"
  61. BIN="$ENGINE/dist/bin/codegraph.js"
  62. OUT="${AGENT_EVAL_OUT:-/tmp/ab-new-vs-baseline}"
  63. PARSE="$HARNESS/parse-run.mjs"
  64. command -v claude >/dev/null || { echo "claude CLI not on PATH"; exit 1; }
  65. [ -d "$TARGET/.codegraph" ] || { echo "target not indexed: run 'codegraph init $TARGET' first"; exit 1; }
  66. if ! git -C "$ENGINE" diff --quiet || ! git -C "$ENGINE" diff --cached --quiet; then
  67. echo "engine repo has uncommitted changes — commit or stash first (this script checks files out)"; exit 1
  68. fi
  69. CHANGED=$(git -C "$ENGINE" diff --name-only "$BASE_REF" HEAD -- src 2>/dev/null)
  70. [ -n "$CHANGED" ] || { echo "no src/ changes between $BASE_REF and HEAD — nothing to A/B"; exit 1; }
  71. # On exit: kill any eval daemons + restore the engine to HEAD.
  72. cleanup() {
  73. pkill -9 -f "serve --mcp --path $OUT/" 2>/dev/null
  74. git -C "$ENGINE" checkout HEAD -- $CHANGED 2>/dev/null
  75. ( cd "$ENGINE" && npm run build >/dev/null 2>&1 )
  76. }
  77. # INT/TERM too: killing the script mid-baseline-arm otherwise leaves the engine
  78. # checked out at the baseline ref, which silently poisons every later build.
  79. trap cleanup EXIT INT TERM
  80. mkdir -p "$OUT"
  81. # Sanitized PATH + the absolute-path block, shared with run-all.sh. Sets
  82. # $ARM_PATH and $ARM_SETTINGS; aborts if either layer fails its own probe.
  83. . "$HARNESS/no-cli-shim.sh"
  84. cg_no_cli_setup "$OUT" || exit 1
  85. echo "###### engine=$ENGINE baseline=$BASE_REF"
  86. echo "###### changed: $(echo "$CHANGED" | tr '\n' ' ')"
  87. echo "###### target=$TARGET"
  88. echo "###### task=$TASK"
  89. echo
  90. # Two pristine copies so each arm starts clean (the agent edits its own copy).
  91. rm -rf "$OUT/t-new" "$OUT/t-base"
  92. rsync -a --exclude node_modules --exclude .git --exclude dist --exclude .codegraph "$TARGET/" "$OUT/t-new/"
  93. cp -R "$OUT/t-new" "$OUT/t-base"
  94. prewarm() { # target — spawn a persistent daemon (current $BIN) and wait for its socket
  95. pkill -9 -f "serve --mcp --path $1" 2>/dev/null
  96. CODEGRAPH_DAEMON_IDLE_TIMEOUT_MS=1800000 node "$BIN" serve --mcp --path "$1" </dev/null >/dev/null 2>&1 &
  97. node -e 'const fs=require("fs");let n=0;const t=setInterval(()=>{if(fs.existsSync(process.argv[1]+"/.codegraph/daemon.sock")){clearInterval(t);process.exit(0)}if(n++>150){clearInterval(t);process.exit(1)}},100)' "$1" \
  98. && echo " daemon warm: $1" || echo " WARN: daemon never bound for $1 (arm may run without codegraph)"
  99. }
  100. run_arm() { # label, target-copy — runs the task $RUNS times against one build
  101. local label="$1" tgt="$2" c="$OUT/mcp-$1.json"
  102. # Connect to the pre-warmed daemon; skip the startup re-exec for a fast attach.
  103. # CODEGRAPH_EXPLORE_DEBUG points explore's per-file allocation diagnostic at a
  104. # sidecar (no-op on builds predating it; never perturbs the response).
  105. printf '{"mcpServers":{"codegraph":{"command":"env","args":["CODEGRAPH_WASM_RELAUNCHED=1","CODEGRAPH_EXPLORE_DEBUG=%s","node","%s","serve","--mcp","--path","%s"]}}}' \
  106. "$OUT/explore-$label.jsonl" "$BIN" "$tgt" > "$c"
  107. rm -f "$OUT/explore-$label.jsonl"
  108. echo "############## ARM [$label] ##############"
  109. for i in $(seq 1 "${RUNS:-1}"); do
  110. # Re-warm per run: the previous run's daemon is killed below, and a cold
  111. # attach is exactly the failure this pre-warm exists to prevent.
  112. prewarm "$tgt"
  113. ( cd "$tgt" && PATH="$ARM_PATH" CODEGRAPH_NO_PROMPT_HOOK=1 claude -p "$TASK" \
  114. --output-format stream-json --verbose --permission-mode bypassPermissions \
  115. --model "${MODEL:-sonnet}" --effort "${EFFORT:-high}" --max-budget-usd 4 --strict-mcp-config --mcp-config "$c" \
  116. --settings "$ARM_SETTINGS" \
  117. </dev/null > "$OUT/run-$label-$i.jsonl" 2>"$OUT/run-$label-$i.err" )
  118. echo "-- run $i --"
  119. # --brief: tool counts, result, and the three metric blocks, minus the
  120. # numbered call transcript (RUNS>=2 is otherwise mostly call listings; the
  121. # full sequence is one `parse-run.mjs $OUT/run-$label-$i.jsonl` away).
  122. node "$PARSE" --brief "$OUT/run-$label-$i.jsonl" 2>&1 || echo " (parse failed — see $OUT/run-$label-$i.jsonl)"
  123. pkill -9 -f "serve --mcp --path $tgt" 2>/dev/null
  124. done
  125. echo
  126. }
  127. echo "== NEW build (HEAD) =="
  128. ( cd "$ENGINE" && npm run build >/dev/null 2>&1 ) && echo " built"
  129. node "$BIN" init "$OUT/t-new" >/dev/null 2>&1 && echo " indexed t-new"
  130. run_arm new "$OUT/t-new"
  131. echo "== BASELINE build ($BASE_REF) =="
  132. # Per-file: a file ADDED since baseline has no pathspec on the ref — and a
  133. # single multi-file checkout with one bad pathspec checks out NOTHING, which
  134. # silently ran the NEW build in the baseline arm. Absent-on-baseline → remove.
  135. for f in $CHANGED; do
  136. git -C "$ENGINE" checkout "$BASE_REF" -- "$f" 2>/dev/null || rm -f "$ENGINE/$f"
  137. done
  138. ( cd "$ENGINE" && npm run build >/dev/null 2>&1 ) && echo " built"
  139. node "$BIN" init "$OUT/t-base" >/dev/null 2>&1 && echo " indexed t-base"
  140. run_arm baseline "$OUT/t-base"
  141. # Both arms, all three metrics, one table. The per-run blocks above say WHY a
  142. # number moved (which query fell short, which file nothing cited); this says
  143. # whether it moved at all, with the range across RUNS — never read the median
  144. # of one run.
  145. node "$HARNESS/compare-arms.mjs" "$OUT" new baseline 2>&1 || true
  146. echo "###### DONE. Read the ARM COMPARISON above first, then the per-run blocks"
  147. echo "###### for the queries and files behind any number that moved."
  148. echo "###### Full logs in: $OUT"