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.

15.6 KB
Raw Blame
import type { TaskItem, TaskProjectMoveRefusal } from '@lobechat/types';
import { and, eq, inArray, isNotNull, ne, notInArray, or, sql } from 'drizzle-orm';
import { alias } from 'drizzle-orm/pg-core';

import {
  taskActivities,
  taskComments,
  taskDependencies,
  taskDocuments,
  tasks,
  taskTopics,
} from '../../schemas/task';
import type { LobeChatDatabase } from '../../type';
import { notTrashed } from '../../utils/softDelete';
import {
  activitiesOwnership,
  commentsOwnership,
  depsOwnership,
  docsOwnership,
  seqOwnership,
  type TaskModelContext,
  taskOwnership,
  taskOwnershipSql,
  topicsOwnership,
} from './context';

export const findSubtasks = async (
  ctx: TaskModelContext,
  parentTaskId: string,
): Promise<TaskItem[]> => {
  return ctx.db
    .select()
    .from(tasks)
    .where(and(eq(tasks.parentTaskId, parentTaskId), taskOwnership(ctx)))
    .orderBy(tasks.sortOrder, tasks.seq);
};

/**
 * Fetch all descendants of a root task using Drizzle select() (returns camelCase fields).
 * Uses breadth-first traversal with O(depth) queries.
 */
export const findAllDescendants = async (
  ctx: TaskModelContext,
  rootTaskId: string,
): Promise<TaskItem[]> => {
  const all: TaskItem[] = [];
  let parentIds = [rootTaskId];

  while (parentIds.length > 0) {
    const children = await ctx.db
      .select()
      .from(tasks)
      .where(and(inArray(tasks.parentTaskId, parentIds), taskOwnership(ctx)))
      .orderBy(tasks.sortOrder, tasks.seq);

    if (children.length === 0) break;

    all.push(...children);
    parentIds = children.map((c) => c.id);
  }

  return all;
};

/**
 * Whether a direct subtask sits outside `projectId` (`null` = no project). Scoped
 * like the identifier sequence rather than by `taskOwnership`: another member's
 * private subtask is hidden from the caller but still has to share its parent's
 * project. A trashed subtask no longer counts, like a trashed dependency.
 */
export const hasSubtaskOutsideProject = async (
  ctx: TaskModelContext,
  taskId: string,
  projectId: string | null,
): Promise<boolean> => {
  const [row] = await ctx.db
    .select({ id: tasks.id })
    .from(tasks)
    .where(
      and(
        eq(tasks.parentTaskId, taskId),
        seqOwnership(ctx),
        notTrashed(tasks.isDeleted),
        projectId === null
          ? isNotNull(tasks.projectId)
          : sql`${tasks.projectId} IS DISTINCT FROM ${projectId}`,
      ),
    )
    .limit(1);
  return !!row;
};

// Recursive query to get full task tree
export const getTaskTree = async (
  ctx: TaskModelContext,
  rootTaskId: string,
): Promise<TaskItem[]> => {
  const ownership = taskOwnershipSql(ctx);
  const result = await ctx.db.execute(sql`
    WITH RECURSIVE task_tree AS (
      SELECT * FROM tasks WHERE id = ${rootTaskId} AND ${ownership}
      UNION ALL
      SELECT t.* FROM tasks t
      JOIN task_tree tt ON t.parent_task_id = tt.id
    )
    SELECT * FROM task_tree
  `);

  return result.rows as unknown as TaskItem[];
};

/**
 * For a list of task IDs, find all agent IDs (assignee + creator) across their full task trees.
 * Walks UP to find root, then DOWN to collect all agents.
 * Returns { [inputTaskId]: agentId[] }
 */
