rayhpeng c38d291505 refactor(schedule): standardize the module on the hexagonal architecture
Replaces the pre-hexagonal scheduled-task implementation with a slice
built to the layering spec: a pure domain (two aggregates, two state
machines, the policy value object), output ports it declares itself,
SQL/launcher/thread adapters implementing them under `app/adapters/`,
and a composition root that is the one place any of them is
instantiated.

The old implementation mixed all of that into `app/scheduler/service.py`
and a router that reached straight into repositories, so the rules that
matter -- overlap policy, lease handling, which write owns which
timestamp -- were only reachable through a live database. They are now
unit-assertable on in-memory fakes, with the contract suite running each
port against both the fake and real sqlite, and the concurrency
invariants pinned by dedicated race tests.

Two bugs the old shape hid are fixed on the way: a completion hook that
replayed a stale snapshot and rolled back the launch write, and a
corrupt stored row surfacing to the client as a 4xx.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 11:16: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))