mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-08-15 09:19:02 +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>
47 lines
2.0 KiB
Python
47 lines
2.0 KiB
Python
"""Secondary adapter (anti-corruption layer) -- narrowing ThreadMetaStore to ThreadLookup.
|
|
|
|
Implements ``ThreadLookup`` from ``deerflow.domain.schedule.ports``. This
|
|
context does not own the ``threads_meta`` table and writes no SQL against it: it
|
|
asks its one question through the store the thread context already provides.
|
|
|
|
``require_existing=True`` is the load-bearing argument. The store's default
|
|
treats an absent row as accessible -- reasonable for a thread that has not been
|
|
written yet, wrong for binding a task to it, since the task would then reference
|
|
a thread that never existed.
|
|
|
|
TODO(hexagonal): this depends on ``ThreadMetaStore``, an infrastructure
|
|
component, rather than on a contract published by the thread 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 ``ThreadLookup`` port does not move.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from deerflow.domain.schedule.ports import ThreadLookup
|
|
|
|
if TYPE_CHECKING:
|
|
from deerflow.persistence.thread_meta.base import ThreadMetaStore
|
|
|
|
|
|
class ThreadStoreThreadLookup(ThreadLookup):
|
|
"""Adapts the wide ``ThreadMetaStore`` to the one question this context asks.
|
|
|
|
Both halves of that question -- does the thread exist, and may this user use
|
|
it -- collapse into a single bool on purpose: reporting them separately
|
|
would let a caller probe for the existence of threads they cannot see.
|
|
|
|
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, thread_store: ThreadMetaStore) -> None:
|
|
self._thread_store = thread_store
|
|
|
|
async def exists_for_user(self, thread_id: str, user_id: str) -> bool:
|
|
return bool(await self._thread_store.check_access(thread_id, user_id, require_existing=True))
|