Skip to content

[DRAFT] ✨ Add Brev VM lifecycle management for model serving#9

Draft
taagarwa-rh wants to merge 9 commits into
mainfrom
feat/brev-integration
Draft

[DRAFT] ✨ Add Brev VM lifecycle management for model serving#9
taagarwa-rh wants to merge 9 commits into
mainfrom
feat/brev-integration

Conversation

@taagarwa-rh

@taagarwa-rh taagarwa-rh commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Automatically manage Brev GPU instances from the job queue service so benchmark jobs can use models served on Brev without manual setup. Pass server_url="brev" to trigger the lifecycle; any other value skips Brev entirely for backwards compatibility.

Lifecycle: create instance on first job → start model container → run benchmark → swap or stop model between jobs → delete instance when queue drains. Same-model jobs are grouped in the queue and reuse the running container to avoid redundant start/stop cycles.

Fault tolerance: startup orphan cleanup, retry-3 on delete, SIGTERM/SIGINT/atexit handlers as a safety net for expensive VMs.

NOTE: Brev tokens are short lived and cannot be reliably used for creating and deleting VMs. Until long-lived tokens are introduced, this must stay in draft

Automatically manage Brev GPU instances from the job queue service so
benchmark jobs can use models served on Brev without manual setup.
Pass server_url="brev" to trigger the lifecycle; any other value skips
Brev entirely for backwards compatibility.

Lifecycle: create instance on first job → start model container →
run benchmark → swap or stop model between jobs → delete instance
when queue drains. Same-model jobs are grouped in the queue and
reuse the running container to avoid redundant start/stop cycles.

Fault tolerance: startup orphan cleanup, retry-3 on delete,
SIGTERM/SIGINT/atexit handlers as a safety net for expensive VMs.

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: d87878f7-8abf-49f6-baac-ed7405f6fda8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change installs the Brev CLI, exposes a Brev model endpoint, adds Brev instance and model lifecycle management, and integrates model-aware routing, queue ordering, startup recovery, shutdown cleanup, and post-job cleanup into the job queue service.

Changes

Brev model execution

Layer / File(s) Summary
Brev lifecycle implementation
src/coding_agent_bench/brev.py
Defines model configurations and manages Brev login, instance creation, port forwarding, health checks, model switching, cleanup, deletion retries, and orphan recovery.
Brev runtime and network wiring
Containerfile, .env.example, deploy/job-queue-service.yml
Installs the Brev CLI, documents the Brev token, injects it into the deployment, and exposes container port 9000 through brev-model-service.
Queued job model routing and cleanup
src/coding_agent_bench/api.py
Stores and sorts queued jobs by model, passes model names to execution, rewrites Brev server URLs, switches models, and performs cleanup across success, failure, cancellation, and exception paths.
Server startup and shutdown integration
src/coding_agent_bench/api.py
Tracks the active Brev instance, cleans orphaned resources and jobs at startup, registers emergency cleanup handlers, and destroys active instances during shutdown.
Brev integration documentation
README.md
Documents Brev setup, secrets, lifecycle behavior, prerequisites, and an example Brev job request.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant JobWorker
  participant BrevInstance
  participant BrevCLI
  participant ModelEndpoint
  JobWorker->>BrevInstance: ensure running and select model
  BrevInstance->>BrevCLI: create or execute model
  BrevInstance->>ModelEndpoint: poll /health
  JobWorker->>ModelEndpoint: run command with managed server URL
  JobWorker->>BrevInstance: post-job cleanup
  BrevInstance->>BrevCLI: stop model or delete instance
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.83% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the Brev VM lifecycle management added for model serving.
Description check ✅ Passed The description matches the Brev VM lifecycle automation, queue behavior, and cleanup handlers in the changeset.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/brev-integration

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🧹 Nitpick comments (1)
src/coding_agent_bench/brev.py (1)

24-29: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

MODEL_CONFIGS is empty, so every Brev job fails.

With no entries, start_model (line 172-178) raises ValueError("No Brev model config...") for any model_name, and stop_model becomes a no-op. Any command routed with --server-url brev will therefore fail. Confirm this placeholder is intended to be populated before this path is exercised.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/coding_agent_bench/brev.py` around lines 24 - 29, Populate MODEL_CONFIGS
with at least one valid Brev model entry, including its Docker command and
container name, before enabling the Brev server path; ensure the configured
model names match those accepted by start_model and stop_model.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Containerfile`:
- Around line 24-25: The Brev installation command in the Containerfile runs
after curl has been removed and therefore cannot execute. Move the Brev install
RUN command before the cleanup that removes curl, or defer removing curl until
after the installation step.

In `@src/coding_agent_bench/api.py`:
- Around line 305-318: Brev initialization failures in _run_job can escape
before its existing try block, terminating _worker without updating job state.
Move the uses_brev setup, including ensure_running, stop_model, start_model, and
_substitute_server_url, inside the guarded error-handling path so failures mark
the job FAILED and invoke _brev_post_job_cleanup; alternatively add equivalent
exception handling around _run_job in _worker.
- Around line 186-189: Remove the signal.signal registrations for SIGTERM and
SIGINT from the lifespan setup, preserving uvicorn’s handlers so it can initiate
graceful shutdown and execute the async _brev_instance.destroy() path; retain
_brev_emergency_cleanup via lifespan/atexit as appropriate.

