-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo_coding_agent.sh
More file actions
executable file
·300 lines (263 loc) · 12.1 KB
/
demo_coding_agent.sh
File metadata and controls
executable file
·300 lines (263 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
# =============================================================================
# EvalMonkey — Coding Agent Demo
# =============================================================================
# Runs baseline benchmarks + chaos injection + improvement eval generation
# against the built-in coding_agent sample app (apps/coding_agent/app.py).
#
# Covers:
# Benchmarks : human-eval (Coding) · mbpp (Coding)
# Chaos tests : code_context_strip · code_conflicting_constraints
# client_prompt_injection · code_wrong_language
#
# Prerequisites:
# 1. Copy .env.example → .env and set EVAL_MODEL + provider key.
# 2. pip install -e . (installs evalmonkey CLI into your venv)
#
# Usage (from evalmonkey/):
# chmod +x demo_coding_agent.sh
# ./demo_coding_agent.sh
# =============================================================================
set -euo pipefail
# ── ANSI colour helpers ────────────────────────────────────────────────────
BOLD='\033[1m'
CYAN='\033[0;36m'
BCYAN='\033[1;36m'
GREEN='\033[0;32m'
BGREEN='\033[1;32m'
YELLOW='\033[1;33m'
MAGENTA='\033[0;35m'
BMAGENTA='\033[1;35m'
RED='\033[0;31m'
BRED='\033[1;31m'
DIM='\033[2m'
NC='\033[0m'
# ── Pretty printers ────────────────────────────────────────────────────────
divider() {
echo -e "${DIM}$(printf '─%.0s' {1..62})${NC}"
}
section() { # section "emoji" "Title"
echo ""
echo -e "${BCYAN}╔══════════════════════════════════════════════════════════╗${NC}"
printf "${BCYAN}║${NC} ${BOLD}$1 $2${NC}$(printf ' %.0s' $(seq 1 $((56 - ${#1} - ${#2}))))${BCYAN}║${NC}\n"
echo -e "${BCYAN}╚══════════════════════════════════════════════════════════╝${NC}"
echo ""
}
step() { echo -e " ${BMAGENTA}▶${NC} ${BOLD}$*${NC}"; }
success() { echo -e " ${BGREEN}✔${NC} $*"; }
warn() { echo -e " ${YELLOW}⚠${NC} $*"; }
fail() { echo -e " ${BRED}✘${NC} $*"; exit 1; }
info() { echo -e " ${DIM}$*${NC}"; }
# ── Load .env ──────────────────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ -f "$SCRIPT_DIR/.env" ]; then
set -a; source "$SCRIPT_DIR/.env"; set +a
success "Loaded .env"
else
warn ".env not found — using existing shell environment"
fi
# ── Activate virtualenv ────────────────────────────────────────────────────
for VENV_DIR in "$SCRIPT_DIR/venv" "$SCRIPT_DIR/.venv"; do
if [ -f "${VENV_DIR}/bin/activate" ]; then
# shellcheck source=/dev/null
source "${VENV_DIR}/bin/activate"
success "Activated virtualenv: ${VENV_DIR}"
break
fi
done
# ── Sanity checks ──────────────────────────────────────────────────────────
command -v evalmonkey &>/dev/null || fail "evalmonkey not found. Run: pip install -e . (inside your venv)"
[ -n "${EVAL_MODEL:-}" ] || fail "EVAL_MODEL is not set in .env"
# ── Config ─────────────────────────────────────────────────────────────────
AGENT_PORT=8003
AGENT_URL="http://127.0.0.1:${AGENT_PORT}/solve"
BENCHMARKS=("human-eval" "mbpp")
CHAOS_PROFILES=("code_context_strip" "code_conflicting_constraints" "client_prompt_injection" "code_wrong_language")
LIMIT=2
TS=$(date +%Y%m%d_%H%M%S)
OUTPUT_BASE="output/coding_demo_${TS}"
# ── Intro splash ───────────────────────────────────────────────────────────
clear
echo ""
echo -e "${BMAGENTA}"
cat << 'EOF'
███████╗██╗ ██╗ █████╗ ██╗
██╔════╝██║ ██║██╔══██╗██║
█████╗ ██║ ██║███████║██║
██╔══╝ ╚██╗ ██╔╝██╔══██║██║
███████╗ ╚████╔╝ ██║ ██║███████╗
╚══════╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝
EvalMonkey — Agent Benchmarking & Chaos Framework
EOF
echo -e "${NC}"
echo -e "${BOLD} Coding Agent Demo${NC}"
divider
echo ""
echo -e " ${CYAN}Agent ${NC}: apps/coding_agent/app.py → ${AGENT_URL}"
echo -e " ${CYAN}Model ${NC}: ${EVAL_MODEL}"
echo -e " ${CYAN}Benchmarks ${NC}: ${BENCHMARKS[*]}"
echo -e " ${CYAN}Chaos ${NC}: ${CHAOS_PROFILES[*]}"
echo -e " ${CYAN}Samples ${NC}: ${LIMIT} per run"
echo -e " ${CYAN}Output ${NC}: ${OUTPUT_BASE}/"
echo ""
divider
echo ""
# ── Start coding agent ─────────────────────────────────────────────────────
section "🚀" "Starting Coding Agent"
if lsof -ti :"${AGENT_PORT}" &>/dev/null; then
step "Clearing port ${AGENT_PORT}..."
kill "$(lsof -ti :"${AGENT_PORT}")" 2>/dev/null || true
sleep 1
fi
step "Launching apps/coding_agent/app.py on port ${AGENT_PORT}..."
# Use the venv python so evalmonkey imports resolve correctly
PYTHON_BIN="$(command -v python || command -v python3 || echo python3)"
"${PYTHON_BIN}" apps/coding_agent/app.py >"${SCRIPT_DIR}/output/.coding_agent_${TS}.log" 2>&1 &
AGENT_PID=$!
echo ""
info "PID: ${AGENT_PID} — waiting for startup..."
sleep 4
# Readiness probe
if curl -sf --max-time 3 -X POST "${AGENT_URL}" \
-H "Content-Type: application/json" \
-d '{"question":"ping"}' -o /dev/null 2>/dev/null; then
success "Coding agent is live at ${AGENT_URL}"
else
success "Coding agent started (port active)"
fi
# ── Cleanup trap ───────────────────────────────────────────────────────────
cleanup() {
echo ""
divider
warn "Shutting down coding agent (PID ${AGENT_PID})..."
kill "${AGENT_PID}" 2>/dev/null || true
echo ""
success "Demo complete!"
echo -e " ${DIM}Eval assets saved to: ${OUTPUT_BASE}/${NC}"
echo ""
}
trap cleanup EXIT
# ── Helper: one benchmark or chaos run ────────────────────────────────────
run_benchmark() {
local scenario=$1
echo ""
step "Benchmark: ${BCYAN}${scenario}${NC} (${LIMIT} samples)"
divider
FORCE_COLOR=1 evalmonkey run-benchmark \
--scenario "$scenario" \
--target-url "$AGENT_URL" \
--limit "$LIMIT" \
--request-key question \
--response-path data 2>&1 | sed -E "s/Found the latest cached dataset configuration( '[^']+')? at [^ ]+/Found the latest cached dataset configuration\1/g"
divider
}
run_chaos() {
local scenario=$1
local profile=$2
echo ""
step "Chaos: ${BRED}${profile}${NC} on ${CYAN}${scenario}${NC} (${LIMIT} samples)"
divider
FORCE_COLOR=1 evalmonkey run-chaos \
--scenario "$scenario" \
--target-url "$AGENT_URL" \
--chaos-profile "$profile" \
--limit "$LIMIT" \
--request-key question \
--response-path data 2>&1 | sed -E "s/Found the latest cached dataset configuration( '[^']+')? at [^ ]+/Found the latest cached dataset configuration\1/g"
divider
}
# ── Phase 1: Baseline Benchmarks ──────────────────────────────────────────
section "📊" "Phase 1: Baseline Benchmarks"
echo -e " Running ${#BENCHMARKS[@]} coding benchmarks to establish capability baseline..."
echo ""
for bench in "${BENCHMARKS[@]}"; do
run_benchmark "$bench" || warn "Benchmark '${bench}' had errors — continuing"
sleep 1
done
echo ""
success "Baseline benchmarks complete"
# ── Phase 2: Chaos Injection ───────────────────────────────────────────────
section "🔥" "Phase 2: Chaos Injection Tests"
echo -e " Injecting ${#CHAOS_PROFILES[@]} chaos profiles to stress-test the coding agent..."
echo ""
# Alternate between both benchmarks for variety
PRIMARY="${BENCHMARKS[0]}" # human-eval
SECONDARY="${BENCHMARKS[1]}" # mbpp
run_chaos "$PRIMARY" "${CHAOS_PROFILES[0]}" || warn "Chaos '${CHAOS_PROFILES[0]}' had errors — continuing"
sleep 1
run_chaos "$SECONDARY" "${CHAOS_PROFILES[1]}" || warn "Chaos '${CHAOS_PROFILES[1]}' had errors — continuing"
sleep 1
run_chaos "$PRIMARY" "${CHAOS_PROFILES[2]}" || warn "Chaos '${CHAOS_PROFILES[2]}' had errors — continuing"
sleep 1
run_chaos "$SECONDARY" "${CHAOS_PROFILES[3]}" || warn "Chaos '${CHAOS_PROFILES[3]}' had errors — continuing"
sleep 1
echo ""
success "Chaos injection complete"
# ── Phase 3: Merge traces & generate improvement evals ────────────────────
section "🛠" "Phase 3: Generating Improvement Eval Assets"
echo -e " Collecting all failing traces and generating targeted evals..."
echo ""
mkdir -p "${OUTPUT_BASE}"
MERGED_TRACES="${OUTPUT_BASE}/traces.json"
echo "[]" > "${MERGED_TRACES}"
TRACE_COUNT=$(python3 - <<PYEOF
import json, glob
merged = []
for f in sorted(glob.glob("output/*/traces.json")):
if "coding_demo_${TS}" in f:
continue # skip our own (empty) output dir
try:
data = json.loads(open(f).read())
merged.extend(data)
except Exception:
pass
with open("${MERGED_TRACES}", "w") as out:
json.dump(merged, out, indent=2)
print(len(merged))
PYEOF
)
step "Merged ${TRACE_COUNT} failing trace(s) from this run"
echo ""
if [ "${TRACE_COUNT}" -gt "0" ]; then
evalmonkey generate-evals \
--traces-file "${MERGED_TRACES}" \
--output-dir "${OUTPUT_BASE}"
echo ""
success "Improvement eval assets saved to ${OUTPUT_BASE}/"
echo ""
echo -e " ${DIM}Files:${NC}"
ls -1 "${OUTPUT_BASE}/" | while read -r f; do
echo -e " ${DIM}▸ ${OUTPUT_BASE}/${f}${NC}"
done
else
echo ""
success "No failures detected — coding agent is solid! 🎉"
fi
# ── Phase 4: Fix instructions ──────────────────────────────────────────────
if [ "${TRACE_COUNT}" -gt "0" ]; then
section "💡" "Phase 4: Feed Failures to a Coding Agent"
echo -e " Copy the improvement brief and paste it into ${BOLD}Claude Code${NC} or ${BOLD}Cursor${NC}"
echo -e " to automatically fix the coding agent based on its failures."
echo ""
echo -e " ${BGREEN}cat ${OUTPUT_BASE}/improvement_prompt.md | pbcopy${NC}"
echo -e " ${DIM}# Then Cmd+V into Claude Code or Cursor${NC}"
echo ""
divider
echo ""
echo -e " Or read it directly:"
echo -e " ${CYAN}cat ${OUTPUT_BASE}/improvement_prompt.md${NC}"
echo ""
fi
# ── Phase 5: History & reliability trend ──────────────────────────────────
section "📈" "Phase 5: Production Reliability Trend"
echo -e " Showing score history for all benchmarks run today..."
echo ""
for bench in "${BENCHMARKS[@]}"; do
evalmonkey history --scenario "$bench" 2>/dev/null || true
echo ""
done
divider
echo ""
echo -e " ${BGREEN}Re-run after fixing your agent:${NC}"
echo -e " ${CYAN}evalmonkey run-benchmark --scenario human-eval --target-url ${AGENT_URL}${NC}"
echo ""