export const getTreeAgentIdsForTaskIds = async (
  ctx: TaskModelContext,
  taskIds: string[],
): Promise<Record<string, string[]>> => {
  if (taskIds.length === 0) return {};

  const taskIdParams = taskIds.map((id) => sql`${id}`);
  const taskIdList = sql.join(taskIdParams, sql`, `);

  const ownershipBare = taskOwnershipSql(ctx);
  const ownershipAliased = taskOwnershipSql(ctx, 't');
  const result = await ctx.db.execute(sql`
    WITH RECURSIVE
    ancestors AS (
      SELECT id AS origin_id, id, parent_task_id
      FROM tasks
      WHERE id IN (${taskIdList})
        AND ${ownershipBare}
      UNION ALL
      SELECT a.origin_id, t.id, t.parent_task_id
      FROM tasks t
      JOIN ancestors a ON t.id = a.parent_task_id
      WHERE ${ownershipAliased}
    ),
    roots AS (
      SELECT DISTINCT ON (origin_id) origin_id, id AS root_id
      FROM ancestors
      WHERE parent_task_id IS NULL
    ),
    descendants AS (
      SELECT r.origin_id, t.id, t.assignee_agent_id, t.created_by_agent_id
      FROM tasks t
      JOIN roots r ON t.id = r.root_id
      WHERE ${ownershipAliased}
      UNION ALL
      SELECT d.origin_id, t.id, t.assignee_agent_id, t.created_by_agent_id
      FROM tasks t
      JOIN descendants d ON t.parent_task_id = d.id
      WHERE ${ownershipAliased}
    )
    SELECT origin_id, assignee_agent_id, created_by_agent_id
    FROM descendants
    WHERE assignee_agent_id IS NOT NULL OR created_by_agent_id IS NOT NULL
  `);

  const map: Record<string, Set<string>> = {};
  for (const row of result.rows as any[]) {
    const originId = row.origin_id as string;
    if (!map[originId]) map[originId] = new Set();
    if (row.assignee_agent_id) map[originId].add(row.assignee_agent_id as string);
    if (row.created_by_agent_id) map[originId].add(row.created_by_agent_id as string);
  }

  return Object.fromEntries(Object.entries(map).map(([k, v]) => [k, Array.from(v)]));
};

/**
 * Collect a task and all its descendants (parentTaskId-linked) via BFS.
 * Honors the current ownership scope.
 */
export const collectTaskSubtree = async (
  ctx: TaskModelContext,
  rootId: string,
  runner: LobeChatDatabase,
): Promise<TaskItem[]> => {
  const [root] = await runner
    .select()
    .from(tasks)
    .where(and(eq(tasks.id, rootId), taskOwnership(ctx)))
    .limit(1);
  if (!root) return [];

  const collected: TaskItem[] = [root];
  let frontier: string[] = [root.id];

  while (frontier.length > 0) {
    const children = await runner
      .select()
      .from(tasks)
      .where(and(inArray(tasks.parentTaskId, frontier), taskOwnership(ctx)));
    if (children.length === 0) break;
    collected.push(...children);
    frontier = children.map((c) => c.id);
  }

  return collected;
};

/**
 * Write one visibility onto a task subtree and the task-owned child rows that
 * mirror it, in one transaction. Only the task aggregate is written here; the
 * cross-aggregate flow (Work visibility mirror) lives in
 * `TaskSubtreeRepository.updateVisibility`, which runs this inside its own
 * transaction. `TaskSubtreeRepository.updateVisibility` documents the
 * cascade rules.
 */
