deer-flow/backend/app/gateway/scheduled_task_validation.py
Totoro 2dd2e5415c
feat(scheduled-tasks): finish the user-facing side of conversation schedules (RFC #6340, 1/2) (#6378)
* 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>
2026-10-07 07:47:32 +08:00

373 lines
19 KiB
Python

"""Scheduled-task validation shared by HTTP and conversation schedule operations.
Every rule raises a coded error (``scheduled_task_errors.scheduler_error``), so
the tasks page and the agent receive the same ``code`` for the same mistake.
Request models declare only types; value rules live here.
"""
import asyncio
from collections.abc import Awaitable, Callable, Collection
from datetime import UTC, datetime
from typing import Any
from zoneinfo import ZoneInfo
from app.gateway.scheduled_task_errors import scheduler_error
from deerflow.persistence.scheduled_task_runs.finalization import is_host_pause_marker
from deerflow.scheduler.schedules import MAX_INTERVAL_SECONDS, next_run_at, normalize_cron_expression, parse_interval_seconds, validate_timezone
from deerflow.scheduler.stop_rule import MAX_STOP_CONDITION_CHARS, normalize_stop_condition
from deerflow.utils.goal_objective import MAX_GOAL_OBJECTIVE_CHARS, normalize_goal_objective
FREQUENT_THRESHOLD_SECONDS = 3600
TERMINAL_TASK_STATUSES = frozenset({"completed", "failed", "cancelled"})
CONTEXT_MODES = frozenset({"fresh_thread_per_run", "reuse_thread"})
SCHEDULE_TYPES = frozenset({"once", "cron", "interval"})
# Fields a PATCH (or chat update) may clear by sending null.
CLEARABLE_FIELDS = frozenset({"goal_objective", "max_runs", "end_at", "stop_condition"})
_BOUND_FIELDS = frozenset({"goal_objective", "max_runs", "end_at"})
def validate_interval_seconds(schedule_spec: dict[str, Any], min_seconds: int) -> int:
try:
every_seconds = parse_interval_seconds(schedule_spec)
except ValueError as exc:
raise scheduler_error(422, "invalid_schedule", str(exc)) from exc
if every_seconds < min_seconds:
raise scheduler_error(422, "interval_too_short", f"interval schedule must be at least {min_seconds} seconds", min_seconds=min_seconds)
if every_seconds > MAX_INTERVAL_SECONDS:
raise scheduler_error(422, "interval_too_long", f"interval schedule must be at most {MAX_INTERVAL_SECONDS} seconds", max_seconds=MAX_INTERVAL_SECONDS)
return every_seconds
def is_frequent_schedule(schedule_type: str, schedule_spec: dict[str, Any]) -> bool:
"""More often than hourly: a sub-hour interval, or a cron with a non-fixed minute."""
if schedule_type == "interval":
every_seconds = schedule_spec.get("every_seconds")
return isinstance(every_seconds, int) and not isinstance(every_seconds, bool) and every_seconds < FREQUENT_THRESHOLD_SECONDS
if schedule_type == "cron":
fields = str(schedule_spec.get("cron", "")).split()
minute = fields[0] if fields else ""
return not (minute.isascii() and minute.isdigit())
return False
def _as_datetime(value: datetime | str | None) -> datetime | None:
"""Read a stored timestamp (``_row_to_dict`` returns ISO strings) as aware UTC."""
if value is None:
return None
if isinstance(value, str):
value = datetime.fromisoformat(f"{value[:-1]}+00:00" if value.endswith("Z") else value)
return value.replace(tzinfo=UTC) if value.tzinfo is None else value.astimezone(UTC)
def resolve_end_at(value: datetime | str | None, timezone: str) -> datetime | None:
"""Return ``end_at`` as aware UTC; a naive value is wall-clock time in the task's timezone.
UTC because SQLite drops the offset of DateTime(timezone=True) columns.
"""
if value is None:
return None
if isinstance(value, str):
try:
value = datetime.fromisoformat(f"{value[:-1]}+00:00" if value.endswith("Z") else value)
except ValueError as exc:
raise scheduler_error(422, "invalid_request", "end_at must be an ISO 8601 date-time") from exc
if not isinstance(value, datetime):
raise scheduler_error(422, "invalid_request", "end_at must be an ISO 8601 date-time")
if value.tzinfo is None:
value = value.replace(tzinfo=ZoneInfo(timezone))
return value.astimezone(UTC)
def validate_task_bounds(
*,
goal_objective: str | None,
max_runs: int | None,
end_at: datetime | str | None,
context_mode: str,
schedule_type: str,
schedule_spec: dict[str, Any],
next_run_at: datetime | str | None,
now: datetime,
used: int = 0,
require_limit_for_frequent: bool,
changed: Collection[str] | None = None,
) -> None:
"""Check goal and safety-cap rules on a (merged) task definition.
``changed`` limits the max_runs/end_at value checks to the fields a
request sets, so renaming a task whose cap is used up or whose end time
passed is not rejected; ``None`` checks everything (creation). A changed
``next_run_at`` (schedule edit, resume) re-checks that a still-future
``end_at`` leaves room for the next run; an ``end_at`` that already passed
is left to the repository's reactivation check (``limits_exhausted``).
"""
def checks(field: str) -> bool:
return changed is None or field in changed
if goal_objective is not None:
if not isinstance(goal_objective, str):
raise scheduler_error(422, "invalid_goal", "goal_objective must be a string", max_chars=MAX_GOAL_OBJECTIVE_CHARS)
try:
normalize_goal_objective(goal_objective)
except ValueError as exc:
raise scheduler_error(422, "invalid_goal", str(exc), max_chars=MAX_GOAL_OBJECTIVE_CHARS) from exc
if context_mode != "fresh_thread_per_run":
raise scheduler_error(422, "goal_requires_fresh_thread", "goal-backed schedules require fresh_thread_per_run")
if max_runs is not None and checks("max_runs"):
if isinstance(max_runs, bool) or not isinstance(max_runs, int) or max_runs < 1:
raise scheduler_error(422, "invalid_max_runs", "max_runs must be a positive integer")
if used > 0 and max_runs <= used:
raise scheduler_error(422, "max_runs_not_above_used", f"max_runs must be greater than the {used} automatic runs already used", used=used)
if end_at is not None and (checks("end_at") or checks("next_run_at")):
end = _as_datetime(end_at)
if end <= now and checks("end_at"):
raise scheduler_error(422, "end_at_in_past", "end_at must be in the future")
upcoming = _as_datetime(next_run_at)
if end > now and upcoming is not None and end <= upcoming:
raise scheduler_error(422, "end_at_before_first_run", "end_at must allow at least one scheduled occurrence")
if require_limit_for_frequent and max_runs is None and end_at is None and is_frequent_schedule(schedule_type, schedule_spec):
raise scheduler_error(422, "frequent_requires_limit", "Schedules more frequent than hourly require max_runs or end_at")
def _normalized_stop_condition(value: Any) -> str | None:
if value is not None and not isinstance(value, str):
raise scheduler_error(422, "invalid_stop_condition", "stop_condition must be a string", max_chars=MAX_STOP_CONDITION_CHARS)
try:
return normalize_stop_condition(value)
except ValueError as exc:
raise scheduler_error(422, "invalid_stop_condition", str(exc), max_chars=MAX_STOP_CONDITION_CHARS) from exc
def _check_once_time(schedule_type: str, upcoming: datetime | None, *, scheduler_config: Any, now: datetime) -> None:
if schedule_type != "once":
return
if upcoming is None:
raise scheduler_error(422, "once_in_past", "once schedule must be in the future")
min_seconds = scheduler_config.min_once_delay_seconds
if (upcoming - now).total_seconds() < min_seconds:
raise scheduler_error(422, "once_too_soon", f"once schedule must be at least {min_seconds} seconds in the future", min_seconds=min_seconds)
def _validate_schedule(schedule_type: str, schedule_spec: dict[str, Any], timezone: str, *, scheduler_config: Any, now: datetime) -> tuple[dict[str, Any], datetime | None]:
schedule_spec = dict(schedule_spec)
try:
validate_timezone(timezone)
except ValueError as exc:
raise scheduler_error(422, "invalid_timezone", str(exc)) from exc
try:
if schedule_type == "cron":
raw_cron = schedule_spec.get("cron")
if not isinstance(raw_cron, str):
raise scheduler_error(422, "invalid_schedule", "cron schedule requires schedule_spec.cron")
schedule_spec["cron"] = normalize_cron_expression(raw_cron)
if schedule_type == "interval":
validate_interval_seconds(schedule_spec, scheduler_config.min_once_delay_seconds)
upcoming = next_run_at(schedule_type, schedule_spec, timezone, now=now)
except (ValueError, OverflowError) as exc:
raise scheduler_error(422, "invalid_schedule", str(exc)) from exc
_check_once_time(schedule_type, upcoming, scheduler_config=scheduler_config, now=now)
return schedule_spec, upcoming
async def validate_scheduled_task_create(
body: Any,
*,
user_id: str,
thread_store: Any,
scheduler_config: Any,
assistant_resolver: Callable[..., Awaitable[str]],
now: datetime | None = None,
conversation_rules: bool = False,
) -> dict[str, Any]:
"""Return repository ``create`` fields after the shared checks.
The prompt is stored exactly as given; the stop condition is normalized
into its own field. ``conversation_rules`` applies the chat-only rule
that sub-hourly schedules need a safety cap.
"""
reference = now or datetime.now(UTC)
if body.context_mode not in CONTEXT_MODES:
raise scheduler_error(422, "invalid_context_mode", "Unsupported context_mode")
if body.context_mode == "reuse_thread":
if not body.thread_id:
raise scheduler_error(422, "reuse_thread_requires_thread", "reuse_thread requires thread_id")
if not await thread_store.check_access(body.thread_id, user_id, require_existing=True):
raise scheduler_error(404, "thread_not_found", "Thread not found")
if body.schedule_type not in SCHEDULE_TYPES:
raise scheduler_error(422, "invalid_schedule_type", "Unsupported schedule_type")
schedule_spec, upcoming = await asyncio.to_thread(_validate_schedule, body.schedule_type, body.schedule_spec, body.timezone, scheduler_config=scheduler_config, now=reference)
assistant_id = await assistant_resolver(body.assistant_id, user_id=user_id)
stop_condition = _normalized_stop_condition(getattr(body, "stop_condition", None))
goal_objective = getattr(body, "goal_objective", None)
max_runs = getattr(body, "max_runs", None)
end_at = await asyncio.to_thread(resolve_end_at, getattr(body, "end_at", None), body.timezone)
validate_task_bounds(
goal_objective=goal_objective,
max_runs=max_runs,
end_at=end_at,
context_mode=body.context_mode,
schedule_type=body.schedule_type,
schedule_spec=schedule_spec,
next_run_at=upcoming,
now=reference,
require_limit_for_frequent=conversation_rules,
)
return {
"thread_id": body.thread_id,
"context_mode": body.context_mode,
"assistant_id": assistant_id,
"title": body.title,
"prompt": body.prompt,
"stop_condition": stop_condition,
"schedule_type": body.schedule_type,
"schedule_spec": schedule_spec,
"timezone": body.timezone,
"next_run_at": upcoming,
"goal_objective": goal_objective,
"max_runs": max_runs,
"end_at": end_at,
}
def _recompute_schedule(existing: dict[str, Any], updates: dict[str, Any], *, scheduler_config: Any, now: datetime) -> None:
schedule_type = str(updates.get("schedule_type", existing["schedule_type"]))
if schedule_type != existing["schedule_type"] and not isinstance(updates.get("schedule_spec"), dict):
raise scheduler_error(422, "invalid_schedule", "changing schedule_type requires schedule_spec")
schedule_spec = dict(updates["schedule_spec"]) if isinstance(updates.get("schedule_spec"), dict) else dict(existing["schedule_spec"])
timezone = str(updates.get("timezone", existing["timezone"]))
try:
if schedule_type == "cron":
raw_cron = schedule_spec.get("cron")
if not isinstance(raw_cron, str):
raise scheduler_error(422, "invalid_schedule", "cron schedule requires schedule_spec.cron")
schedule_spec["cron"] = normalize_cron_expression(raw_cron)
if schedule_type == "interval":
every_seconds = validate_interval_seconds(schedule_spec, scheduler_config.min_once_delay_seconds)
try:
previous_seconds = parse_interval_seconds(dict(existing["schedule_spec"])) if existing["schedule_type"] == "interval" else None
except ValueError:
previous_seconds = None
# Re-saving an unchanged cadence keeps the next occurrence.
if previous_seconds == every_seconds and existing.get("next_run_at") is not None:
upcoming = existing["next_run_at"]
else:
upcoming = next_run_at(schedule_type, schedule_spec, timezone, now=now)
else:
upcoming = next_run_at(schedule_type, schedule_spec, timezone, now=now)
except (ValueError, OverflowError) as exc:
raise scheduler_error(422, "invalid_schedule", str(exc)) from exc
_check_once_time(schedule_type, _as_datetime(upcoming), scheduler_config=scheduler_config, now=now)
updates["schedule_spec"] = schedule_spec
updates["next_run_at"] = upcoming
# A terminal task whose schedule now has a future occurrence is re-armed:
# claim_due_tasks only admits "enabled" rows. The repository then checks
# its safety cap (limits_exhausted).
if upcoming is not None and existing["status"] in TERMINAL_TASK_STATUSES:
updates["status"] = "enabled"
async def validate_scheduled_task_update(
existing: dict[str, Any],
fields: dict[str, Any],
*,
user_id: str,
thread_store: Any,
scheduler_config: Any,
assistant_resolver: Callable[..., Awaitable[str]],
used: int,
now: datetime,
) -> dict[str, Any]:
"""Return repository ``updates`` for a PATCH or chat update.
``fields`` holds only what the request set. ``None`` clears
``goal_objective``, ``max_runs``, ``end_at`` and ``stop_condition``; for
other fields it means "unchanged", except ``assistant_id`` where it
restores the default agent.
"""
updates = {key: value for key, value in fields.items() if value is not None or key in CLEARABLE_FIELDS}
if "assistant_id" in fields:
updates["assistant_id"] = await assistant_resolver(fields["assistant_id"], user_id=user_id)
if "context_mode" in updates and updates["context_mode"] not in CONTEXT_MODES:
raise scheduler_error(422, "invalid_context_mode", "Unsupported context_mode")
if "schedule_type" in updates and updates["schedule_type"] not in SCHEDULE_TYPES:
raise scheduler_error(422, "invalid_schedule_type", "Unsupported schedule_type")
effective_context_mode = str(updates.get("context_mode", existing["context_mode"]))
effective_thread_id = updates.get("thread_id", existing.get("thread_id"))
if effective_context_mode == "reuse_thread":
if not effective_thread_id:
raise scheduler_error(422, "reuse_thread_requires_thread", "reuse_thread requires thread_id")
if not await thread_store.check_access(str(effective_thread_id), user_id, require_existing=True):
raise scheduler_error(404, "thread_not_found", "Thread not found")
elif effective_context_mode == "fresh_thread_per_run":
updates["thread_id"] = None
if "timezone" in updates:
try:
await asyncio.to_thread(validate_timezone, str(updates["timezone"]))
except ValueError as exc:
raise scheduler_error(422, "invalid_timezone", str(exc)) from exc
if {"schedule_spec", "timezone", "schedule_type"} & updates.keys():
await asyncio.to_thread(_recompute_schedule, existing, updates, scheduler_config=scheduler_config, now=now)
timezone = str(updates.get("timezone", existing["timezone"]))
if "stop_condition" in updates:
updates["stop_condition"] = _normalized_stop_condition(updates["stop_condition"])
if "end_at" in updates:
updates["end_at"] = await asyncio.to_thread(resolve_end_at, updates["end_at"], timezone)
validate_task_bounds(
goal_objective=updates.get("goal_objective", existing.get("goal_objective")),
max_runs=updates.get("max_runs", existing.get("max_runs")),
end_at=updates.get("end_at", existing.get("end_at")),
context_mode=effective_context_mode,
schedule_type=str(updates.get("schedule_type", existing["schedule_type"])),
schedule_spec=updates.get("schedule_spec", existing["schedule_spec"]),
next_run_at=updates.get("next_run_at", existing.get("next_run_at")),
now=now,
used=used,
require_limit_for_frequent=existing.get("origin_thread_id") is not None,
changed=(_BOUND_FIELDS | {"next_run_at"}) & updates.keys(),
)
return updates
def prepare_resume_updates(existing: dict[str, Any], renewal: dict[str, Any], *, used: int, now: datetime) -> dict[str, Any]:
"""Repository ``updates`` that resume a paused or finished task.
The next run is the stored one when still ahead, else recomputed from
``now`` (no catch-up run); a one-time task whose time passed cannot
resume. ``renewal`` may set or clear (``None``) ``max_runs`` / ``end_at``
in the same request; the repository rejects a reactivation whose cap is
still used up. The unmet streak is kept. An enabled task needs nothing.
"""
if existing.get("status") == "enabled":
return {}
renewal = {key: value for key, value in renewal.items() if key in {"max_runs", "end_at"}}
upcoming = _as_datetime(existing.get("next_run_at"))
if upcoming is None or upcoming <= now:
try:
upcoming = next_run_at(existing["schedule_type"], existing["schedule_spec"], existing["timezone"], now=now)
except (ValueError, OverflowError) as exc:
raise scheduler_error(422, "invalid_schedule", str(exc)) from exc
if upcoming is None:
raise scheduler_error(422, "once_time_passed", "This one-time task's time has passed; set a new run_at to run it again")
updates: dict[str, Any] = {"status": "enabled", "next_run_at": upcoming}
if is_host_pause_marker(existing.get("last_error")):
# A later manual pause must not read as "Paused by agent".
updates["last_error"] = None
if "end_at" in renewal:
renewal["end_at"] = resolve_end_at(renewal["end_at"], existing["timezone"])
updates.update(renewal)
validate_task_bounds(
goal_objective=existing.get("goal_objective"),
max_runs=updates.get("max_runs", existing.get("max_runs")),
end_at=updates.get("end_at", existing.get("end_at")),
context_mode=existing["context_mode"],
schedule_type=existing["schedule_type"],
schedule_spec=existing["schedule_spec"],
next_run_at=upcoming,
now=now,
used=used,
require_limit_for_frequent=existing.get("origin_thread_id") is not None,
changed={*renewal, "next_run_at"},
)
return updates