mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-09-18 18:46:17 +00:00
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.
75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
"""Boundary mapping (not a port implementation) -- RunRecord -> RunOutcome.
|
|
|
|
Unlike its siblings in this package, this module implements no port: it is the
|
|
inbound translation the composition root installs on the run runtime's
|
|
completion hook, so the domain never imports ``RunRecord``.
|
|
|
|
It also owns the filtering the legacy completion hook did inline. Every run in
|
|
the process reaches that hook, so most of them are none of this context's
|
|
business, and returning ``None`` says exactly that -- not an error, just
|
|
nothing to write back. The service is then never called at all, which is why it
|
|
has no guard clauses of its own.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from deerflow.domain.schedule.model import RunStatus
|
|
from deerflow.domain.schedule.ports import RunOutcome
|
|
|
|
if TYPE_CHECKING:
|
|
from deerflow.runtime import RunRecord
|
|
|
|
# The runtime reports four terminal states; the domain has three, because
|
|
# `timeout` and `error` are the same fact to a scheduled task while
|
|
# `interrupted` is deliberately not -- a cancel or same-thread takeover ends
|
|
# the task CANCELLED, not FAILED.
|
|
_TERMINAL_STATUSES = {
|
|
"success": RunStatus.SUCCESS,
|
|
"error": RunStatus.FAILED,
|
|
"timeout": RunStatus.FAILED,
|
|
"interrupted": RunStatus.INTERRUPTED,
|
|
}
|
|
|
|
_INTERRUPTED_WITHOUT_ERROR = "run was interrupted before completion"
|
|
|
|
|
|
def run_outcome_from_record(record: RunRecord) -> RunOutcome | None:
|
|
"""Translate a finished run into domain vocabulary, or `None` to ignore it.
|
|
|
|
`None` is returned when the run is not a scheduled execution (no usable
|
|
task metadata, no owner) or has not reached a terminal state yet.
|
|
"""
|
|
metadata = record.metadata or {}
|
|
task_id = metadata.get("scheduled_task_id")
|
|
record_id = metadata.get("scheduled_task_run_id")
|
|
user_id = record.user_id
|
|
# `metadata` is a free-form dict a caller can influence, so the ids are
|
|
# type-checked rather than assumed; `user_id` is required because every
|
|
# task read is scoped by it.
|
|
if not isinstance(task_id, str) or not isinstance(record_id, str) or not user_id:
|
|
return None
|
|
|
|
status = _TERMINAL_STATUSES.get(str(record.status.value))
|
|
if status is None:
|
|
return None
|
|
|
|
if status is RunStatus.SUCCESS:
|
|
# A stale error left on a successful record must not be written back as
|
|
# the task's last_error.
|
|
error = None
|
|
elif status is RunStatus.INTERRUPTED:
|
|
error = record.error or _INTERRUPTED_WITHOUT_ERROR
|
|
else:
|
|
error = record.error
|
|
|
|
return RunOutcome(
|
|
task_id=task_id,
|
|
record_id=record_id,
|
|
run_id=record.run_id,
|
|
user_id=user_id,
|
|
status=status,
|
|
error=error,
|
|
)
|