export const setSubtreeVisibility = async (
  ctx: TaskModelContext,
  rootId: string,
  descendantIds: string[],
  visibility: 'private' | 'public',
): Promise<TaskItem | null> => {
  const taskIds = [rootId, ...descendantIds];
  const stamp = new Date();

  return ctx.db.transaction(async (tx) => {
    // A private task is creator-only, so a member assignee would keep a task
    // they can no longer open. Unassign them before the root is read back
    // below; the creator's own assignment and the agent assignee stay.
    if (visibility === 'private') {
      await tx
        .update(tasks)
        .set({ assigneeUserId: null })
        .where(
          and(
            inArray(tasks.id, taskIds),
            taskOwnership(ctx),
            isNotNull(tasks.assigneeUserId),
            ne(tasks.assigneeUserId, tasks.createdByUserId),
          ),
        );
    }

    // Update the root with RETURNING so we read the post-update row in the
    // same statement. A second SELECT filtered by `ownership()` would self-
    // cancel when a workspace owner demotes another member's task to private:
    // the UPDATE filter sees the OLD (public) row and writes, but the read-
    // back filter sees the NEW (private + other-creator) row and returns 0
    // rows, so the caller would observe a NOT_FOUND error even though the
    // write succeeded.
    const [updated] = await tx
      .update(tasks)
      .set({ updatedAt: stamp, visibility })
      .where(and(eq(tasks.id, rootId), taskOwnership(ctx)))
      .returning();

    if (descendantIds.length > 0) {
      await tx
        .update(tasks)
        .set({ updatedAt: stamp, visibility })
        .where(and(inArray(tasks.id, descendantIds), taskOwnership(ctx)));
    }

    await tx
      .update(taskDependencies)
      .set({ visibility })
      .where(and(inArray(taskDependencies.taskId, taskIds), depsOwnership(ctx)));

    await tx
      .update(taskDocuments)
      .set({ visibility })
      .where(and(inArray(taskDocuments.taskId, taskIds), docsOwnership(ctx)));

    // Demotion-only cascade for the event-shaped child rows (see the
    // `TaskSubtreeRepository.updateVisibility` docstring): their visibility
    // mirrors the task, so pulling the task back to private must also pull
    // public-era topics/comments out of workspace scope.
    if (visibility === 'private') {
      await tx
        .update(taskTopics)
        .set({ visibility })
        .where(and(inArray(taskTopics.taskId, taskIds), topicsOwnership(ctx)));

      await tx
        .update(taskComments)
        .set({ visibility })
        .where(and(inArray(taskComments.taskId, taskIds), commentsOwnership(ctx)));

      await tx
        .update(taskActivities)
        .set({ visibility })
        .where(and(inArray(taskActivities.taskId, taskIds), activitiesOwnership(ctx)));
    }

    return updated ?? null;
  });
};

/**
 * Row-lock every task of a subtree for the enclosing transaction, whatever its
 * visibility, and return their statuses. A takeover authorizes the root it can
 * see, then must act on the whole subtree: private subtasks the caller cannot
 * see and runs starting concurrently both have to be caught under the lock.
 */
export const lockWholeSubtree = async (
  ctx: TaskModelContext,
  rootId: string,
): Promise<{ id: string; status: string }[]> => {
  if (!ctx.workspaceId) return [];
  const result = await ctx.db.execute(sql`
    WITH RECURSIVE task_tree AS (
      SELECT id FROM tasks WHERE id = ${rootId} AND workspace_id = ${ctx.workspaceId}
      UNION ALL
      SELECT t.id FROM tasks t JOIN task_tree tt ON t.parent_task_id = tt.id
    )
    SELECT id, status FROM tasks
    WHERE id IN (SELECT id FROM task_tree) AND is_deleted IS NOT TRUE
    FOR UPDATE
  `);
  return result.rows as { id: string; status: string }[];
};

/**
 * Make the caller the creator of a task subtree, for a member taking another
 * member's public task private as their own. Only the `tasks` rows change: the
 * child rows keep their authors and stay readable through the creator branch
 * of `childOwnership`.
 *
 * Automation stops: runs are attributed to the creator, so the previous
 * creator's queued schedule/heartbeat ticks no longer match the row, and an
 * armed `scheduled` task would otherwise look live with nothing driving it.
 * It returns to `backlog` (`paused` reads as "awaiting review" in the UI); the
 * new creator re-arms it if they want it.
 */
export const takeOverSubtree = async (ctx: TaskModelContext, taskIds: string[]): Promise<void> => {
  if (taskIds.length === 0) return;
  await ctx.db
    .update(tasks)
    .set({
      automationMode: null,
      createdByUserId: ctx.userId,
      status: sql`CASE WHEN ${tasks.status} = 'scheduled' THEN 'backlog' ELSE ${tasks.status} END`,
      updatedAt: new Date(),
    })
    .where(and(inArray(tasks.id, taskIds), taskOwnership(ctx)));
};

/**
 * Delete the given tasks (owned child rows cascade through their FKs).
 * Acceptances belong to the verify aggregate; `TaskSubtreeRepository`
 * removes them in the same transaction.
 */
export const deleteTasksByIds = async (
  ctx: TaskModelContext,
  taskIds: string[],
): Promise<number> => {
  if (taskIds.length === 0) return 0;

  const result = await ctx.db
    .delete(tasks)
    .where(and(inArray(tasks.id, taskIds), taskOwnership(ctx)))
    .returning({ id: tasks.id });

  return result.length;
};

