mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-08-17 02:09:15 +00:00
The outer ring for the domain added in #4597: SQL repositories, the run launcher, the thread lookup, and the run-completion listener implementing the ports it declared, plus the HTTP router and the poller that drive them. All of it is instantiated in one composition root, so no route or lifespan hook builds an adapter of its own. With the ports filled, the pre-hexagonal implementation is deleted rather than left alongside: `app/scheduler/service.py` and its router mixed policy, persistence, and HTTP into one class, which is why its rules were only reachable through a live database. Keeping both would leave two implementations of the same rules writing to the same table. Three of the domain's contracts needed real work on this side rather than a straight port of the pre-#4597 adapters: - The launcher now distinguishes certain failure from doubt. Only a 4xx is certain enough to raise LaunchFailedError, which releases the task's single active slot; a 5xx, an arbitrary exception, or a reply whose identity will not decode all raise LaunchIndeterminateError and keep the slot held. Guessing "failed" after the launch request was sent is what re-opens #4452's duplicate execution. - The task repository implements the optimistic token. `save` is a conditional UPDATE on `version` rather than read-check-write, because the latter lets two savers observe the same version and both commit; every other committed write increments it. This needs a column, so it ships with migration 0011 -- the only schema change in the slice, and the reason the alembic head pins move. - The router builds commands with plain `None` for "not supplied", and maps ConcurrentUpdateError onto a retryable 409. The concurrency invariants are pinned by contract suites that run each port against both the in-memory double and real sqlite -- including a new TestOptimisticConcurrency covering what invalidates an earlier read -- plus the dispatch-race tests against a real database. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
109 lines
5.2 KiB
Python
109 lines
5.2 KiB
Python
"""Secondary adapter (anti-corruption layer) -- starting a run through Gateway.
|
|
|
|
Implements ``RunLauncher`` from ``deerflow.domain.schedule.ports``. This context
|
|
owns no part of the run lifecycle: it asks the Gateway to start one and
|
|
translates whatever comes back into the two outcomes the domain distinguishes.
|
|
|
|
That translation is the reason this file exists. The Gateway signals a busy
|
|
thread two different ways -- ``ConflictError`` from the run manager, or an
|
|
``HTTPException(409)`` from the route-level path -- and the legacy scheduler
|
|
service therefore imported ``fastapi`` to tell them apart. Both are the same
|
|
domain fact, and saying so here is what keeps the web framework and the run
|
|
runtime out of the inner ring.
|
|
|
|
**Failed vs indeterminate is the load-bearing distinction** (#4452 / #4504).
|
|
``LaunchFailedError`` releases the task's single active slot, so it may only
|
|
be raised when no run can possibly have started; everything else has to be
|
|
``LaunchIndeterminateError``, which keeps the slot held with the identity
|
|
unknown. The split follows HTTP's own semantics: a 4xx means the request was
|
|
rejected before doing anything, while a 5xx -- or any other exception, or a
|
|
reply whose identity will not decode -- means the launch may already have had
|
|
its side effect. Guessing "failed" there is what re-opens duplicate execution.
|
|
|
|
TODO(hexagonal): this depends on ``launch_scheduled_thread_run``, a Gateway
|
|
service function returning an untyped dict, rather than on a contract published
|
|
by the run context -- that context has not been through a hexagonal slice yet.
|
|
When it publishes one (a DTO, not its aggregate and not its repository),
|
|
replace the body of this class. The ``RunLauncher`` port does not move.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Awaitable, Callable, Mapping
|
|
from typing import Any
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from deerflow.domain.schedule.exceptions import LaunchFailedError, LaunchIndeterminateError, ThreadBusyError
|
|
from deerflow.domain.schedule.ports import LaunchedRun, RunLauncher
|
|
from deerflow.runtime import ConflictError
|
|
|
|
LaunchRun = Callable[..., Awaitable[Mapping[str, Any]]]
|
|
|
|
|
|
class GatewayRunLauncher(RunLauncher):
|
|
"""Adapts the Gateway's scheduled-run launch path to the ``RunLauncher`` port.
|
|
|
|
Takes the launch callable rather than importing it, because the production
|
|
one is bound to the FastAPI app (``launch_scheduled_thread_run(app=app,
|
|
...)``) and that binding belongs to the composition root.
|
|
|
|
Explicit inheritance is a readability aid only: a misspelled method would
|
|
still instantiate fine and silently inherit the Protocol's ``...`` body,
|
|
so the contract tests must call every port method and assert on what it
|
|
returns.
|
|
"""
|
|
|
|
def __init__(self, launch_run: LaunchRun) -> None:
|
|
self._launch_run = launch_run
|
|
|
|
async def launch(
|
|
self,
|
|
*,
|
|
thread_id: str,
|
|
assistant_id: str | None,
|
|
prompt: str,
|
|
owner_user_id: str | None,
|
|
metadata: dict[str, str],
|
|
) -> LaunchedRun:
|
|
try:
|
|
result = await self._launch_run(
|
|
thread_id=thread_id,
|
|
assistant_id=assistant_id,
|
|
prompt=prompt,
|
|
owner_user_id=owner_user_id,
|
|
metadata=metadata,
|
|
)
|
|
except ConflictError as exc:
|
|
raise ThreadBusyError(str(exc)) from exc
|
|
except HTTPException as exc:
|
|
if exc.status_code == 409:
|
|
raise ThreadBusyError(str(exc.detail)) from exc
|
|
if exc.status_code < 500:
|
|
# Rejected on the way in -- bad argument, unknown thread. The
|
|
# request never got far enough to start anything, which is the
|
|
# only condition under which releasing the slot is safe.
|
|
raise LaunchFailedError(str(exc.detail)) from exc
|
|
# A 5xx is raised from inside the launch path, which may already
|
|
# have created the run before failing.
|
|
raise LaunchIndeterminateError(str(exc.detail)) from exc
|
|
except Exception as exc:
|
|
# Deliberately broad: the port promises the domain that nothing but
|
|
# its three errors escapes. Unclassifiable means we cannot certify
|
|
# that no run started -- a dropped connection after the request was
|
|
# sent looks exactly like this -- so it is indeterminate, never
|
|
# failed. `CancelledError` derives from BaseException and is not
|
|
# caught: shutdown is control flow, not a launch outcome.
|
|
raise LaunchIndeterminateError(str(exc)) from exc
|
|
|
|
run_id = result.get("run_id")
|
|
launched_thread_id = result.get("thread_id")
|
|
if not isinstance(run_id, str) or not isinstance(launched_thread_id, str):
|
|
# The launch returned, so a run probably exists -- we just cannot
|
|
# name it. This is the port's indeterminate case by definition
|
|
# (main's `launch_succeeded` is set before unpacking for the same
|
|
# reason); calling it a failure here would release the slot and let
|
|
# the next dispatch start a duplicate.
|
|
raise LaunchIndeterminateError(f"run launch returned no usable identity: {result!r}")
|
|
return LaunchedRun(run_id=run_id, thread_id=launched_thread_id)
|