π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type { AgentOperationStatus, TaskItem, TaskRunTrigger } from '@lobechat/types';
import { IN_FLIGHT_OPERATION_STATUS_LIST } from '@lobechat/types';
import { and, eq, inArray, isNotNull, isNull, notInArray, sql } from 'drizzle-orm';
import type { AgentOperationError } from '../../schemas/agentOperations';
import { agentOperations } from '../../schemas/agentOperations';
import { tasks, taskTopics } from '../../schemas/task';
import type { LobeChatDatabase } from '../../type';
import { notTrashed } from '../../utils/softDelete';
/**
* Operation outcomes the orphaned-run reconciliation settles.
*
* An allow-list on purpose. `done` needs the success path's own inputs (the
* run's last assistant content, the creator bridge) and `interrupted` belongs
* to the interrupt path, so converging either from a sweep would mislabel the
* outcome; and an operation status added later is skipped rather than guessed
* at.
*/
const ORPHANED_RUN_OPERATION_STATUSES: AgentOperationStatus[] = ['abandoned', 'error'];
/** One `running` Task run whose operation has already ended β see the finder. */
export interface OrphanedRunningTopic {
completionReason: string | null;
/**
* The failure the operation recorded, when it recorded one β the raw
* `state.error`, so its message may be nested (`{ errorType, error: { message } }`).
*/
operationError: AgentOperationError | null;
operationId: string;
operationStatus: AgentOperationStatus;
taskId: string;
taskIdentifier: string;
topicId: string;
trigger: TaskRunTrigger | null;
userId: string;
workspaceId: string | null;
}
// Tasks eligible for cron-based dispatch.
// Excludes terminal/paused/running β `paused` requires user attention,
// `running` is already in flight (and `runTask` would CONFLICT anyway).
export const getScheduledTasks = async (db: LobeChatDatabase): Promise<TaskItem[]> => {
return db
.select()
.from(tasks)
.where(
and(
eq(tasks.automationMode, 'schedule'),
isNotNull(tasks.schedulePattern),
// A task in the recycle bin keeps its status; its schedule must not fire.
notTrashed(tasks.isDeleted),
notInArray(tasks.status, ['canceled', 'completed', 'failed', 'paused', 'running']),
),
);
};
/**
* Atomically move `context.scheduler.lastDispatchedOccurrenceAt` from
* `expected` to `next`. Returns false when another writer changed it first.
*
* The schedule dispatcher reserves a cron occurrence this way before
* publishing its execution, so a later tick inside the grace window (or an
* overlapping dispatcher run) cannot publish the same occurrence again while
* the first delivery is still queued.
*/
export const swapDispatchedScheduleOccurrence = async (
db: LobeChatDatabase,
taskId: string,
expected: string | null,
next: string | null,
): Promise<boolean> => {
const current = sql`coalesce(${tasks.context}, '{}'::jsonb)`;
const rows = await db
.update(tasks)
.set({
context: sql`${current} || jsonb_build_object('scheduler', coalesce(${current} -> 'scheduler', '{}'::jsonb) || jsonb_build_object('lastDispatchedOccurrenceAt', ${next}::text))`,
})
.where(
and(
eq(tasks.id, taskId),
sql`coalesce(${current} -> 'scheduler' ->> 'lastDispatchedOccurrenceAt', '') = ${expected ?? ''}`,
),
)
.returning({ id: tasks.id });
return rows.length > 0;
};
// Find stuck tasks (running but heartbeat timed out)
// Only checks tasks that have both lastHeartbeatAt and heartbeatTimeout set
export const findStuckTasks = async (db: LobeChatDatabase): Promise<TaskItem[]> => {
return db
.select()
.from(tasks)
.where(
and(
eq(tasks.status, 'running'),
notTrashed(tasks.isDeleted),
isNotNull(tasks.lastHeartbeatAt),
isNotNull(tasks.heartbeatTimeout),
sql`${tasks.lastHeartbeatAt} < now() - make_interval(secs => ${tasks.heartbeatTimeout})`,
),
);
};
/**
* Running Task runs whose operation has already ended.
*
* The terminal state of a run reaches `task_topics` / `tasks` through the
* run's `onComplete` webhook, and that delivery is fire-and-forget: when it is
* lost, the Task keeps a `running` run forever. Everything downstream then
* believes a dead run is live β the Goal view reads its frozen heartbeat as
* activity, and the coordinator parks on `waiting_external` instead of
* recovering it. This finder is what lets a sweep settle those
* rows without depending on the lost delivery.
*
* The grace window is what keeps the sweep from racing the normal path: the
* operation is settled before its hook is dispatched, so only a run that has
* been over for longer than any delivery lag is treated as orphaned.
*/
export const findOrphanedRunningTopics = async (
db: LobeChatDatabase,
options: { limit?: number; staleBefore: Date },
): Promise<OrphanedRunningTopic[]> => {
const { limit = 200, staleBefore } = options;
return (
db
.select({
completionReason: agentOperations.completionReason,
operationError: agentOperations.error,
operationId: agentOperations.id,
operationStatus: agentOperations.status,
taskId: tasks.id,
taskIdentifier: tasks.identifier,
// Non-null by the guard below; the column is only nullable because a
// legacy row could have been written without one.
topicId: sql<string>`${taskTopics.topicId}`,
trigger: taskTopics.trigger,
userId: tasks.createdByUserId,
workspaceId: tasks.workspaceId,
})
.from(taskTopics)
// The run row names the operation it belongs to; a row without one cannot
// be judged against an operation at all and is left alone.
.innerJoin(agentOperations, eq(taskTopics.operationId, agentOperations.id))
.innerJoin(tasks, eq(taskTopics.taskId, tasks.id))
.where(
and(
eq(taskTopics.status, 'running'),
// Only a Task that still believes it is running is repaired here.
// One that left `running` was settled deliberately β by the user, or
// by a cascade that already cancelled its run β and must not be
// dragged back into a failure.
eq(tasks.status, 'running'),
isNotNull(taskTopics.operationId),
// A run row with no topic cannot be driven through the lifecycle.
isNotNull(taskTopics.topicId),
// Sub-agent children extend their parent's turn and never own the
// Task's run row, so only a top-level operation may settle it.
isNull(agentOperations.parentOperationId),
inArray(agentOperations.status, ORPHANED_RUN_OPERATION_STATUSES),
sql`coalesce(${agentOperations.completedAt}, ${agentOperations.updatedAt}) < ${staleBefore}`,
),
)
.orderBy(sql`coalesce(${agentOperations.completedAt}, ${agentOperations.updatedAt})`)
.limit(limit)
);
};
/** One `running` Task run left behind by an interrupted operation β see the finder. */
export interface InterruptedRunningTopic {
operationId: string;
taskId: string;
taskIdentifier: string;
topicId: string;
userId: string;
workspaceId: string | null;
}
/**
* Running Task runs whose operation was interrupted and that nothing took over.
*
* An interrupt is not an outcome: the run's hook does not settle the row, on
* purpose, because the run that interrupted it β a follow-up the user sent in
* the same conversation β normally takes the row over and settles it when it
* ends. When nothing took it over (the follow-up could not, or the run was
* stopped some other way and nobody settled the row), the Task reads as
* `running` forever. {@link findOrphanedRunningTopics} deliberately leaves
* `interrupted` out, because converging it as a failure would mislabel it.
*
* A row is only returned when *no* operation on its topic is still in flight:
* a conversation that is still going is a Task that is still continuing, even
* when the row has not been handed to the run that is doing the work.
*/
export const findInterruptedRunningTopics = async (
db: LobeChatDatabase,
options: { limit?: number; staleBefore: Date },
): Promise<InterruptedRunningTopic[]> => {
const { limit = 200, staleBefore } = options;
return db
.select({
operationId: agentOperations.id,
taskId: tasks.id,
taskIdentifier: tasks.identifier,
topicId: sql<string>`${taskTopics.topicId}`,
userId: tasks.createdByUserId,
workspaceId: tasks.workspaceId,
})
.from(taskTopics)
.innerJoin(agentOperations, eq(taskTopics.operationId, agentOperations.id))
.innerJoin(tasks, eq(taskTopics.taskId, tasks.id))
.where(
and(
eq(taskTopics.status, 'running'),
eq(tasks.status, 'running'),
isNotNull(taskTopics.topicId),
isNull(agentOperations.parentOperationId),
eq(agentOperations.status, 'interrupted'),
sql`coalesce(${agentOperations.completedAt}, ${agentOperations.updatedAt}) < ${staleBefore}`,
sql`not exists (
select 1 from ${agentOperations} as live
where live.topic_id = ${taskTopics.topicId}
and live.status in (${sql.join(
IN_FLIGHT_OPERATION_STATUS_LIST.map((status) => sql`${status}`),
sql`, `,
)})
)`,
),
)
.orderBy(sql`coalesce(${agentOperations.completedAt}, ${agentOperations.updatedAt})`)
.limit(limit);
};