dashboard: drain the run queue so a multi-step chain completes
A `continue` assessment re-dispatches the next step onto the queue rather than running it inline, so a multi-step request only finishes when a front end drains `Scheduler.run_queue()` -- as the CLI and gateway already do. The dashboard ran only the first step and left the child stranded in the queue, so a compound request looked finished when it was not. - `DashboardHandler._complete` runs the returned reply through `_drain_chain` and returns the last reply (intermediate steps are hidden at `result` verbosity); `/api/submit` and `/api/answer` use it. - `/api/answer` now also returns `request_id` (`SchedulerReply.run_id`) so a front end can cross-link the resumed run. - `test_submit_completes_a_multi_step_chain` drives a deterministic two-step chain and asserts both steps ran, the reply is the final step, nothing is left in the queue, and the runs are linked.
This commit is contained in:
1 parent
9757ae893e
commit
c010cdb44e
2 files changed
+89
-4
No files matched your search
@@ -255,8 +255,11 @@ class DashboardHandler(BaseHTTPRequestHandler):
|
||||
with self.lock:
|
||||
try:
|
||||
body = self._read_json()
|
||||
reply = self.scheduler.submit_request(
|
||||
str(body.get("text", "")), source=str(body.get("source", "dashboard"))
|
||||
reply = self._complete(
|
||||
self.scheduler.submit_request(
|
||||
str(body.get("text", "")),
|
||||
source=str(body.get("source", "dashboard")),
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
self._send_error(500, str(exc))
|
||||
@@ -277,11 +280,11 @@ class DashboardHandler(BaseHTTPRequestHandler):
|
||||
with self.lock:
|
||||
try:
|
||||
body = self._read_json()
|
||||
reply = self.scheduler.answer(str(body.get("text", "")))
|
||||
reply = self._complete(self.scheduler.answer(str(body.get("text", ""))))
|
||||
except Exception as exc:
|
||||
self._send_error(500, str(exc))
|
||||
return
|
||||
self._send(200, {"status": reply.status, "detail": reply.text})
|
||||
self._send(200, {"status": reply.status, "detail": reply.text, "request_id": reply.run_id})
|
||||
return
|
||||
if path == "/api/restart":
|
||||
with self.lock:
|
||||
@@ -372,6 +375,28 @@ class DashboardHandler(BaseHTTPRequestHandler):
|
||||
return
|
||||
self._send(200, {"status": status, "detail": detail})
|
||||
|
||||
def _complete(self, first):
|
||||
"""Finish a request's chain and return the reply that answers it.
|
||||
|
||||
A `continue` assessment re-dispatches the next step onto the queue
|
||||
rather than running it inline, so a multi-step request only completes
|
||||
when a front end drains `Scheduler.run_queue()` — as the CLI and
|
||||
gateway do. Without this the dashboard would run just the first step
|
||||
and strand the rest. The last reply is the final answer (intermediate
|
||||
steps are hidden at `result` verbosity).
|
||||
"""
|
||||
return [first, *self._drain_chain()][-1]
|
||||
|
||||
def _drain_chain(self) -> list:
|
||||
"""Run queued chain steps, bounded like `GatewayService.drain`."""
|
||||
replies = []
|
||||
for _ in range(100):
|
||||
batch = self.scheduler.run_queue()
|
||||
if not batch:
|
||||
break
|
||||
replies.extend(batch)
|
||||
return replies
|
||||
|
||||
def log_message(self, format, *args):
|
||||
pass
|
||||
|
||||
|
||||
@@ -307,6 +307,66 @@ def test_submit_returns_request_id_matching_trace(tmp_path):
|
||||
server.close()
|
||||
|
||||
|
||||
class _ChainEngine:
|
||||
"""Deterministic two-step chain: step 1 -> continue, step 2 -> done."""
|
||||
|
||||
def __init__(self):
|
||||
self.assessments = iter(["continue", "done"])
|
||||
|
||||
def call(self, request):
|
||||
ids = [o.id for o in request.options]
|
||||
q = request.question
|
||||
if "additional work to be done" in q:
|
||||
selected = next(self.assessments, "done")
|
||||
elif "logical next step" in q:
|
||||
selected = "demo"
|
||||
elif "what does this skill do" in q:
|
||||
selected = "same"
|
||||
elif "Is this input a request" in q:
|
||||
selected = "action"
|
||||
else:
|
||||
selected = ids[0]
|
||||
if selected not in ids:
|
||||
selected = ids[0]
|
||||
return DecisionResult(
|
||||
request=request,
|
||||
option_ids=ids,
|
||||
probabilities=[1.0 if i == selected else 0.0 for i in ids],
|
||||
)
|
||||
|
||||
|
||||
def test_submit_completes_a_multi_step_chain(tmp_path):
|
||||
"""A `continue` step is queued, not run inline, so a multi-step request only
|
||||
finishes when a front end drains `run_queue()`. The dashboard must do that,
|
||||
as the CLI and gateway do, or the child is stranded in the queue forever."""
|
||||
scheduler = build_scheduler(tmp_path, engine=_ChainEngine())
|
||||
counter = {"n": 0}
|
||||
|
||||
def act(ctx, request):
|
||||
counter["n"] += 1
|
||||
return ActionResult(action_log=f"step {counter['n']} done", new_state="x")
|
||||
|
||||
scheduler.tree["demo"] = [
|
||||
Skill(name="step", category="demo", description="Do a demo step.", act=act)
|
||||
]
|
||||
server = Server(scheduler)
|
||||
try:
|
||||
status, payload = server.post("/api/submit", {"text": "do the thing"})
|
||||
assert status == 200
|
||||
# The reply is the final step, not the hidden intermediate.
|
||||
assert payload["status"] == "ran"
|
||||
assert "step 2 done" in payload["detail"]
|
||||
assert counter["n"] == 2
|
||||
# Nothing is left stranded in the queue.
|
||||
assert list(scheduler.queue.items()) == []
|
||||
# The chain ran both steps and linked them.
|
||||
status, trace = server.get("/api/trace")
|
||||
kinds = [e["kind"] for run in trace["runs"] for e in run["events"]]
|
||||
assert "continued" in kinds
|
||||
finally:
|
||||
server.close()
|
||||
|
||||
|
||||
def test_relabel_roundtrip(tmp_path):
|
||||
scheduler = build_scheduler(tmp_path)
|
||||
seed_decision(scheduler.log)
|
||||
|
||||
Reference in new issue
Block a user