Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

6.0 KB
Raw Blame
import { isCronOccurrence } from '@lobechat/utils/cronEval';
import { TRPCError } from '@trpc/server';
import debug from 'debug';
import { and, eq } from 'drizzle-orm';

import { BriefModel } from '@/database/models/brief';
import { TaskModel } from '@/database/models/task';
import { TaskTopicModel } from '@/database/models/taskTopic';
import { tasks } from '@/database/schemas';
import { getServerDB } from '@/database/server';
import { notTrashed } from '@/database/utils/softDelete';

import { TaskRunnerService } from './index';

const log = debug('task-runner:schedule-tick');

const TERMINAL_STATUSES = new Set(['canceled', 'completed', 'failed']);
const isTerminal = (status: string) => TERMINAL_STATUSES.has(status);

export type ScheduleTickOutcome =
  { ran: true; taskIdentifier: string } | { ran: false; reason: ScheduleTickSkipReason };

export type ScheduleTickSkipReason =
  | 'archived'
  | 'human-waiting'
  | 'in-flight'
  | 'max-executions-reached'
  | 'mode-changed'
  | 'no-pattern'
  | 'not-found'
  | 'paused'
  | 'schedule-changed'
  | 'terminal';

/**
 * Run a schedule tick β€” invoked by the QStash `/schedule-execute` HTTP handler
 * after the central `/schedule-dispatch` decided this task is due.
 *
 * DB is the authority: re-checks task state because the dispatch message may
 * arrive after the user paused, canceled, or changed the automation mode.
 */
export async function runScheduleTick(
  taskId: string,
  userId: string,
  options: { occurrence?: string } = {},
): Promise<ScheduleTickOutcome> {
  const db = await getServerDB();

  // System-level dispatch: we don't have the workspace context here. Read the
  // task row directly (creator-scoped, workspace-agnostic) to learn
  // `task.workspaceId`, then use it to instantiate downstream models so brief
  // writes / lifecycle hits land in the right workspace.
  const [task] = await db
    .select()
    .from(tasks)
    .where(
      and(eq(tasks.id, taskId), eq(tasks.createdByUserId, userId), notTrashed(tasks.isDeleted)),
    )
    .limit(1);
  if (!task) {
    log('skip task=%s reason=not-found', taskId);
    return { ran: false, reason: 'not-found' };
  }
  // Archived tasks are hidden from every list; never run them out of sight.
  if (task.archivedAt) {
    log('skip task=%s reason=archived', taskId);
    return { ran: false, reason: 'archived' };
  }
  const wsId = task.workspaceId ?? undefined;
  if (task.automationMode !== 'schedule') {
    log('skip task=%s reason=mode-changed (mode=%s)', taskId, task.automationMode);
    return { ran: false, reason: 'mode-changed' };
  }
  if (!task.schedulePattern) {
    log('skip task=%s reason=no-pattern', taskId);
    return { ran: false, reason: 'no-pattern' };
  }
  // A delayed delivery was reserved before it fires; the user may have moved the
  // schedule since, and the new time is dispatched on its own.
  if (
    options.occurrence &&
    !isCronOccurrence(task.schedulePattern, task.scheduleTimezone, new Date(options.occurrence))
  ) {
    log('skip task=%s reason=schedule-changed (occurrence=%s)', taskId, options.occurrence);
    return { ran: false, reason: 'schedule-changed' };
  }
  if (isTerminal(task.status)) {
    log('skip task=%s reason=terminal (status=%s)', taskId, task.status);
    return { ran: false, reason: 'terminal' };
  }
  if (task.status === 'paused') {
    log('skip task=%s reason=paused', taskId);
    return { ran: false, reason: 'paused' };
  }

  const briefModel = new BriefModel(db, userId, wsId);
  if (await briefModel.hasUnresolvedUrgentByTask(taskId, { excludeTypes: ['error'] })) {
    log('skip task=%s reason=human-waiting', taskId);
    return { ran: false, reason: 'human-waiting' };
  }

  // Defense-in-depth cap check.
  //
  // `TaskLifecycleService.onTopicComplete` is the primary stopper β€” it parks
  // a schedule-mode task at `completed` (instead of `scheduled`) the instant
  // it consumes its final allowed run, so the UI reflects the cap immediately
  // even on low-frequency crons (e.g. daily, maxExecutions=1).
  //
  // This pre-tick recheck catches the residual edge cases the lifecycle
  // hook can miss: a crashed tick that never reached onTopicComplete; a
  // user editing maxExecutions downward after the cap is already exceeded;
  // or stale `scheduled` rows from older code paths.
  const scheduleConfig =
    ((task.config as { schedule?: { maxExecutions?: number | null } } | null) ?? {}).schedule ?? {};
  const maxExecutions = scheduleConfig.maxExecutions ?? null;
  if (maxExecutions != null && maxExecutions > 0) {
    const scheduler =
      ((task.context as { scheduler?: { scheduleStartedAt?: string } } | null) ?? {}).scheduler ??
      {};
    const startedAtIso = scheduler.scheduleStartedAt;
    if (startedAtIso) {
      const startedAt = new Date(startedAtIso);
      const topicModel = new TaskTopicModel(db, userId, wsId);
      // Only automation ticks count against the quota β€” ad-hoc manual runs must
      // not consume scheduled executions.
      const runCount = await topicModel.countByTask(taskId, {
        since: startedAt,
        triggers: ['schedule'],
      });
      if (runCount >= maxExecutions) {
        log(
          'skip task=%s reason=max-executions-reached (%d/%d) β€” marking completed',
          taskId,
          runCount,
          maxExecutions,
        );
        const taskModel = new TaskModel(db, userId, wsId);
        await taskModel.updateStatus(taskId, 'completed', { completedAt: new Date() });
        return { ran: false, reason: 'max-executions-reached' };
      }
    }
  }

  const runner = new TaskRunnerService(db, userId, wsId);
  try {
    await runner.runTask({ taskId, trigger: 'schedule' });
  } catch (e) {
    // Concurrent tick / manual run already running this task β€” graceful skip.
    if (e instanceof TRPCError && e.code === 'CONFLICT') {
      log('skip task=%s reason=in-flight', taskId);
      return { ran: false, reason: 'in-flight' };
    }
    throw e;
  }
  log('ran task=%s identifier=%s', taskId, task.identifier);
  return { ran: true, taskIdentifier: task.identifier };
}