In `@src/coding_agent_bench/brev.py`:
- Around line 219-246: After all retries fail in destroy, do not mark the VM as
stopped or return normally; preserve _running (and the instance reference) and
re-raise last_err so _brev_post_job_cleanup surfaces the failure instead of
discarding a potentially running VM. Keep cleanup state resets only on
successful deletion.

---

Nitpick comments:
In `@src/coding_agent_bench/brev.py`:
- Around line 24-29: Populate MODEL_CONFIGS with at least one valid Brev model
entry, including its Docker command and container name, before enabling the Brev
server path; ensure the configured model names match those accepted by
start_model and stop_model.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: ed7b603e-af3a-4e5d-a3e7-afc4af3f7c98

📥 Commits

Reviewing files that changed from the base of the PR and between 0bd357c and b7383ff.

📒 Files selected for processing (4)
  • Containerfile
  • deploy/job-queue-service.yml
  • src/coding_agent_bench/api.py
  • src/coding_agent_bench/brev.py

Comment thread Containerfile
Comment thread src/coding_agent_bench/api.py Outdated
Comment thread src/coding_agent_bench/api.py Outdated
Comment thread src/coding_agent_bench/brev.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
Containerfile (2)

24-24: 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift

Pin the Brev installer. This image runs install-latest.sh from main, so upstream changes can alter the build without review. Pin a release or commit and validate its checksum/signature before executing it.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Containerfile` at line 24, Pin the installer referenced by the
Containerfile’s Brev installation RUN command to a specific release or commit
instead of downloading install-latest.sh from main, then verify the downloaded
script’s checksum or signature before executing it.

24-24: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Install brev onto the final image PATH

install-latest.sh puts the binary in ${HOME}/.local/bin, and Containerfile never adds that directory to PATH. Since src/coding_agent_bench/brev.py runs brev directly via subprocess, runtime calls can fail unless you install it into /usr/local/bin as root or export PATH=$HOME/.local/bin:$PATH in the image.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Containerfile` at line 24, Ensure the brev binary installed by the RUN
command is available to runtime subprocess calls in the final image: either
install it into /usr/local/bin as root or configure the image PATH to include
${HOME}/.local/bin, so src/coding_agent_bench/brev.py can invoke brev directly.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@README.md`:
- Around line 186-194: The documented optional BREV_TOKEN conflicts with the
required SecretKeyRef in the deployment. Update the BREV_TOKEN reference in the
job-queue deployment manifest to set optional: true, preserving the README
instructions that omit the key.

---

Outside diff comments:
In `@Containerfile`:
- Line 24: Pin the installer referenced by the Containerfile’s Brev installation
RUN command to a specific release or commit instead of downloading
install-latest.sh from main, then verify the downloaded script’s checksum or
signature before executing it.
- Line 24: Ensure the brev binary installed by the RUN command is available to
runtime subprocess calls in the final image: either install it into
/usr/local/bin as root or configure the image PATH to include
${HOME}/.local/bin, so src/coding_agent_bench/brev.py can invoke brev directly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: ad6ee39f-917b-495f-943c-88a01f810285

📥 Commits

Reviewing files that changed from the base of the PR and between b7383ff and 83f3b00.

📒 Files selected for processing (6)
  • .env.example
  • Containerfile
  • README.md
  • deploy/job-queue-service.yml
  • src/coding_agent_bench/api.py
  • src/coding_agent_bench/brev.py
✅ Files skipped from review due to trivial changes (1)
  • .env.example
🚧 Files skipped from review as they are similar to previous changes (3)
  • deploy/job-queue-service.yml
  • src/coding_agent_bench/api.py
  • src/coding_agent_bench/brev.py

Comment thread README.md
Comment on lines +186 to +194
4. Create a secret file named `job-queue-secret` with an `API_KEY` (and optional `BREV_TOKEN`) and apply it:
```yaml
apiVersion: v1
kind: Secret
metadata:
name: job-queue-secret
stringData:
API_KEY: <your-api-key>
# BREV_TOKEN: <your-brev-token>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Align the optional-token documentation with the deployment contract.

deploy/job-queue-service.yml references job-queue-secret/BREV_TOKEN without marking the SecretKeyRef optional. A secret created exactly as documented without that key can leave the pod in CreateContainerConfigError, even when Brev is not used. Mark the reference optional in the deployment, or make BREV_TOKEN required in these instructions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@README.md` around lines 186 - 194, The documented optional BREV_TOKEN
conflicts with the required SecretKeyRef in the deployment. Update the
BREV_TOKEN reference in the job-queue deployment manifest to set optional: true,
preserving the README instructions that omit the key.

@taagarwa-rh taagarwa-rh changed the title ✨ Add Brev VM lifecycle management for model serving [DRAFT] ✨ Add Brev VM lifecycle management for model serving Jul 11, 2026
@taagarwa-rh taagarwa-rh marked this pull request as draft July 13, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant