Buckets:

download
raw
17.2 kB
"""One copy of the queue flow every real-stack e2e uses to run a plan.
The bridge has no direct-execute route any more: ``POST /runs`` and
``POST /runs/{id}/launch`` both answer 410 ``use_the_queue``. Running a plan is
now four steps -- stage it in the shared draft, enqueue the exact revision that
was staged with the launch token, start the queue where it is stopped, poll the
run record -- and this module is the single place they are spelled out, so a
change to the flow lands in one file rather than in every deploy proof that
happens to run a plan.
``tests/e2e/test_bluesky_queue_e2e.py`` deliberately does NOT import this: that
module is the acceptance instrument for the queue surface itself, and a proof
of the flow must not be written in terms of a helper that assumes it. Every
OTHER e2e -- the ones whose subject is a substrate, a plan, or a physics
round trip, and for which the queue is merely transport -- should use this.
WHY EACH STEP IS SHAPED THE WAY IT IS:
* The draft is CLEARED before it is staged. A ``PATCH /draft`` that changes
nothing is a documented no-op: no revision bump. Re-running the same plan
with the same arguments (which is exactly what ``pytest.mark.flaky`` does on
a rerun) would therefore hand back the revision the first attempt already
consumed, and the enqueue would be refused ``draft_revision_already_launched``
-- a confusing failure with nothing to do with the code under test. Deleting
first makes every stage a genuine plan-change, so the revision always moves.
* The pending queue is DRAINED before staging. ``POST /queue/start`` starts the
whole queue, not one item, and the queue lives in a Redis named volume that
``osprey down`` deliberately keeps -- so a previous run's leftovers would go
back on the hardware under this run's start. Removal goes through the
bridge's own ``DELETE /queue/items/{uid}``, never into Redis.
* The enqueue CARRIES THE TOKEN, and the start is conditional. A deployment
is either armed (``bluesky.queue_autostart`` on -- what the shipped
``control-assistant`` preset does) or stopped. On an armed queue the add is
the arming write: the item runs the moment the manager reaches it, an
unarmed add is refused ``launch_token_required``, and a ``POST /queue/start``
afterwards is refused ``manager_not_idle`` while the plan is in motion. On a
stopped queue the add just sits there until an armed start. ``start_draining``
reads which of the two the manager reports and does only what that posture
needs, so one flow serves both kinds of deployment.
``wait_for_worker_environment`` is the one piece here that belongs to a deploy
FIXTURE rather than to the flow: it is the readiness gate an enqueue depends on
and that ``/health`` does not give you. See its docstring.
"""
from __future__ import annotations
import json
import time
import urllib.error
import urllib.request
from typing import Any
LAUNCH_TOKEN_HEADER = "X-Launch-Token"
#: Terminal ``GET /runs/{id}`` statuses. ``stopped`` and ``error`` are terminal
#: too -- a caller asserting "completed" wants to see them reported promptly,
#: not to wait out its whole deadline first.
TERMINAL_STATUSES = ("completed", "error", "stopped")
#: How long a deploy fixture waits for the RE worker environment to come up.
#: Generous on purpose: opening it connects every substrate device over Channel
#: Access, which takes tens of seconds on a cold stack (and longer under QEMU
#: emulation on Apple Silicon).
WORKER_ENV_TIMEOUT_SEC = 300.0
def request(
base_url: str,
path: str,
method: str,
body: dict[str, Any] | None = None,
*,
token: str | None = None,
timeout: float = 20.0,
) -> tuple[int, Any]:
"""One HTTP call, returning ``(status, parsed_body)`` for 2xx AND 4xx/5xx.
Refusals carry the ``{"detail": {"code", "detail", ...}}`` body callers
assert on, so an ``HTTPError`` is a normal result here, never an exception
to propagate.
"""
data = json.dumps(body).encode("utf-8") if body is not None else None
headers: dict[str, str] = {}
if data is not None:
headers["Content-Type"] = "application/json"
elif method != "GET":
data = b""
if token:
headers[LAUNCH_TOKEN_HEADER] = token
req = urllib.request.Request( # noqa: S310 - localhost only
f"{base_url}{path}", data=data, method=method, headers=headers
)
try:
with urllib.request.urlopen(req, timeout=timeout) as resp: # noqa: S310
raw = resp.read()
try:
return resp.status, json.loads(raw.decode("utf-8"))
except ValueError:
return resp.status, raw.decode("utf-8", errors="replace")
except urllib.error.HTTPError as exc:
raw = exc.read()
try:
return exc.code, json.loads(raw.decode("utf-8"))
except ValueError:
return exc.code, raw.decode("utf-8", errors="replace")
def wait_for_worker_environment(
base_url: str, *, timeout: float = WORKER_ENV_TIMEOUT_SEC, poll: float = 2.0
) -> dict[str, Any]:
"""Block until the manager reports an OPEN RE worker environment.
The gate every deploy fixture in this suite owes its own enqueue. HTTP
readiness on ``/health`` does NOT imply it: the bridge opens the worker
environment in a background task that is deliberately excluded from
readiness (device connect takes tens of seconds), so a stack can answer 200
with an empty worker namespace. ``POST /queue/items`` validates against
``plans_allowed``, which the manager downloads from the worker only when
that environment opens -- so enqueueing too early is refused 409 "not in
the list of allowed plans", a message that reads like a permissions problem
and is nothing of the sort (the shipped ``user_group_permissions.yaml``
allows ``[":.*"]``; the list was empty because the namespace was).
``manager_state`` is NOT the signal to wait on. It reads ``idle`` both
before the environment has ever been opened and after it is up, so gating
on it would be very nearly vacuous. ``worker_environment_exists`` is the
one field that separates the two.
``worker_environment_exists`` alone is necessary but NOT sufficient: it
flips true the moment the worker process is up, while ``plans_allowed``
is filled in on a separate, later path -- the manager notices the open
environment on a poll, downloads the worker's plan and device lists, and
only then recomputes what is allowed. An enqueue landing in that window
is refused with exactly the 409 this gate exists to prevent. So a second
phase polls ``GET /devices`` until the ``devices`` array of its paginated
envelope is non-empty: the manager assigns its allowed-plans and
allowed-devices lists in one synchronous body (plans first), so a
non-empty device page proves ``plans_allowed`` has already landed. Every
substrate ships at least one device, so "non-empty" is the right bar.
Returns the manager status document that satisfied the wait. Raises
``AssertionError`` naming the last thing seen -- a failure here says the
worker environment (or its plan list) never came up, which points at the
queueserver rather than at the plan the caller was about to run.
"""
deadline = time.monotonic() + timeout
last: Any = "(no answer yet)"
status_doc: dict[str, Any] | None = None
while time.monotonic() < deadline:
http_status, body = request(base_url, "/queue", "GET")
if http_status == 200 and isinstance(body, dict):
last = body.get("status") or {}
if isinstance(last, dict) and last.get("worker_environment_exists"):
status_doc = dict(last)
break
else:
last = body
time.sleep(poll)
if status_doc is None:
raise AssertionError(
f"the RE worker environment never opened within {timeout:.0f}s -- the queue "
f"cannot accept plans until it does (last manager status: {last!r})"
)
# Phase 2, on the same deadline: the environment is open, but the manager
# may not have downloaded the worker's plan list yet (see docstring).
last = "(no answer yet)"
while time.monotonic() < deadline:
http_status, body = request(base_url, "/devices", "GET")
if http_status == 200 and isinstance(body, dict) and body.get("devices"):
return status_doc
last = body
time.sleep(poll)
raise AssertionError(
f"the RE worker environment opened but the manager never published its "
f"device list within {timeout:.0f}s -- plans_allowed is filled on the same "
f"path, so an enqueue now would be refused 409 "
f"(last GET /devices envelope: {last!r})"
)
def drain_pending_queue(base_url: str) -> None:
"""Remove every PENDING item, so this run's start drains only its own work.
History is deliberately left alone: it is append-only and every caller
looks up its own run ids in it.
"""
status, queue = request(base_url, "/queue", "GET")
assert status == 200, f"GET /queue failed: {status} {queue}"
for item in queue.get("items") or []:
uid = item.get("item_uid")
if not isinstance(uid, str):
continue
removed_status, removed = request(base_url, f"/queue/items/{uid}", "DELETE")
assert removed_status == 200, (
f"could not drain queue item {uid}: {removed_status} {removed}"
)
def stage_draft(base_url: str, plan_name: str, plan_args: dict[str, Any], *, client_id: str) -> int:
"""Clear the draft, stage ``plan_name``/``plan_args`` into it, return the revision."""
request(base_url, f"/draft?client_id={client_id}", "DELETE")
status, body = request(
base_url,
"/draft",
"PATCH",
{"plan_name": plan_name, "plan_args_patch": plan_args, "client_id": client_id},
)
assert status == 200, f"PATCH /draft failed: {status} {body}"
assert body["plan_name"] == plan_name, f"the draft did not take the plan name: {body}"
revision = body["revision"]
assert isinstance(revision, int), f"no integer revision on the PATCH response: {body}"
return revision
def enqueue(base_url: str, revision: int, *, token: str | None = None) -> str:
"""Enqueue the plan staged at ``revision``; return its OSPREY run id.
``token`` is what an armed deployment requires of every add (the add runs
the plan there), and what a stopped one ignores -- so a caller that means
to run the plan always sends it.
"""
status, body = request(
base_url, "/queue/items", "POST", {"draft_revision": revision}, token=token
)
assert status == 200, f"POST /queue/items failed: {status} {body}"
run_id = body.get("run_id")
assert run_id, f"no run_id in the enqueue response: {body}"
assert body.get("revision") == revision, f"the enqueue pinned a different revision: {body}"
return str(run_id)
def start_queue(base_url: str, token: str) -> None:
"""Arm and start a STOPPED queue -- the action that puts waiting plans on hardware.
Only for a caller that knows the queue is stopped; a start on an armed
queue with a plan in motion is refused ``manager_not_idle``. A plan that
was just enqueued wants `start_draining`, which reads the posture first.
"""
status, body = request(base_url, "/queue/start", "POST", token=token, timeout=120.0)
assert status == 200, f"armed POST /queue/start failed: {status} {body}"
assert body.get("started") is True, f"start did not report started: {body}"
def queue_is_armed(base_url: str) -> bool:
"""Whether the manager reports autostart on -- an added plan runs by itself."""
status, queue = request(base_url, "/queue", "GET")
assert status == 200, f"GET /queue failed: {status} {queue}"
manager = queue.get("status") if isinstance(queue, dict) else None
return bool(isinstance(manager, dict) and manager.get("queue_autostart_enabled"))
def start_draining(base_url: str, token: str, run_id: str) -> None:
"""Make sure the queue is draining the plan just enqueued as ``run_id``.
Posture-aware, read off the manager rather than assumed from config:
* armed (autostart on) -- the add already put the plan in motion, so there
is nothing to start and a start would be refused ``manager_not_idle``;
* stopped -- the armed start is what drains the queue.
The bridge arms an autostart deployment right after the worker environment
opens, a moment `wait_for_worker_environment` cannot see, so a start that
loses that race is tolerated when it was refused because OUR plan is
already moving: the queue is draining the run either way.
"""
if queue_is_armed(base_url):
return
status, body = request(base_url, "/queue/start", "POST", token=token, timeout=120.0)
if status == 409 and _refusal_code(body) == "manager_not_idle":
record_status, record = run_record(base_url, run_id)
assert record_status == 200 and record.get("status") in ("running", *TERMINAL_STATUSES), (
f"POST /queue/start was refused manager_not_idle, but run {run_id} is not the "
f"plan in motion: {body} (run record: {record})"
)
return
assert status == 200, f"armed POST /queue/start failed: {status} {body}"
assert body.get("started") is True, f"start did not report started: {body}"
def _refusal_code(body: Any) -> str | None:
detail = body.get("detail") if isinstance(body, dict) else None
return detail.get("code") if isinstance(detail, dict) else None
def stage_and_enqueue(
base_url: str,
plan_name: str,
plan_args: dict[str, Any],
*,
client_id: str,
token: str | None = None,
) -> str:
"""Drain leftovers, stage the plan, enqueue it. Returns the OSPREY run id."""
drain_pending_queue(base_url)
revision = stage_draft(base_url, plan_name, plan_args, client_id=client_id)
return enqueue(base_url, revision, token=token)
def run_record(base_url: str, run_id: str) -> tuple[int, Any]:
return request(base_url, f"/runs/{run_id}", "GET")
def wait_for_terminal_status(
base_url: str, run_id: str, *, timeout: float, poll: float = 0.5
) -> dict[str, Any]:
"""Poll ``GET /runs/{id}`` until it reports a terminal status, or the deadline.
Returns the LAST record seen either way -- a caller's own
``status == "completed"`` assertion is what turns a timeout into its own
(much more informative) failure message.
"""
deadline = time.monotonic() + timeout
last: dict[str, Any] = {}
while time.monotonic() < deadline:
status, body = run_record(base_url, run_id)
if status == 200 and isinstance(body, dict):
last = body
if body.get("status") in TERMINAL_STATUSES:
return last
time.sleep(poll)
return last
def assert_start_request_route_is_gone(base_url: str) -> None:
"""Assert the deployed bridge exposes NO ``POST /queue/start-request``.
``POST /queue/start`` is the only route that starts a queue. The route this
probes used to be the second half of a two-action arming flow: an agent
without the launch token filed a start REQUEST here, and an operator then
confirmed it from the queue panel. Both halves are gone -- the agent either
holds the token and starts the queue outright, or is refused.
Worth probing over HTTP rather than by grepping the source, because this is
the only assertion in the suite that can see the DEPLOYED bridge. Every
other test of this path mocks the MCP server's HTTP client, so an MCP
server posting to a route the bridge no longer serves stays green
everywhere else -- which is exactly the two-party break the deletion could
have left behind, in either direction.
A 404 is the pass. Anything else means a route answers there: 405 would say
the path exists under another method, and a 2xx/4xx from the app itself
would say the mechanism is still deployed.
"""
status, body = request(base_url, "/queue/start-request", "POST", {})
assert status == 404, (
f"POST /queue/start-request answered {status} ({body!r}) -- the bridge "
"still serves a start-request route. Starting a queue is one action "
"through POST /queue/start; a second route that files a request for an "
"operator to confirm is the flow that was removed"
)
def run_plan(
base_url: str,
plan_name: str,
plan_args: dict[str, Any],
*,
token: str,
client_id: str,
timeout: float,
poll: float = 0.5,
) -> tuple[str, dict[str, Any]]:
"""The whole flow: stage -> armed enqueue -> start where stopped -> poll.
Returns ``(run_id, last_run_record)``. The record is returned rather than
asserted on so each proof can say what a non-completion means in ITS terms.
"""
run_id = stage_and_enqueue(base_url, plan_name, plan_args, client_id=client_id, token=token)
start_draining(base_url, token, run_id)
return run_id, wait_for_terminal_status(base_url, run_id, timeout=timeout, poll=poll)

Xet Storage Details

Size:
17.2 kB
·
Xet hash:
c3d9ee4cbd381ae756e0f736c6bcaf81663543ec6307691be060610392e19a97

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.