/** Why `moveUnfiledTreeToProject` refused to move a task tree. */
export class TaskTreeProjectMoveError extends Error {
  constructor(readonly reason: TaskProjectMoveRefusal) {
    super(`Task tree cannot move to the project: ${reason}`);
    this.name = 'TaskTreeProjectMoveError';
  }
}

/**
 * File a root task that has no project, together with every descendant, under
 * `projectId` in one transaction.
 *
 * A subtask must share its parent's project and a dependency must stay inside
 * one project, while a plain project patch moves only the one row. So a root
 * with subtasks could never enter a project: moving the root splits it from its
 * subtasks, and moving a subtask splits it from its root. Moving the whole tree
 * at once keeps both rules.
 *
 * Only an unfiled tree moves: every task in it must have no project, so this
 * never pulls a task out of a project it was deliberately filed under. The
 * subtree is collected visibility-blind (`seqOwnership`) and without trashed
 * rows, like `hasSubtaskOutsideProject`: another member's private subtask is
 * hidden from the caller but must follow its parent, while a trashed one no
 * longer counts for either rule. Every row is locked before the checks
 * so a concurrent reparent or dependency cannot slip in between check and write.
 *
 * Returns the updated root, or `null` when the caller cannot see the root.
 */
export const moveUnfiledTreeToProject = async (
  ctx: TaskModelContext,
  rootId: string,
  projectId: string,
): Promise<TaskItem | null> =>
  ctx.db.transaction(async (tx) => {
    const [root] = await tx
      .select({ id: tasks.id, parentTaskId: tasks.parentTaskId, projectId: tasks.projectId })
      .from(tasks)
      .where(and(eq(tasks.id, rootId), taskOwnership(ctx)))
      .for('update')
      .limit(1);
    if (!root) return null;
    if (root.parentTaskId) throw new TaskTreeProjectMoveError('has_parent');
    if (root.projectId !== null) throw new TaskTreeProjectMoveError('filed_task');

    const treeIds = [root.id];
    // Guards against a corrupt parent cycle, which would otherwise loop forever.
    const seen = new Set(treeIds);
    let frontier = [root.id];
    while (frontier.length > 0) {
      const children = await tx
        .select({ id: tasks.id, projectId: tasks.projectId })
        .from(tasks)
        .where(
          and(
            inArray(tasks.parentTaskId, frontier),
            seqOwnership(ctx),
            notTrashed(tasks.isDeleted),
          ),
        )
        .for('update');
      if (children.some((child) => child.projectId !== null)) {
        throw new TaskTreeProjectMoveError('filed_subtask');
      }
      frontier = children.map((child) => child.id).filter((id) => !seen.has(id));
      for (const id of frontier) seen.add(id);
      treeIds.push(...frontier);
    }

    // A dependency inside the tree moves with it; one reaching a live task
    // outside the tree would end up crossing into another project.
    const outside = alias(tasks, 'outside_task');
    const [externalLink] = await tx
      .select({ id: taskDependencies.id })
      .from(taskDependencies)
      .innerJoin(
        outside,
        or(
          and(
            inArray(taskDependencies.taskId, treeIds),
            eq(outside.id, taskDependencies.dependsOnId),
          ),
          and(
            inArray(taskDependencies.dependsOnId, treeIds),
            eq(outside.id, taskDependencies.taskId),
          ),
        ),
      )
      .where(
        and(
          notInArray(outside.id, treeIds),
          notTrashed(outside.isDeleted),
          sql`${outside.projectId} IS DISTINCT FROM ${projectId}`,
        ),
      )
      .limit(1);
    if (externalLink) throw new TaskTreeProjectMoveError('external_dependency');

    const stamp = new Date();
    const [updated] = await tx
      .update(tasks)
      .set({ projectId, updatedAt: stamp })
      .where(and(eq(tasks.id, root.id), taskOwnership(ctx)))
      .returning();
    if (treeIds.length > 1) {
      await tx
        .update(tasks)
        .set({ projectId, updatedAt: stamp })
        .where(and(inArray(tasks.id, treeIds.slice(1)), seqOwnership(ctx)));
    }
    return updated ?? null;
  });