rayhpeng 7852421c68 feat(schedule): complete the outer ring with the launch adapters
Adds the three remaining adapters plus the poller. Nothing is wired yet --
the composition root is the next commit -- so this is additive and the
legacy `app/scheduler/service.py` still serves production.

`run_launcher.py` is the pivot of the whole slice. The Gateway signals a
busy thread two ways -- `ConflictError` from the run manager, or an
`HTTPException(409)` from the route-level path -- which is why the legacy
scheduler service imported fastapi to tell them apart. Both are one domain
fact, and saying so here is what lets that import disappear without the
busy/failed distinction disappearing with it. Everything else becomes
`LaunchFailedError`, because the port promises the domain that nothing but
its two errors escapes. `CancelledError` is deliberately not caught:
shutdown is control flow, not a launch outcome.

`thread_lookup.py` narrows `ThreadMetaStore` to the one question this
context asks. `require_existing=True` is load-bearing -- the store's
default treats an absent row as accessible, which is right for a thread
not yet written and wrong for binding a task to it.

Both inherit their port explicitly, matching every other adapter in the
codebase including feedback's own anti-corruption layer, and both carry
the TODO naming the published contract that would replace them once the
upstream context has been through a slice of its own.

`run_outcome_mapping.py` implements no port: it is the inbound translation
the composition root will install on the completion hook, and it owns the
filtering the legacy hook did inline. Returning None means "this run is
none of the schedule context's business", so the service is simply never
called and needs no guard clauses.

`poller.py` keeps the two behaviours the legacy loop got right: a failing
poll must not end the loop (one transient "database is locked" used to
stop scheduling for the rest of the process life), and reconciliation must
not block startup.

One deliberate behaviour change: the legacy `start()` swept stale runs and
stuck once-tasks under separate try/excepts, so the first failing did not
stop the second. `reconcile_on_startup` is one call that lets failures
propagate -- the domain's position is that fatality is the caller's policy
-- so the poller's single except means a failed first sweep now skips the
second. Both end up logged and non-fatal, as before.

Tests: 50 new cases across the four modules, each port method called and
asserted on its return value. That is not decoration: inheriting a
Protocol means a misspelled method silently inherits its `...` body and
returns None, so the suite was verified by mutation -- renaming `launch`
and `exists_for_user` turns 16 and 6 cases red respectively.
2026-07-28 18:28:46 +08:00

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))