Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a Dockerfile, a headless execution script (run_in_pod.sh), and a metrics exporter (export_run_metrics.py) with corresponding unit tests for running MaxKernel Claude Code on TPU containers. The feedback highlights a missing ANTHROPIC_HEADLESS=1 environment variable in the execution script, a potential process leak during cleanup where running run_code.py subprocesses are not terminated, and a falsy value bug in the metrics exporter where using or for token fallbacks fails when the token count is 0.
| OUTPUT_DIR="${OUTPUT_DIR:-${MK_ROOT}/workspace/_pod_run}" | ||
|
|
||
| DRY_RUN=false |
There was a problem hiding this comment.
The PR description mentions that the script configures a headless environment (ANTHROPIC_HEADLESS=1), but this environment variable is never exported or set in run_in_pod.sh. Without setting ANTHROPIC_HEADLESS=1, Claude Code CLI may attempt to run interactively or open a browser for authentication, causing the headless execution to hang or fail.
| OUTPUT_DIR="${OUTPUT_DIR:-${MK_ROOT}/workspace/_pod_run}" | |
| DRY_RUN=false | |
| OUTPUT_DIR="${OUTPUT_DIR:-${MK_ROOT}/workspace/_pod_run}" | |
| export ANTHROPIC_HEADLESS=1 | |
| DRY_RUN=false |
| cleanup() { | ||
| local exit_code=$? | ||
| if [ -n "$TPU_SERVER_PID" ] && kill -0 "$TPU_SERVER_PID" 2>/dev/null; then | ||
| echo "Stopping background TPU server (PID: $TPU_SERVER_PID)..." | ||
| kill "$TPU_SERVER_PID" 2>/dev/null || true | ||
| wait "$TPU_SERVER_PID" 2>/dev/null || true | ||
| fi | ||
| exit "$exit_code" | ||
| } |
There was a problem hiding this comment.
When run_in_pod.sh is terminated or times out, it stops the background TPU server process. However, the TPU server runs jobs in separate process groups (using preexec_fn=os.setsid in tpu_server.py). If a job is currently running, killing the TPU server process will not terminate the running run_code.py subprocess, leading to leaked orphan processes that keep the TPU locked. Adding a cleanup command to kill any leaked run_code.py processes ensures the TPU is freed up.
| cleanup() { | |
| local exit_code=$? | |
| if [ -n "$TPU_SERVER_PID" ] && kill -0 "$TPU_SERVER_PID" 2>/dev/null; then | |
| echo "Stopping background TPU server (PID: $TPU_SERVER_PID)..." | |
| kill "$TPU_SERVER_PID" 2>/dev/null || true | |
| wait "$TPU_SERVER_PID" 2>/dev/null || true | |
| fi | |
| exit "$exit_code" | |
| } | |
| cleanup() { | |
| local exit_code=$? | |
| if [ -n "$TPU_SERVER_PID" ] && kill -0 "$TPU_SERVER_PID" 2>/dev/null; then | |
| echo "Stopping background TPU server (PID: $TPU_SERVER_PID)..." | |
| kill "$TPU_SERVER_PID" 2>/dev/null || true | |
| wait "$TPU_SERVER_PID" 2>/dev/null || true | |
| fi | |
| pkill -9 -f run_code.py || true | |
| exit "$exit_code" | |
| } |
| if "cache_read_input_tokens" in u or "cache_read_tokens" in u: | ||
| val = u.get("cache_read_input_tokens") or u.get("cache_read_tokens") | ||
| if val is not None: | ||
| cache_read_tokens += int(val) | ||
| found_tokens = True |
There was a problem hiding this comment.
Using the or operator to fall back from cache_read_input_tokens to cache_read_tokens introduces a bug when cache_read_input_tokens is 0. Since 0 is falsy in Python, the expression will incorrectly evaluate to cache_read_tokens (which might be None or a non-zero value), or if both are 0 or None, it will skip setting found_tokens to True. Use a conditional expression checking for None explicitly to avoid this falsy value bug.
| if "cache_read_input_tokens" in u or "cache_read_tokens" in u: | |
| val = u.get("cache_read_input_tokens") or u.get("cache_read_tokens") | |
| if val is not None: | |
| cache_read_tokens += int(val) | |
| found_tokens = True | |
| if "cache_read_input_tokens" in u or "cache_read_tokens" in u: | |
| val = u.get("cache_read_input_tokens") if u.get("cache_read_input_tokens") is not None else u.get("cache_read_tokens") | |
| if val is not None: | |
| cache_read_tokens += int(val) | |
| found_tokens = True |
| if "cache_read_input_tokens" in u or "cache_read_tokens" in u: | ||
| val = u.get("cache_read_input_tokens") or u.get("cache_read_tokens") | ||
| if val is not None: | ||
| cache_read_tokens += int(val) |
There was a problem hiding this comment.
Similar to the previous finding, using or to fall back from cache_read_input_tokens to cache_read_tokens will incorrectly use the fallback value when cache_read_input_tokens is 0. Explicitly check for None instead.
val = u.get("cache_read_input_tokens") if u.get("cache_read_input_tokens") is not None else u.get("cache_read_tokens")
if val is not None:
cache_read_tokens += int(val)
found_tokens = True| if result_usage is not None: | ||
| in_tok = result_usage.get("input_tokens") | ||
| out_tok = result_usage.get("output_tokens") | ||
| cache_tok = result_usage.get("cache_read_input_tokens") or result_usage.get("cache_read_tokens") |
There was a problem hiding this comment.
Using or to fall back from cache_read_input_tokens to cache_read_tokens here also suffers from the falsy 0 bug. If cache_read_input_tokens is 0, it will incorrectly fall back to cache_read_tokens.
| cache_tok = result_usage.get("cache_read_input_tokens") or result_usage.get("cache_read_tokens") | |
| cache_tok = result_usage.get("cache_read_input_tokens") if result_usage.get("cache_read_input_tokens") is not None else result_usage.get("cache_read_tokens") |
cb1f0f6 to
cb10413
Compare
Support overriding the default model name using the MODEL_NAME environment variable in auto_agent and hitl_agent.
…full branch context
…rocesses, and handle zero token values
… venv interpreter
…nerized environments
d083082 to
d18da3e
Compare
Summary
Adds headless in-pod runner support, a multi-runtime container image specification, and a metrics exporter for Claude Code optimization runs on Cloud TPUs.
Key Changes
run_in_pod.sh):server/tpu_server.py) and Claude Code CLI (claude -p)./healthendpoint before launching optimization.ANTHROPIC_HEADLESS=1, non-interactive flags).Dockerfile):@anthropic-ai/claude-code, Python 3.12, JAX with Cloud TPU support, and Anthropic Vertex AI SDK.export_run_metrics.py):state.jsonandclaude_stream.jsonto emit structuredsummary_metrics.jsoncontaining kernel speedup, baseline and best execution latency, candidate iterations, and prompt/candidate token breakdowns.