mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-26 13:16:24 +00:00
* feat(scheduler): lifecycle rules, goal/limit parity and coded errors for scheduled tasks
- Migration 0031: nullable scheduled_tasks.unmet_streak_after_seq (internal
streak boundary) and stop_condition (the user's stop rule, own column).
- Stop rule is normalized (stop_rule.py) and appended only at launch; the
stored prompt never contains it. Wording adapts when scheduler.tool_enabled
is off.
- Willem 1: page-created tasks get goal evaluation, <standing_notes>, the
previous-run reference and own-schedule stop; request_stop and the
scheduled capability no longer require an origin conversation.
- Repository: single reactivation choke point raising
ScheduledTaskLimitsExhausted; streak boundary moves on goal/prompt/stop
condition/note changes (not on resume); goal-check failures neither count
nor reset the streak; pause of a finished task returns "finished"; trials
on a paused task keep the host pause marker.
- New repository reads: automatic_runs_used_for, active_run_status_for,
list_manageable_from_thread, thread_relation/thread_run on thread tasks,
task_ids_for_thread, run_number, and run_number/total_tokens/summary on
run history rows.
- REST: goal/max_runs/end_at/stop_condition on create and PATCH (null
clears, naive end_at = task timezone), resume recomputes from now with an
optional renewal body, once_time_passed, task_finished, limits_exhausted,
scheduler_not_running on create, trigger returns outcome/existing/thread_id,
task responses add automatic_runs_used and active_run_status.
- Coded errors {code, message, params} for routes, shared validation and the
chat capability (scheduled_task_errors.py), pinned by
contracts/scheduled_task_errors_contract.json; goal notes contract v2 with
check_failure_codes, scheduled_origin_key and host_run_errors (host_notes.py).
- /api/features adds scheduled_tasks {available, running, tool_enabled,
min_interval_seconds}; ScheduledTaskService.is_running.
- Docs: six README scheduler sections, API.md scheduled tasks section
(breaking error shape), CONFIGURATION.md, AGENTS notes, CHANGELOGs.
- Tests: migration 0031, stop rule, error contract, run listing, lifecycle
over HTTP, repository, service and capability cases; updated tests for the
new error shape, finished-task pause and head revision.
* feat(scheduler): conversation update/resume, run-conversation authority and readable launches
- schedule_task: update and resume actions, stop_condition and clear_fields
parameters, plain parameter types (value rules come back as coded results),
rewritten docstrings for schedule_task and stop_scheduled_task.
- Capability: update/resume share the REST validation and error codes;
interactive turns in a task's run conversation may manage that task;
scheduled runs keep only own-stop; grants require a running scheduler.
- Results: compact JSON-safe task views with local times, display card/text,
last run in list, timezone_source/browser_timezone, now_local; no ISO, user,
lease or origin fields; stop_instructions removed.
- Timezone: explicit > browser (context.client_timezone, validated in
start_run and never forwarded to the run config) > not needed for intervals
and offset run_at > timezone_required.
- Trial gate accepts natural direct requests after acknowledgements and
particles; trial results report thread_id and existing.
- Scheduled launches: stable message id scheduled-<task_run_id>, server-owned
deerflow_scheduled_origin metadata with the user-language parts, run thread
title for fresh-thread tasks.
- Worker records goal continuations in the verdict; lead prompt language rule
covers stored task text and scheduled runs.
- Docs: six READMEs, CONFIGURATION, API, runtime/tools AGENTS, CHANGELOGs.
* feat(frontend): scheduled-task core types, formatting, errors and glossary
- throwGatewayApiError now throws GatewayApiError (status, code, params,
rawMessage); message stays the string detail or detail.message
- scheduled-task types gain stop_condition, automatic_runs_used,
active_run_status, origin/notes, run_number/total_tokens/summary, thread
relation, trigger result, renewal and scheduled origin; isTaskBusy and
hasActiveRun helpers
- api: fetchScheduledTask, resume with optional renewal body, typed trigger
result and update payload with null clears
- hooks: polling at 3 s while a run is active (list, thread list, run
history), useScheduledTask with live gating and final 404, useInView,
localized error toasts; run history refreshes tasks when a run finishes
- new pure modules: errors.ts (contract code to localized copy), format.ts
(relative task times, viewer time, display zone), status.ts, actions.ts,
run-error.ts; describeTaskSchedule in cron.ts; goal-outcome adds unchecked
goal checks, describeTaskOutcome and parseStopRunId
- features: scheduled_tasks block with defaults when missing
- buildRunContext sends client_timezone
- titleOfThread takes a localized untitled label; breadcrumb names scheduled
tasks and agents; reference chips use the localized untitled label
- schedule input: shared browserTimeZone, labelled selects, aria-pressed
type toggles, interval minimum from the hint template
- i18n: all PR1 scheduled-task keys in en-US and zh-CN, glossary fixes,
recipe instructions moved into i18n
- e2e mocks follow the new task, trigger, resume, thread-task and features
shapes; expectNoRawIdentifiers helpers for e2e and DOM tests
- tests for errors, format, cron, status, actions, goal outcome, run errors,
hooks, run context, thread titles and features; scheduled e2e copy updated
* feat(frontend): list-first scheduled tasks page with outcomes and editable goals
- page.tsx is now a thin composition of components/workspace/scheduled-tasks:
status tabs (All/Active/Paused/Finished) and search over the task list,
?task_id= deep link with router.replace on selection, ?thread_id= chip
with Show all, empty state, scheduler-off notice and unavailable state
- task list rows show the schedule in words and one status line (next run,
running now, paused by agent, auto-paused, finished by its cap)
- task detail: status badge, actions from availableActions with reasons,
More actions menu (Duplicate, Copy task ID, Delete), outcome notice,
Runs / Stops when / Each run's goal / Does / Notes from chat / History,
origin chat link; success toasts for every action
- run rows: run number or Trial run, summary, goal and assumption badges,
continuations, tokens, Open chat link; raw errors and the evaluator
reason only behind Details
- create/edit/duplicate dialog with goal, stop condition, safety caps and
advanced agent/context options; PATCH sends only changed fields, null
clears a cap
- renew dialog raises or removes caps and resumes in one request; a
limits_exhausted Resume opens it
- removed i18n keys the old page used; added feedback.resumedNoTime
- recorded en-3 and zh-1 page fixtures; scheduled e2e specs rewritten for
the new page; DOM tests for the detail and the form dialog
- CHANGELOG entries and the frontend AGENTS.md scheduled-task note
* feat(frontend): live schedule card, run conversation provenance and header entry
- schedule_task results (create, update, pause, resume, trial, delete) become
an assistant:scheduled-task message group rendered as a live card: schedule,
next run, stop condition with the safety cap, goal, where results go, Run
once now, Pause/Resume and Open task; the card polls the task only while it
is on screen and shows deleted, scheduler-off and limits-exhausted states
- one card per task per assistant turn; the card group is not a turn boundary
for usage, branching, editing, outline or skill attribution
- a human message with deerflow_scheduled_origin stays a human group with
scheduledOrigin and renders as a "Scheduled run / Trial run" block with the
task instructions, stop condition and notes collapsed; the launched text is
never shown and the message is not editable
- readable step labels for schedule_task actions and stop_scheduled_task
- ThreadScheduledTasksButton replaces the header link: hidden without tasks,
shows the count, links to the task (or the chat filter), and a run's chat
links to its task
- recorded en-3 / zh-1 chat and run-thread fixtures; chat-card and run-thread
e2e specs; unit and DOM tests for parsing, grouping, turn boundaries, card,
run block, header button, step labels and reconnect hydration
- i18n: header.countLabelOne added, unused card.next removed; CHANGELOG and
frontend AGENTS.md notes
* fix(scheduler): address verification findings
Move the scheduled-task lifecycle guidance from backend/AGENTS.md into the persistence guide and leave a one-line pointer, so the effective AGENTS.md chains for agents/middlewares, sandbox and subagents stay under the 98304-byte hard limit enforced by scripts/check_agent_guidance.py in lint-check.
* fix(scheduler): address review findings
* fix(scheduled-tasks): polish from live acceptance
- zh schedule wording: an interval of one unit reads 每分钟 / 每小时 / 每秒,
and a saved interval of whole days reads 每天 / 每 2 天 (Every day / Every 2
days) instead of 每 1 分钟 or 每 24 小时; several units keep the number
- zh spacing: 已于{time} 满足 and 在{time} 的运行中 now leave a space between
the interpolated time and the Chinese after it; a copy test guards every
scheduled-task zh string against a placeholder glued to Chinese
- run summary: when the first line of the final reply only leads in to a list
(ends with ":" or ":"), the list items follow it as plain text (bullets,
numbering, checkboxes, bold, code and links stripped), joined with ";" for
CJK text and "; " otherwise, within the 160-character cap
- a schedule_task result (create, update, pause, resume, trial, delete) that
arrives in a chat invalidates that chat's task list, the tasks list and the
named task, so the header button and cards update without waiting for the
idle poll; results already in the loaded history do not refetch
- e2e and unit fixtures are now the live acceptance recordings (DeepSeek
Flash, demo data, fictional names): live-weekday.json (create, trial, edit,
pause, resume in one zh chat) and live-minute.json (per-minute chat, run 1
and run 2, paused by the agent), fetched with GET from the demo Gateway and
trimmed; the hand-shaped en-3 / zh-1 fixtures are removed and the chat-card,
run-thread and tasks-page specs run in both locales over the recorded data
* docs(changelog): reference #6378 for the scheduled-tasks entries
* docs(agents): shorten the scheduled-task pointer to stay under the guidance limit
* test(scheduled-tasks): pin the live takes to their recorded time zone
The live fixtures were recorded in Asia/Shanghai, and an interval task
reads in the viewer's zone, so the 16:48 assertions failed on UTC CI
runners. Pin the browser zone in the two live-take e2e specs and the
zh live-take unit test.
* fix(scheduled-tasks): keep untouched end times and drop a chat filter the new task is not in
- The edit, duplicate and renew dialogs keep a stored end time's instant
while its field still shows it. A minute-precision wall value cannot
keep seconds or name the later 01:30 of a DST fall-back, so a
title-only edit moved the cap an hour earlier, and raising the run
limit re-sent the end time without its seconds.
- Creating or duplicating a task from a chat's task view drops the chat
filter when the new task does not belong to that chat, instead of
showing "This task isn't one of this chat's tasks".
* fix(scheduled-tasks): tie pause notices to the pause they describe
- Paused by agent links and times only the run named in last_error. When
that run is not loaded, the notice shows no link and the task's own time
instead of an earlier run that also asked to stop.
- The outcome notice reads the latest history page whichever page is
shown, so browsing older runs no longer swaps the auto-pause reason and
"Open latest run" to an earlier miss. It shares that page's request.
* fix(scheduled-tasks): three lifecycle gaps found while re-checking review classes
- The edit form compares a cron in the form its schedule input emits, so an
untouched "0 09 * * 0,6" is not re-sent as "0 9 * * 6,0" and a title-only
edit no longer re-arms or refuses a finished task.
- Chat resume asks for a timezone, as update does, when a naive end_at would
land in the UTC placeholder of a zone-free task.
- Re-arming a task clears a host pause marker, as resume does, so a later
manual pause no longer reads as "Paused by agent".
* fix(scheduled-tasks): keep the notice's latest-runs query from refreshing older pages
useLatestScheduledTaskRuns kept React Query's default refetch on mount,
focus and reconnect, so going offline and back while an older history page
was shown fetched the latest page again. Refreshing stays with the history,
which refreshes the latest page only while it is shown.
* test(scheduled-tasks): let the viewer's time carry its own day label
Between 16:00 and 24:00 UTC, UTC and Asia/Shanghai are on different days,
so the run prompt shows "Tomorrow 01:00 · Today 09:00 your time" and the
fixed "01:00 · 09:00 your time" assertion failed depending on the hour.
---------
Co-authored-by: Totoro-qaq <279883115+Totoro-qaq@users.noreply.github.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
180 lines
7.8 KiB
Python
180 lines
7.8 KiB
Python
from datetime import UTC, datetime
|
|
from types import SimpleNamespace
|
|
from unittest.mock import Mock
|
|
from zoneinfo import ZoneInfo
|
|
|
|
import httpx
|
|
import pytest
|
|
import pytest_asyncio
|
|
from fastapi import FastAPI
|
|
|
|
from app.gateway.authz import AuthContext
|
|
from app.gateway.routers import scheduled_tasks
|
|
from deerflow.scheduler.schedules import next_run_at
|
|
|
|
URL = "/api/scheduled-tasks/preview-cron"
|
|
PAYLOAD = {"cron": "0 9 * * 1-5", "timezone": "Asia/Shanghai", "count": 3, "start_at": "2026-09-12T00:00:00Z"}
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client(monkeypatch):
|
|
app = FastAPI()
|
|
app.include_router(scheduled_tasks.router)
|
|
|
|
@app.middleware("http")
|
|
async def authenticate(request, call_next):
|
|
user = None if request.headers.get("x-test-auth") == "anonymous" else SimpleNamespace(id="preview-user")
|
|
permissions = [] if request.headers.get("x-test-auth") == "denied" else ["threads:read"]
|
|
request.state.auth = AuthContext(user=user, permissions=permissions)
|
|
return await call_next(request)
|
|
|
|
async def user_from_request(request):
|
|
return request.state.auth.user
|
|
|
|
monkeypatch.setattr(scheduled_tasks, "get_optional_user_from_request", user_from_request)
|
|
# A preview must remain usable without a task database, thread store or worker.
|
|
for name in ("get_scheduled_task_repo", "get_scheduled_task_run_repo", "get_thread_store", "get_scheduled_task_service", "get_config"):
|
|
monkeypatch.setattr(scheduled_tasks, name, Mock(side_effect=AssertionError(f"Preview accessed {name}")))
|
|
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as session:
|
|
yield session
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_preview_returns_exact_utc_and_local_times_without_persistence(client):
|
|
response = await client.post(URL, json={**PAYLOAD, "cron": " 0\t9 * * 1-5 "})
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {
|
|
"cron": "0 9 * * 1-5",
|
|
"timezone": "Asia/Shanghai",
|
|
"start_at": "2026-09-12T00:00:00Z",
|
|
"occurrences": [
|
|
{"run_at": "2026-09-14T01:00:00Z", "local_time": "2026-09-14T09:00:00+08:00"},
|
|
{"run_at": "2026-09-15T01:00:00Z", "local_time": "2026-09-15T09:00:00+08:00"},
|
|
{"run_at": "2026-09-16T01:00:00Z", "local_time": "2026-09-16T09:00:00+08:00"},
|
|
],
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(("auth", "status"), [("anonymous", 401), ("denied", 403)])
|
|
async def test_preview_enforces_authentication_and_read_permission(client, auth, status):
|
|
response = await client.post(URL, json=PAYLOAD, headers={"x-test-auth": auth})
|
|
assert response.status_code == status
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
"override",
|
|
[
|
|
{"count": 0},
|
|
{"count": 11},
|
|
{"count": True},
|
|
{"count": "3"},
|
|
{"cron": ""},
|
|
{"cron": " " * 257},
|
|
{"cron": "0 0 * * * *"},
|
|
{"cron": "61 * * * *"},
|
|
{"cron": "0 0 31 2 *"},
|
|
{"timezone": "Not/A_Zone"},
|
|
{"timezone": "../UTC"},
|
|
{"timezone": "/UTC"},
|
|
{"timezone": ""},
|
|
{"timezone": "x" * 129},
|
|
{"start_at": "2026-09-12T00:00:00"},
|
|
{"start_at": "invalid"},
|
|
{"start_at": "9999-12-31T23:59:59Z"},
|
|
],
|
|
)
|
|
async def test_preview_rejects_invalid_or_unfulfillable_inputs(client, override):
|
|
response = await client.post(URL, json={**PAYLOAD, **override})
|
|
assert response.status_code == 422, response.text
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_preview_default_reference_is_captured_once_and_count_defaults_to_five(client, monkeypatch):
|
|
class FixedDatetime(datetime):
|
|
calls = 0
|
|
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
cls.calls += 1
|
|
return datetime(2026, 9, 12, 0, 0, tzinfo=UTC)
|
|
|
|
monkeypatch.setattr(scheduled_tasks, "datetime", FixedDatetime)
|
|
response = await client.post(URL, json={"cron": "0 * * * *", "timezone": "UTC"})
|
|
assert response.status_code == 200, response.text
|
|
data = response.json()
|
|
assert FixedDatetime.calls == 1
|
|
assert data["start_at"] == "2026-09-12T00:00:00Z"
|
|
assert [row["run_at"] for row in data["occurrences"]] == [f"2026-09-12T0{hour}:00:00Z" for hour in range(1, 6)]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_preview_normalizes_offset_reference_and_excludes_reference_instant(client):
|
|
response = await client.post(URL, json={**PAYLOAD, "cron": "0 * * * *", "count": 1, "start_at": "2026-09-12T08:00:00+08:00"})
|
|
assert response.status_code == 200, response.text
|
|
assert response.json()["start_at"] == "2026-09-12T00:00:00Z"
|
|
assert response.json()["occurrences"][0]["run_at"] == "2026-09-12T01:00:00Z"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("start_at", ["2026-03-07T12:00:00Z", "2026-10-31T12:00:00Z"])
|
|
async def test_preview_preserves_scheduler_dst_semantics(client, start_at):
|
|
cron = "30 2 * * *"
|
|
zone = "America/New_York"
|
|
cursor = datetime.fromisoformat(start_at)
|
|
expected = []
|
|
for _ in range(10):
|
|
cursor = next_run_at("cron", {"cron": cron}, zone, now=cursor)
|
|
expected.append(cursor)
|
|
response = await client.post(URL, json={"cron": cron, "timezone": zone, "count": 10, "start_at": start_at})
|
|
assert response.status_code == 200, response.text
|
|
rows = response.json()["occurrences"]
|
|
assert [datetime.fromisoformat(row["run_at"]) for row in rows] == expected
|
|
assert [row["local_time"] for row in rows] == [instant.astimezone(ZoneInfo(zone)).isoformat() for instant in expected]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_preview_calculates_off_the_request_thread(client, monkeypatch):
|
|
import threading
|
|
|
|
request_thread = threading.get_ident()
|
|
observed = []
|
|
original = scheduled_tasks.compute_next_run_at
|
|
|
|
def calculate(*args, **kwargs):
|
|
observed.append(threading.get_ident())
|
|
return original(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(scheduled_tasks, "compute_next_run_at", calculate)
|
|
response = await client.post(URL, json=PAYLOAD)
|
|
assert response.status_code == 200, response.text
|
|
assert len(observed) == 3
|
|
assert all(ident != request_thread for ident in observed)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_preview_first_occurrence_matches_task_creation_at_same_clock(client, monkeypatch):
|
|
from unittest.mock import AsyncMock
|
|
|
|
from _router_auth_helpers import call_unwrapped
|
|
|
|
class FixedDatetime(datetime):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
return datetime(2026, 9, 12, 0, 0, tzinfo=UTC)
|
|
|
|
monkeypatch.setattr(scheduled_tasks, "datetime", FixedDatetime)
|
|
response = await client.post(URL, json={"cron": PAYLOAD["cron"], "timezone": PAYLOAD["timezone"], "count": 1})
|
|
assert response.status_code == 200, response.text
|
|
# Only the separate creation below is allowed to acquire persistence dependencies.
|
|
repo = SimpleNamespace(create=AsyncMock(return_value={"id": "task-preview"}), automatic_runs_used_for=AsyncMock(return_value={}), get_active_run_status=AsyncMock(return_value=None))
|
|
monkeypatch.setattr(scheduled_tasks, "get_scheduled_task_repo", lambda request: repo)
|
|
monkeypatch.setattr(scheduled_tasks, "is_scheduler_running", lambda request: True)
|
|
monkeypatch.setattr(scheduled_tasks, "get_thread_store", lambda request: None)
|
|
monkeypatch.setattr(scheduled_tasks, "get_config", lambda: SimpleNamespace(scheduler=SimpleNamespace(min_once_delay_seconds=60)))
|
|
request = SimpleNamespace(state=SimpleNamespace(auth=AuthContext(user=SimpleNamespace(id="preview-user"))))
|
|
body = scheduled_tasks.ScheduledTaskCreateRequest(title="Example", prompt="Example", schedule_type="cron", schedule_spec={"cron": PAYLOAD["cron"]}, timezone=PAYLOAD["timezone"])
|
|
await call_unwrapped(scheduled_tasks.create_scheduled_task, request=request, body=body)
|
|
assert repo.create.await_args.kwargs["next_run_at"] == datetime.fromisoformat(response.json()["occurrences"][0]["run_at"])
|