返回 CodeWhale
app-server-smoke.sh
根目录 / scripts / release / app-server-smoke.sh
1 #!/usr/bin/env bash
2 # app-server runtime API release smoke.
3 #
4 # Two independent checks, both safe to run before every release:
5 #
6 # 1. app-server stdio probe (always): drives `codewhale app-server --stdio`
7 # with JSON-RPC health/capabilities requests and asserts the control
8 # surface answers. Spends no model tokens, makes no network calls, and runs
9 # against a throwaway config so it never reads the maintainer's real keys.
10 #
11 # 2. provider/model matrix (opt-in, --matrix): discovers configured providers
12 # from `codewhale auth list`, maps each to a cheap sentinel model, and
13 # either prints the plan (default, dry-run) or runs a tiny `exec` prompt per
14 # provider (--real). `auth list` reports presence flags only; secrets are
15 # never printed and exec output is passed through a redactor.
16 #
17 # Usage:
18 # scripts/release/app-server-smoke.sh # stdio probe only
19 # scripts/release/app-server-smoke.sh --matrix # + print provider matrix (dry-run)
20 # scripts/release/app-server-smoke.sh --matrix --real # + exec a sentinel per provider
21 # scripts/release/app-server-smoke.sh --matrix --provider deepseek --provider zai
22 # scripts/release/app-server-smoke.sh --bin ./target/release/codewhale
23 #
24 # Binary resolution order: --bin <path>, $CODEWHALE_BIN, ./target/release/codewhale, PATH.
25 # Per-provider cheap-model override: SMOKE_MODEL_<SLUG> with slug upper-cased and
26 # '-' replaced by '_', e.g. SMOKE_MODEL_XIAOMI_MIMO=mimo-7b.
27
28 set -euo pipefail
29
30 PASS=0
31 FAIL=0
32 BIN="${CODEWHALE_BIN:-}"
33 DO_MATRIX=0
34 DRY_RUN=1
35 SENTINEL="${SMOKE_SENTINEL:-Reply with exactly the single word: pong}"
36 EXEC_TIMEOUT="${SMOKE_EXEC_TIMEOUT:-60}"
37 declare -a ONLY_PROVIDERS=()
38
39 # Best-effort CHEAP models per provider slot. These are intentionally overridable
40 # (SMOKE_MODEL_<SLUG>) because there is no committed route-effective model
41 # inventory yet (#3205); unmapped configured providers fail loudly in --real mode
42 # instead of guessing. Keep this list conservative: only add a provider here when
43 # its mapped id is a genuinely cheap/small model. Providers without a verified
44 # cheap default (e.g. arcee, openrouter, xiaomi-mimo, openai-codex) are left
45 # unmapped on purpose and must be given a model per run via SMOKE_MODEL_<SLUG>.
46 default_model_for() {
47 case "$1" in
48 deepseek) echo "deepseek-chat" ;;
49 zai) echo "glm-4-flash" ;;
50 moonshot) echo "moonshot-v1-8k" ;;
51 openai) echo "gpt-4o-mini" ;;
52 *) echo "" ;;
53 esac
54 }
55
56 log() { printf '\033[1;34m>>> %s\033[0m\n' "$*"; }
57 pass() { printf '\033[1;32m \xe2\x9c\x93 %s\033[0m\n' "$*"; PASS=$((PASS + 1)); }
58 fail() { printf '\033[1;31m \xe2\x9c\x97 %s\033[0m\n' "$*"; FAIL=$((FAIL + 1)); }
59 note() { printf ' %s\n' "$*"; }
60
61 usage() { sed -n '2,33p' "$0"; }
62
63 # Mask anything that looks like a credential. Defense in depth: the CLI does not
64 # print secrets, but exec output is untrusted text.
65 redact() {
66 sed -E \
67 -e 's/(sk-[A-Za-z0-9]{2})[A-Za-z0-9_-]+/\1…REDACTED/g' \
68 -e 's/(Bearer +)[A-Za-z0-9._-]+/\1REDACTED/g' \
69 -e 's/(([Aa][Pp][Ii][_-]?[Kk][Ee][Yy]|[Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt])["'"'"' :=]+)[^"'"'"' ,}]+/\1REDACTED/g'
70 }
71
72 parse_args() {
73 while [[ $# -gt 0 ]]; do
74 case "$1" in
75 --matrix) DO_MATRIX=1 ;;
76 --real) DRY_RUN=0 ;;
77 --dry-run) DRY_RUN=1 ;;
78 --provider) shift; ONLY_PROVIDERS+=("${1:?--provider needs a value}") ;;
79 --bin) shift; BIN="${1:?--bin needs a path}" ;;
80 -h|--help) usage; exit 0 ;;
81 *) echo "unknown argument: $1" >&2; usage >&2; exit 2 ;;
82 esac
83 shift
84 done
85 }
86
87 resolve_bin() {
88 if [[ -n "$BIN" ]]; then
89 [[ -x "$BIN" ]] || { echo "codewhale binary not executable: $BIN" >&2; exit 2; }
90 return
91 fi
92 if [[ -x "./target/release/codewhale" ]]; then
93 BIN="./target/release/codewhale"
94 elif command -v codewhale >/dev/null 2>&1; then
95 BIN="$(command -v codewhale)"
96 else
97 echo "could not find a codewhale binary." >&2
98 echo " build one: cargo build -p codewhale-cli --release" >&2
99 echo " or pass: --bin <path> / CODEWHALE_BIN=<path>" >&2
100 exit 2
101 fi
102 }
103
104 # ── Check 1: app-server stdio control surface ────────────────────────────────
105
106 stdio_probe() {
107 log "=== app-server stdio probe (no model tokens) ==="
108 local out
109 # Keep the canonical home short for macOS Unix sockets, and resolve /tmp's
110 # symlink before the credential store's no-follow directory walk.
111 if ! out="$(python3 - "$BIN" "${SMOKE_STDIO_TIMEOUT:-20}" <<'PY'
112 import json
113 import os
114 from pathlib import Path
115 import signal
116 import subprocess
117 import sys
118 import tempfile
119
120 binary = str(Path(sys.argv[1]).resolve())
121 with tempfile.TemporaryDirectory(prefix="cw-", dir="/tmp") as temporary:
122 root = Path(temporary).resolve()
123 state_root = root / "state"
124 state_root.mkdir(mode=0o700)
125 config = root / "config.toml"
126 config.write_text("telemetry = false\n")
127 env = {key: os.environ[key] for key in ("PATH", "USER", "SYSTEMROOT") if key in os.environ}
128 env.update(HOME=str(state_root), USERPROFILE=str(state_root), CODEWHALE_HOME=str(state_root), XDG_CONFIG_HOME=str(root / "config"), XDG_CACHE_HOME=str(root / "cache"), TMPDIR=str(root), CODEWHALE_NO_UPDATE_CHECK="1")
129 requests = json.dumps({"id": 0, "method": "initialize", "params": {"clientInfo": {"name": "codewhale-smoke", "version": "1"}}}) + "\n"
130 requests += json.dumps({"method": "initialized"}) + "\n"
131 requests += "".join(json.dumps({"id": i, "method": method}) + "\n"
132 for i, method in enumerate(("healthz", "capabilities", "app/capabilities",
133 "prompt/capabilities", "thread/capabilities", "shutdown"), 1))
134 process = subprocess.Popen([binary, "app-server", "--stdio", "--config", str(config)],
135 cwd=root, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
136 stderr=subprocess.PIPE, start_new_session=True)
137 try:
138 stdout, _ = process.communicate(requests.encode(), timeout=float(sys.argv[2]))
139 except subprocess.TimeoutExpired:
140 os.killpg(process.pid, signal.SIGKILL)
141 process.communicate()
142 print("app-server stdio did not shut down within the probe deadline", file=sys.stderr)
143 sys.exit(1)
144 if process.returncode:
145 print(f"app-server stdio exited with status {process.returncode}", file=sys.stderr)
146 sys.exit(1)
147 try:
148 replies = [json.loads(line) for line in stdout.splitlines()]
149 if {reply.get("id") for reply in replies if "result" in reply} != set(range(0, 7)):
150 raise ValueError("missing successful response")
151 except (ValueError, AttributeError):
152 print("app-server stdio did not return seven successful JSON-RPC responses", file=sys.stderr)
153 sys.exit(1)
154 sys.stdout.buffer.write(stdout)
155 PY
156 )"; then
157 fail "app-server stdio responses and graceful shutdown"
158 return
159 fi
160
161 if [[ -z "$out" ]]; then
162 fail "app-server --stdio produced no output"
163 return
164 fi
165
166 probe_assert "$out" '"status":"ok"' "healthz reports ok"
167 probe_assert "$out" '"thread/request"' "capabilities advertise thread/* methods"
168 probe_assert "$out" '"prompt/run"' "capabilities advertise prompt/run"
169 probe_assert "$out" '"transport":"stdio+http"' "app/capabilities reports transport"
170 probe_assert "$out" '"prompt/request"' "prompt/capabilities lists prompt/request"
171 probe_assert "$out" '"thread/goal/get"' "thread/capabilities lists goal read methods"
172 }
173
174 probe_assert() {
175 local haystack="$1" needle="$2" desc="$3"
176 if [[ "$haystack" == *"$needle"* ]]; then
177 pass "$desc"
178 else
179 fail "$desc (missing: $needle)"
180 fi
181 }
182
183 # ── Check 2: provider/model matrix ───────────────────────────────────────────
184
185 # Echo configured provider slugs (active != missing) from `codewhale auth list`.
186 configured_providers() {
187 "$BIN" auth list 2>/dev/null \
188 | awk 'NR > 1 && NF >= 2 && $NF != "missing" { print $1 }'
189 }
190
191 model_for() {
192 local slug="$1" var
193 var="SMOKE_MODEL_$(printf '%s' "$slug" | tr '[:lower:]-' '[:upper:]_')"
194 if [[ -n "${!var:-}" ]]; then
195 printf '%s' "${!var}"
196 else
197 default_model_for "$slug"
198 fi
199 }
200
201 want_provider() {
202 [[ ${#ONLY_PROVIDERS[@]} -eq 0 ]] && return 0
203 local p
204 for p in "${ONLY_PROVIDERS[@]}"; do
205 [[ "$p" == "$1" ]] && return 0
206 done
207 return 1
208 }
209
210 run_matrix() {
211 if [[ $DRY_RUN -eq 1 ]]; then
212 log "=== provider/model matrix (dry-run) ==="
213 else
214 log "=== provider/model matrix (real exec) ==="
215 fi
216
217 local -a providers=()
218 local p
219 while IFS= read -r p; do
220 [[ -z "$p" ]] && continue
221 want_provider "$p" && providers+=("$p")
222 done < <(configured_providers)
223
224 if [[ ${#providers[@]} -eq 0 ]]; then
225 note "no configured providers discovered (auth list); nothing to test"
226 return
227 fi
228
229 local slug model
230 for slug in "${providers[@]}"; do
231 model="$(model_for "$slug")"
232 if [[ -z "$model" ]]; then
233 if [[ $DRY_RUN -eq 1 ]]; then
234 note "$slug -> (UNMAPPED; set SMOKE_MODEL_$(printf '%s' "$slug" | tr '[:lower:]-' '[:upper:]_'))"
235 else
236 fail "$slug has no cheap-model mapping (set SMOKE_MODEL_$(printf '%s' "$slug" | tr '[:lower:]-' '[:upper:]_')=<model>)"
237 fi
238 continue
239 fi
240
241 if [[ $DRY_RUN -eq 1 ]]; then
242 note "$slug -> $model [$BIN --provider $slug --model $model exec \"<sentinel>\"]"
243 continue
244 fi
245
246 local out rc=0
247 if command -v timeout >/dev/null 2>&1; then
248 out="$(timeout "$EXEC_TIMEOUT" "$BIN" --provider "$slug" --model "$model" exec "$SENTINEL" 2>&1)" || rc=$?
249 else
250 out="$("$BIN" --provider "$slug" --model "$model" exec "$SENTINEL" 2>&1)" || rc=$?
251 fi
252 if [[ $rc -eq 0 ]]; then
253 pass "$slug/$model exec ok"
254 note "$(printf '%s' "$out" | redact | tail -n 1)"
255 else
256 fail "$slug/$model exec failed (rc=$rc)"
257 note "$(printf '%s' "$out" | redact | tail -n 3)"
258 fi
259 done
260 }
261
262 main() {
263 parse_args "$@"
264 resolve_bin
265 log "Using binary: $BIN"
266
267 stdio_probe
268 if [[ $DO_MATRIX -eq 1 ]]; then
269 run_matrix
270 else
271 note "(provider/model matrix skipped; pass --matrix to enable)"
272 fi
273
274 echo ""
275 log "Results: $PASS passed, $FAIL failed"
276 [[ $FAIL -eq 0 ]]
277 }
278
279 main "$@"
280
280 lines BASH