Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions miles/rollout/sglang_rollout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import copy
import logging
import os
import uuid
from argparse import Namespace
from collections.abc import Callable
Expand Down Expand Up @@ -340,6 +341,36 @@ async def generate_and_rm_group(
return group


async def flush_agent_server(args: Namespace) -> None:
"""Cancel in-flight Harbor agent-server trials for this run.

Aborting SGLang only stops the in-flight generations; the Harbor agent loops
keep running and keep issuing fresh completion requests to SGLang until they
hit their own max_seq_len / timeout. Flushing the agent server cancels the
in-flight ``/run`` tasks (agent-agnostic) and releases their containers.
"""
agent_server_url = getattr(args, "agent_server_url", None) or os.environ.get("AGENT_SERVER_URL")
instance_id = getattr(args, "session_server_instance_id", None)
if not agent_server_url or not instance_id:
return

headers = None
admin_secret = os.environ.get("HARBOR_ADMIN_SECRET")
if admin_secret:
headers = {"Authorization": f"Bearer {admin_secret}"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Environment variables and command-line arguments (especially URLs and secrets/tokens) are highly prone to containing accidental leading or trailing whitespaces or newlines (e.g., when loaded from .env files or Kubernetes secrets).

Why this matters:

  • URLs: Leading/trailing spaces can cause httpx to raise malformed URL or connection errors.
  • Secrets: Trailing newlines in HARBOR_ADMIN_SECRET will result in an invalid Authorization header, causing the Harbor agent server to reject the request with a 400 or 401 error.

Stripping these values before use ensures robust and reliable API calls.

Suggested change
agent_server_url = getattr(args, "agent_server_url", None) or os.environ.get("AGENT_SERVER_URL")
instance_id = getattr(args, "session_server_instance_id", None)
if not agent_server_url or not instance_id:
return
headers = None
admin_secret = os.environ.get("HARBOR_ADMIN_SECRET")
if admin_secret:
headers = {"Authorization": f"Bearer {admin_secret}"}
agent_server_url = getattr(args, "agent_server_url", None) or os.environ.get("AGENT_SERVER_URL")
instance_id = getattr(args, "session_server_instance_id", None)
if not agent_server_url or not instance_id:
return
agent_server_url = agent_server_url.strip()
headers = None
admin_secret = os.environ.get("HARBOR_ADMIN_SECRET")
if admin_secret:
headers = {"Authorization": f"Bearer {admin_secret.strip()}"}


try:
result = await post(
f"{agent_server_url.rstrip('/')}/flush",
{"session_server_instance_id": instance_id},
max_retries=3,
headers=headers,
)
logger.info(f"Flushed agent server {agent_server_url}: {result}")
except Exception as e:
logger.warning(f"Failed to flush agent server {agent_server_url}: {e}")


async def abort(args: Namespace, rollout_id: int) -> list[list[Sample]]:
aborted_samples = []

Expand All @@ -361,6 +392,10 @@ async def abort(args: Namespace, rollout_id: int) -> list[list[Sample]]:
if isinstance(result, Exception):
logger.warning(f"Failed to abort worker at {url}: {result}")

# Cancel in-flight Harbor trials so they stop hitting SGLang and release their
# containers instead of running on until their own max_seq_len / timeout.
await flush_agent_server(args)

# make sure all the pending tasks are finished
count = 0
while state.pendings:
Expand Down
9 changes: 9 additions & 0 deletions miles/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,15 @@ def add_session_arguments(parser):
default=None,
help="Port of the standalone session server. Auto-allocated if not set.",
)
parser.add_argument(
"--agent-server-url",
type=str,
default=os.environ.get("AGENT_SERVER_URL"),
help="Base URL of the Harbor agent server driving agentic rollouts. "
"When set, oversampling abort also flushes the server's in-flight "
"trials so they stop hitting SGLang. Defaults to the AGENT_SERVER_URL "
"env var.",
)
parser.add_argument(
"--tito-model",
type=str,
Expand Down
Loading