|
| 1 | +#!/usr/bin/env bash |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# Capture aimock fixtures for the c-interrupts graph by running the standalone |
| 5 | +# langgraph dev server against aimock in --record mode. Drives TWO booking |
| 6 | +# flows in sequence so the recorded fixture covers both confirm and cancel |
| 7 | +# resume paths. |
| 8 | +# |
| 9 | +# WHY THIS IS A SPECIAL-CASE SCRIPT (not using the generic |
| 10 | +# scripts/record-aimock-cap.sh): |
| 11 | +# |
| 12 | +# Most caps' flows are normal LLM-call → tool_call → continuation cycles |
| 13 | +# that complete in a single run; the generic recorder handles those by |
| 14 | +# polling for terminal status (success/error/timeout/interrupted) and then |
| 15 | +# merging the captured fixture files. c-interrupts is different: the graph |
| 16 | +# calls langgraph's interrupt() inside a ToolNode, which pauses the run |
| 17 | +# (status=interrupted) and requires the client to POST a `command.resume` |
| 18 | +# value back to continue. The recorded fixture has to capture BOTH the |
| 19 | +# pre-interrupt LLM call AND the post-resume continuation, which means |
| 20 | +# driving the resume API call from the recorder script. The drive_flow |
| 21 | +# helper below handles that two-phase dance. |
| 22 | +# |
| 23 | +# Run from repo root: |
| 24 | +# OPENAI_API_KEY=sk-... bash cockpit/chat/interrupts/angular/e2e/scripts/record-c-interrupts.sh |
| 25 | +set -euo pipefail |
| 26 | + |
| 27 | +REPO_ROOT="$(cd "$(dirname "$0")/../../../../../.." && pwd)" |
| 28 | +cd "$REPO_ROOT" |
| 29 | + |
| 30 | +if [[ -z "${OPENAI_API_KEY:-}" ]]; then |
| 31 | + for env_path in examples/chat/python/.env cockpit/chat/interrupts/python/.env; do |
| 32 | + if [[ -f "$env_path" ]]; then |
| 33 | + set -a; source "$env_path"; set +a |
| 34 | + break |
| 35 | + fi |
| 36 | + done |
| 37 | +fi |
| 38 | +if [[ -z "${OPENAI_API_KEY:-}" ]]; then |
| 39 | + echo "OPENAI_API_KEY not set (in env or examples/chat/python/.env)" >&2 |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +AIMOCK_PORT=19999 |
| 44 | +LANGGRAPH_PORT=5503 |
| 45 | +FIXTURE_OUT="cockpit/chat/interrupts/angular/e2e/fixtures/c-interrupts.json" |
| 46 | +RECORD_DIR="$(pwd)/cockpit/chat/interrupts/angular/e2e/fixtures/.staging" |
| 47 | +rm -rf "$RECORD_DIR" |
| 48 | +mkdir -p "$RECORD_DIR" |
| 49 | +TMP_DIR="$(mktemp -d)" |
| 50 | +trap 'rm -rf "$TMP_DIR"' EXIT |
| 51 | + |
| 52 | +if [[ -f "examples/chat/python/.env" ]]; then |
| 53 | + cp examples/chat/python/.env cockpit/chat/interrupts/python/.env |
| 54 | +fi |
| 55 | + |
| 56 | +echo "[record] starting aimock --record on :$AIMOCK_PORT" |
| 57 | +mkdir -p "$(dirname "$FIXTURE_OUT")" |
| 58 | +npx llmock \ |
| 59 | + --port "$AIMOCK_PORT" \ |
| 60 | + --record \ |
| 61 | + --provider-openai https://api.openai.com \ |
| 62 | + --fixtures "$RECORD_DIR" \ |
| 63 | + --chunk-size 4096 \ |
| 64 | + > "$TMP_DIR/aimock.log" 2>&1 & |
| 65 | +AIMOCK_PID=$! |
| 66 | + |
| 67 | +cleanup() { |
| 68 | + if [[ -n "${LG_PID:-}" ]]; then |
| 69 | + pkill -P "$LG_PID" 2>/dev/null || true |
| 70 | + kill "$LG_PID" 2>/dev/null || true |
| 71 | + fi |
| 72 | + kill "$AIMOCK_PID" 2>/dev/null || true |
| 73 | + wait 2>/dev/null || true |
| 74 | + rm -rf "$TMP_DIR" |
| 75 | +} |
| 76 | +trap cleanup EXIT |
| 77 | + |
| 78 | +for _ in {1..30}; do |
| 79 | + if curl -sf "http://127.0.0.1:$AIMOCK_PORT/health" > /dev/null 2>&1; then break; fi |
| 80 | + if curl -sf "http://127.0.0.1:$AIMOCK_PORT/" > /dev/null 2>&1; then break; fi |
| 81 | + sleep 1 |
| 82 | +done |
| 83 | +echo "[record] aimock ready" |
| 84 | + |
| 85 | +echo "[record] starting langgraph dev on :$LANGGRAPH_PORT (OPENAI_BASE_URL=http://127.0.0.1:$AIMOCK_PORT/v1)" |
| 86 | +if command -v setsid >/dev/null 2>&1; then |
| 87 | + RUN_PREFIX="setsid" |
| 88 | +else |
| 89 | + RUN_PREFIX="" |
| 90 | +fi |
| 91 | +( |
| 92 | + cd cockpit/chat/interrupts/python |
| 93 | + # aimock --record forwards requests to real OpenAI but doesn't substitute |
| 94 | + # the API key, so we must pass the real key through to langgraph. The |
| 95 | + # recorded fixture matches on request body content (userMessage, tool |
| 96 | + # results, etc.) — not on the Authorization header — so no auth leak. |
| 97 | + OPENAI_BASE_URL="http://127.0.0.1:$AIMOCK_PORT/v1" OPENAI_API_KEY="$OPENAI_API_KEY" \ |
| 98 | + exec $RUN_PREFIX uv run langgraph dev --port "$LANGGRAPH_PORT" --no-browser |
| 99 | +) > "$TMP_DIR/langgraph.log" 2>&1 & |
| 100 | +LG_PID=$! |
| 101 | + |
| 102 | +for _ in {1..60}; do |
| 103 | + if curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/ok" > /dev/null; then break; fi |
| 104 | + sleep 1 |
| 105 | +done |
| 106 | +if ! curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/ok" > /dev/null; then |
| 107 | + echo "[record] langgraph failed to start; tail of log:" >&2 |
| 108 | + tail -30 "$TMP_DIR/langgraph.log" >&2 |
| 109 | + exit 2 |
| 110 | +fi |
| 111 | +echo "[record] langgraph ready" |
| 112 | + |
| 113 | +# Helper: drive one full booking flow (prompt → interrupt → resume → final). |
| 114 | +drive_flow() { |
| 115 | + local prompt="$1" |
| 116 | + local resume_value="$2" |
| 117 | + local label="$3" |
| 118 | + |
| 119 | + echo "[record][$label] thread + run with prompt: $prompt" |
| 120 | + local thread |
| 121 | + thread=$(curl -sf -X POST "http://127.0.0.1:$LANGGRAPH_PORT/threads" \ |
| 122 | + -H 'content-type: application/json' -d '{}' \ |
| 123 | + | python3 -c 'import sys,json; print(json.load(sys.stdin)["thread_id"])') |
| 124 | + local run |
| 125 | + run=$(curl -sf -X POST "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/runs" \ |
| 126 | + -H 'content-type: application/json' \ |
| 127 | + -d "{\"assistant_id\": \"c-interrupts\", \"input\": {\"messages\": [{\"role\": \"user\", \"content\": \"$prompt\"}]}}" \ |
| 128 | + | python3 -c 'import sys,json; print(json.load(sys.stdin)["run_id"])') |
| 129 | + echo "[record][$label] thread=$thread run=$run; polling for interrupt" |
| 130 | + |
| 131 | + # LangGraph quirk: when interrupt() fires inside a ToolNode, runs.get() |
| 132 | + # reports status=success. The authoritative interrupt signal is the |
| 133 | + # presence of an unresolved interrupt in thread state. Gate on that. |
| 134 | + local status="" |
| 135 | + local has_interrupt="False" |
| 136 | + for _ in {1..120}; do |
| 137 | + status=$(curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/runs/$run" \ |
| 138 | + | python3 -c 'import sys,json; print(json.load(sys.stdin).get("status",""))') |
| 139 | + case "$status" in |
| 140 | + interrupted|success|error|timeout) break ;; |
| 141 | + esac |
| 142 | + sleep 2 |
| 143 | + done |
| 144 | + if [[ "$status" == "error" || "$status" == "timeout" ]]; then |
| 145 | + echo "[record][$label] run terminal status=$status (no normal stop)" >&2 |
| 146 | + tail -40 "$TMP_DIR/langgraph.log" >&2 |
| 147 | + exit 3 |
| 148 | + fi |
| 149 | + has_interrupt=$(curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/state" \ |
| 150 | + | python3 -c 'import sys,json; d=json.load(sys.stdin); print(any(it.get("value") is not None for t in d.get("tasks",[]) for it in t.get("interrupts",[])))') |
| 151 | + if [[ "$has_interrupt" != "True" ]]; then |
| 152 | + echo "[record][$label] expected pending interrupt in thread state, found none (run status=$status)" >&2 |
| 153 | + tail -40 "$TMP_DIR/langgraph.log" >&2 |
| 154 | + exit 3 |
| 155 | + fi |
| 156 | + echo "[record][$label] interrupt fired; posting resume=$resume_value" |
| 157 | + |
| 158 | + local resume_run |
| 159 | + resume_run=$(curl -sf -X POST "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/runs" \ |
| 160 | + -H 'content-type: application/json' \ |
| 161 | + -d "{\"assistant_id\": \"c-interrupts\", \"command\": {\"resume\": \"$resume_value\"}}" \ |
| 162 | + | python3 -c 'import sys,json; print(json.load(sys.stdin)["run_id"])') |
| 163 | + |
| 164 | + # Resume run completion signal: terminal status reached AND no pending |
| 165 | + # interrupt remains in thread state. |
| 166 | + status="" |
| 167 | + for _ in {1..120}; do |
| 168 | + status=$(curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/runs/$resume_run" \ |
| 169 | + | python3 -c 'import sys,json; print(json.load(sys.stdin).get("status",""))') |
| 170 | + case "$status" in |
| 171 | + success|error|timeout|interrupted) break ;; |
| 172 | + esac |
| 173 | + sleep 2 |
| 174 | + done |
| 175 | + if [[ "$status" == "error" || "$status" == "timeout" ]]; then |
| 176 | + echo "[record][$label] resume run did not reach a normal stop (status=$status)" >&2 |
| 177 | + tail -40 "$TMP_DIR/langgraph.log" >&2 |
| 178 | + exit 4 |
| 179 | + fi |
| 180 | + local leftover |
| 181 | + leftover=$(curl -sf "http://127.0.0.1:$LANGGRAPH_PORT/threads/$thread/state" \ |
| 182 | + | python3 -c 'import sys,json; d=json.load(sys.stdin); print(any(it.get("value") is not None for t in d.get("tasks",[]) for it in t.get("interrupts",[])))') |
| 183 | + if [[ "$leftover" == "True" ]]; then |
| 184 | + echo "[record][$label] resume left a pending interrupt in thread state" >&2 |
| 185 | + tail -40 "$TMP_DIR/langgraph.log" >&2 |
| 186 | + exit 4 |
| 187 | + fi |
| 188 | + echo "[record][$label] resume run succeeded" |
| 189 | +} |
| 190 | + |
| 191 | +drive_flow "Book me on UA123." "confirm" "confirm" |
| 192 | +drive_flow "Book me on AA404." "cancel" "cancel" |
| 193 | + |
| 194 | +# Give aimock a moment to flush per-request fixture files. |
| 195 | +sleep 2 |
| 196 | + |
| 197 | +RECORDED_DIR="$RECORD_DIR/recorded" |
| 198 | +if [[ ! -d "$RECORDED_DIR" ]]; then |
| 199 | + echo "[record] no recorded fixtures dir at $RECORDED_DIR" >&2 |
| 200 | + tail -40 "$TMP_DIR/aimock.log" >&2 |
| 201 | + exit 5 |
| 202 | +fi |
| 203 | +RECORDED_FILES=$(find "$RECORDED_DIR" -name "*.json" | wc -l | tr -d ' ') |
| 204 | +echo "[record] $RECORDED_FILES recorded fixture files in $RECORDED_DIR" |
| 205 | + |
| 206 | +python3 - <<PYEOF |
| 207 | +import json, os, glob |
| 208 | +recorded = sorted(glob.glob(os.path.join(r"$RECORDED_DIR", "*.json"))) |
| 209 | +merged = {"fixtures": []} |
| 210 | +for f in recorded: |
| 211 | + with open(f) as fh: |
| 212 | + data = json.load(fh) |
| 213 | + merged["fixtures"].extend(data.get("fixtures", [])) |
| 214 | +with open(r"$FIXTURE_OUT", "w") as fh: |
| 215 | + json.dump(merged, fh, indent=2) |
| 216 | +print(f"[record] merged {len(merged['fixtures'])} entries into $FIXTURE_OUT") |
| 217 | +PYEOF |
| 218 | + |
| 219 | +rm -rf "$RECORD_DIR" |
| 220 | + |
| 221 | +if [[ ! -s "$FIXTURE_OUT" ]]; then |
| 222 | + echo "[record] fixture file is missing or empty: $FIXTURE_OUT" >&2 |
| 223 | + exit 6 |
| 224 | +fi |
| 225 | +echo "[record] fixture written: $FIXTURE_OUT ($(wc -c < "$FIXTURE_OUT") bytes)" |
| 226 | +ENTRY_COUNT=$(python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(len(d.get("fixtures",[])))' "$FIXTURE_OUT") |
| 227 | +echo "[record] $ENTRY_COUNT fixture entries" |
0 commit comments