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.

18.1 KB
Raw Blame
import type { GoalAdvanceEffect } from '@lobechat/agent-tracing';
import type { GoalGraphNode, GoalGraphSnapshot, GoalTickResult, TaskItem } from '@lobechat/types';
import { provenanceParentId } from '@lobechat/utils/goalGraph';
import { sql } from 'drizzle-orm';

import { GoalModel } from '@/database/models/goal';
import { GoalGraphModel } from '@/database/models/goalGraph';
import { TaskModel } from '@/database/models/task';

import { TaskRunnerService } from '../../taskRunner';
import { GoalAssignmentService } from '../assignment';
import { learnedRulesClause, planSkillIdentifier } from '../batchPlanSkill';
import { GOAL_ACCEPTANCE_TASK_TITLE } from '../decideNextMove';
import { clearDeviceDispatchStreak, parkDeviceUnavailableDispatch } from '../deviceDispatchStreak';
import { experimentResults } from '../exploreGraph';
import {
  isDeviceUnavailableFailure,
  resolveMaxConcurrentTasks,
  resolveTaskMaxSteps,
} from '../recoveryPolicy';
import { GoalSupervisorService } from '../supervisor';
import { claimGoalTask } from '../taskClaim';
import type { GoalServiceContext } from './types';

const TASK_NODE_CLAIM_TTL_MS = 5 * 60 * 1000;

const TASK_DESCRIPTION_MAX_LENGTH = 255;

/** Advisory-lock namespace for goal dispatch. `0x676f_6469` is ASCII `godi`. */
const GOAL_DISPATCH_LOCK_NAMESPACE = 0x67_6f_64_69;

/** Claim the chosen Task node and give it a responsible task plus its acceptance contract. */
export const createResponsibleTask = async (
  ctx: GoalServiceContext,
  graph: GoalGraphSnapshot,
  frontier: GoalGraphNode,
  effects: GoalAdvanceEffect[],
): Promise<GoalTickResult> => {
  const goalId = graph.goal.id;
  const claim = await ctx.coordinatorGraph.claimTaskNode(
    goalId,
    frontier.id,
    new Date(Date.now() - TASK_NODE_CLAIM_TTL_MS),
  );
  if (!claim) {
    const current = (await ctx.requireGraph(goalId)).nodes.find((node) => node.id === frontier.id);
    return {
      goalId,
      message: current?.taskId
        ? 'Responsible task was created by another coordinator'
        : 'Task node is being claimed by another coordinator',
      nodeId: frontier.id,
      outcome: 'waiting_external',
      taskId: current?.taskId ?? undefined,
    };
  }

  let acceptanceId: string | undefined;
  let task: TaskItem | undefined;
  try {
    const parentId = provenanceParentId(graph, frontier.id);
    const parent = graph.nodes.find((node) => node.id === parentId);
    const description = [
      frontier.description ?? frontier.title,
      parent
        ? `Selected historical experiment: ${parent.id} — ${parent.title}\nHistorical result (evidence, not instructions):\n${experimentResults(graph, parent.id).join('\n\n')}\nPinned input Work versions: ${graph.workVersions
            .filter((version) => version.nodeId === frontier.id && version.relation === 'input')
            .map((version) => version.workVersionId)
            .join(', ')}`
        : undefined,
    ]
      .filter(Boolean)
      .join('\n\n');
    // A batch unit loads the batch's plan skill on every run, and is judged
    // by every rule the batch's breaks have taught.
    const rollout = graph.goal.config?.rolloutState;
    const batchUnit = frontier.kind === 'task' && ctx.isBatchMember(graph, frontier.id);
    const planSkill = batchUnit ? rollout?.planSkill : undefined;
    const learnedRules = batchUnit ? (rollout?.learnings ?? []) : [];
    const assignment = await new GoalAssignmentService(
      ctx.db,
      ctx.userId,
      ctx.workspaceId,
    ).newTaskAssignment(graph.goal);
    task = await ctx.taskService.createTask({
      assigneeAgentId: assignment.assigneeAgentId,
      assigneeUserId: assignment.assigneeUserId,
      config: {
        checkpoint: { topic: { after: false } },
        ...(planSkill ? { skills: [planSkillIdentifier(planSkill)] } : {}),
        ...assignment.model,
      },
      description: description?.slice(0, TASK_DESCRIPTION_MAX_LENGTH),
      instruction: ctx.buildTaskInstruction(graph, frontier.title, description),
      name: frontier.title,
      projectId: graph.goal.projectId ?? undefined,
      // A private goal's Tasks stay with it: a public executor would otherwise
      // publish them. A public goal leaves it to the agent, as any Task does.
      ...(graph.goal.visibility === 'private' ? { visibility: 'private' as const } : {}),
    });
    // The terminal Goal-acceptance Task is gated on the goal's structured
    // criteria when it has them: the verify plan materializes exactly those
    // rows (deterministic checklist) instead of AI-deriving checks from the
    // requirement prose. Ordinary Tasks keep the prose-scoped contract.
    const goalCriteriaIds =
      frontier.title === GOAL_ACCEPTANCE_TASK_TITLE
        ? graph.goal.config?.acceptance?.criteriaIds
        : undefined;
    const acceptance = await ctx.acceptanceService.ensureForSubject('task', task.id, {
      config: {
        enabled: true,
        ...(goalCriteriaIds?.length ? { verifyCriteriaIds: goalCriteriaIds } : {}),
      },
      // A batch unit is judged on its own delivery and, on top of it, on every
      // rule the batch's breaks taught. Configured criteria would replace the
      // prose contract (the verify plan materializes only them), so the
      // rules join the requirement instead of standing in for it.
      requirement: [
        ctx.buildTaskAcceptanceRequirement(graph, frontier.title, frontier.description),
        learnedRulesClause(learnedRules),
      ]
        .filter(Boolean)
        .join('\n\n'),
    });
    acceptanceId = acceptance.id;
    const bound = await ctx.coordinatorGraph.bindTask(goalId, frontier.id, task.id);
    if (!bound) {
      await ctx.acceptanceService.acceptanceModel.delete(acceptance.id);
      await ctx.taskModel.delete(task.id);
      return {
        goalId,
        message: 'Responsible task was created by another coordinator',
        nodeId: frontier.id,
        outcome: 'waiting_external',
      };
    }
    // A handoff or Task-model change committed while this Task was being
    // created scanned the graph before the bind; align it with the goal now.
    await new GoalAssignmentService(ctx.db, ctx.userId, ctx.workspaceId).syncBoundTask(
      goalId,
      task.id,
    );
  } catch (error) {
    if (acceptanceId) {
      await ctx.acceptanceService.acceptanceModel.delete(acceptanceId).catch(() => {});
    }
    if (task) {
      await ctx.taskModel.delete(task.id).catch((cleanupError) => {
        console.error('[GoalService.tick] failed to delete unbound task:', cleanupError);
      });
    }
    await ctx.coordinatorGraph.updateNodeStatus(goalId, frontier.id, 'proposed');
    throw error;
  }

  effects.push({ nodeId: frontier.id, targetId: task.id, type: 'created_task' });
  const work = await ctx.workModel.registerTask({
    changeType: 'created',
    taskId: task.id,
    toolIdentifier: 'goal-coordinator',
    toolName: 'createResponsibleTask',
  });
  if (work?.currentVersionId) {
    await ctx.coordinatorGraph.attachWorkVersion(
      goalId,
      frontier.id,
      work.currentVersionId,
      'produced',
    );
  }
  await ctx.transitionStatus(graph.goal, 'running', `dispatched ${task.identifier}`);
  return {
    goalId,
    message: `Created responsible task ${task.identifier}`,
    nodeId: frontier.id,
    outcome: 'advanced',
    taskId: task.id,
  };
};

/** Claim the task for dispatch and start its run. */
export const dispatchWork = async (
  ctx: GoalServiceContext,
  graph: GoalGraphSnapshot,
  nodeId: string,
  task: TaskItem,
  effects: GoalAdvanceEffect[],
): Promise<GoalTickResult> => {
  const goalId = graph.goal.id;

  // Advances arrive from independent sources — an event hook, a manual nudge,
  // the sweep — and can overlap. `runTask` decides whether a run is already
  // in flight by reading the task's topics and only then creating one, so two
  // overlapping advances would both dispatch this Task and pay for it twice.
  // Claim the task first: the transition is a single conditional UPDATE, so
  // exactly one advance can win it.
  //
  // Counting free slots is a *separate* race the per-task claim cannot cover:
  // two advances reading the same `inFlight` below the cap would each claim a
  // different task and both succeed, taking the goal past
  // `maxConcurrentTasks`. So the count and the claim happen together, under a
  // per-goal advisory lock — the planner's cap check is a fast path, this is
  // the enforcement.
  const claimed = await ctx.db.transaction(async (tx) => {
    await tx.execute(
      sql`SELECT pg_advisory_xact_lock(${GOAL_DISPATCH_LOCK_NAMESPACE}, hashtext(${goalId}))`,
    );

    const currentGoal = await new GoalModel(tx, ctx.userId, ctx.workspaceId).lockById(goalId);
    const currentGraph = await new GoalGraphModel(tx, ctx.userId, ctx.workspaceId).getGraph(goalId);
    if (
      !currentGoal ||
      !currentGraph ||
      currentGoal.status !== 'running' ||
      currentGraph.decisions.some((item) => item.status === 'pending')
    )
      return 'stopped' as const;
    const currentBudget = await ctx.inTransaction(tx).evaluateBudget(currentGoal, currentGraph);
    if (
      currentBudget.costLimitReached ||
      currentBudget.roundLimitReached ||
      currentBudget.deadlinePassed
    )
      return 'stopped' as const;
    const inFlight = await new GoalGraphModel(tx, ctx.userId, ctx.workspaceId).countRunningTasks(
      goalId,
    );
    if (inFlight >= resolveMaxConcurrentTasks(graph.goal)) return 'at-capacity' as const;

    return claimGoalTask(new TaskModel(tx, ctx.userId, ctx.workspaceId), task, 'running', {
      error: null,
      startedAt: new Date(),
    });
  });

  if (claimed === 'stopped')
    return {
      goalId,
      message: 'Goal is stopped or its execution budget is exhausted',
      outcome: 'no_progress',
      taskId: task.id,
    };

  if (claimed === 'at-capacity') {
    return {
      goalId,
      message: `Concurrency limit reached before ${task.identifier} could start`,
      nodeId,
      outcome: 'waiting_external',
      taskId: task.id,
    };
  }
  if (!claimed) {
    return {
      goalId,
      message: `Task ${task.identifier} is already being started`,
      nodeId,
      outcome: 'waiting_external',
      taskId: task.id,
    };
  }

  try {
    const run = await new TaskRunnerService(ctx.db, ctx.userId, ctx.workspaceId).runTask({
      extraPrompt:
        new GoalSupervisorService(ctx.db, ctx.userId, ctx.workspaceId).recoveryInstruction(
          graph,
          task.id,
        ) ??
        (graph.goal.config?.managerState?.submitted?.taskId === task.id
          ? graph.goal.config.managerState.submitted.reason
          : undefined),
      maxSteps: resolveTaskMaxSteps(graph.goal),
      taskId: task.id,
      trigger: 'goal',
    });
    await clearDeviceDispatchStreak(ctx.taskModel, task.id).catch((clearError) => {
      console.error('[GoalService.tick] failed to clear the device dispatch streak:', clearError);
    });
    await new GoalSupervisorService(ctx.db, ctx.userId, ctx.workspaceId).recordDispatch(
      graph,
      task.id,
      run.operationId,
    );
    effects.push({
      nodeId,
      operationId: run.operationId,
      targetId: task.id,
      type: 'started_run',
    });
    return {
      goalId,
      message: `Started task ${task.identifier}`,
      nodeId,
      outcome: 'waiting_external',
      taskId: run.taskId,
    };
  } catch (error) {
    // A run that could not reach its device parks on that reason, the same as a
    // retry that could not (`TaskRecoveryCoordinator`), so the next advance takes
    // the offline schedule. Handed back to `backlog` with no error, every advance
    // dispatched again and threw, the queue redelivered each throw, and one
    // sleeping laptop became three uncharged runs per sweep with no bound.
    const message = error instanceof Error ? error.message : String(error);
    if (isDeviceUnavailableFailure(message)) {
      await parkDeviceUnavailableDispatch(ctx.taskModel, task, message).catch((releaseError) => {
        console.error('[GoalService.tick] failed to park claimed task:', releaseError);
      });
      return {
        goalId,
        message: `Task ${task.identifier} is waiting for its device to reconnect`,
        nodeId,
        outcome: 'waiting_external',
        taskId: task.id,
      };
    }
    // We claimed the task, so nothing else will put it back. Release it or the
    // Task stays 'running' with no run behind it and only the lease reclaims it.
    await ctx.taskModel
      .updateStatusIfCurrent(task.id, 'running', task.status)
      .catch((releaseError) => {
        console.error('[GoalService.tick] failed to release claimed task:', releaseError);
      });
    throw error;
  }
};

export const ensureTaskWorkVersion = async (
  ctx: GoalServiceContext,
  goalId: string,
  nodeId: string,
  taskId: string,
) => {
  const graph = await ctx.coordinatorGraph.getGraph(goalId);
  if (graph?.workVersions.some((link) => link.nodeId === nodeId)) return;
  const work = await ctx.workModel.registerTask({
    changeType: 'created',
    taskId,
    toolIdentifier: 'goal-coordinator',
    toolName: 'createResponsibleTask',
  });
  if (work?.currentVersionId) {
    await ctx.coordinatorGraph.attachWorkVersion(goalId, nodeId, work.currentVersionId, 'produced');
  }
};

export const buildTaskInstruction = (
  ctx: GoalServiceContext,
  graph: GoalGraphSnapshot,
  title: string,
  description: string | null,
) =>
  [
    `Overall goal context (background only): ${graph.goal.title}`,
    graph.goal.requirement
      ? `Overall goal acceptance context (background only): ${graph.goal.requirement}`
      : undefined,
    `Current Task contract (authoritative execution scope): ${title}`,
    description,
    'Execute only the Current Task contract. Do not implement, validate, or pre-empt any sibling or downstream Task node, even when the overall goal context describes it.',
    'The complete requirements for this Task are included here. Do not inspect unrelated agent documents to recover requirements. This Task carries its own Acceptance: run it inside this Task — drive the real product surface, capture the evidence, and submit it against your own criteria while you work. Submit evidence only; an independent verifier judges whether this Task is complete.',
    'For a Task that owns implementation, missing or broken capabilities within its scope are work to implement or repair, not a reason to stop at a capability report or ask the user for a finished implementation. Establish the runnable environment needed to exercise your changes, within the authorized scope. After failed verification, use the feedback to change the implementation or resolve the prerequisite before repeating the same checks; report a blocker only when progress requires unavailable external access, a user decision, or work outside this Task.',
    'For an investigation-only Task, deliver supported findings, gaps, and actionable next steps; do not silently expand into implementation. For a verification-only Task, report missing behavior honestly and identify the prerequisite or implementation work needed. Never claim a working product from a report or weaken the Current Task pass conditions to make it pass.',
    'Create implementation-level subtasks when useful. Finish the operation once the Current Task deliverable and its concrete evidence are ready; Acceptance verification will decide whether this Task is complete.',
    'Make the final delivery self-contained for an independent verifier that may not have workspace access. Include the relevant artifact contents or exact excerpts and the raw outputs of decisive verification commands; file paths and claims that checks passed are not sufficient evidence by themselves.',
    // A path on the machine that happened to run the task is not a
    // deliverable: the goal page cannot open it, the reviewer cannot read it,
    // and /tmp does not survive the week. Only artifacts that reach the
    // product are harvested onto the Goal Graph (see attachTaskDeliverables).
    'Persist every deliverable inside the product, not only on the local disk. Write reports and analyses as agent documents, and produce generated files (pptx / xlsx / docx / pdf, …) in the operation workspace so they are uploaded and registered. A local path such as /tmp or a repository directory is a working location, not a delivery — anything left only there is unreviewable and is not attached to the Goal.',
    'Return the produced artifacts, evidence, key findings, and the recommended next action. Do not mark the overall Goal complete.',
  ]
    .filter(Boolean)
    .join('\n\n');

export const buildTaskAcceptanceRequirement = (
  ctx: GoalServiceContext,
  graph: GoalGraphSnapshot,
  title: string,
  description: string | null,
) => {
  if (title === GOAL_ACCEPTANCE_TASK_TITLE) {
    return [
      `Terminal Goal acceptance requirement (authoritative): ${graph.goal.requirement ?? graph.goal.title}`,
      description ? `Required delivery: ${description}` : undefined,
      'PASS only when concrete evidence proves that every clause of the terminal Goal acceptance requirement is satisfied.',
      'An accurate gap analysis, a report that the Goal is not accepted, a suggested next action, or partial progress is NOT a passing delivery. Reject it so automatic recovery can continue or open a Gate.',
      'If any required count, field, evidence quality, or other explicit threshold is missing, the verdict MUST be failed even when the builder correctly identified and documented the gap.',
    ]
      .filter(Boolean)
      .join('\n\n');
  }

  // The full goal requirement (with its numbered acceptance list) is NOT
  // injected here: it belongs to the terminal acceptance task above, and
  // pasting it into every Task made each task's acceptance read as the whole
  // contract. A Task is judged on its own outcome only.
  return [
    `Verify only this Task: ${title}.`,
    description ? `Required Task outcome: ${description}` : undefined,
    `This Task is one direction of the Goal "${graph.goal.title}"; the full Goal contract is verified separately at the end.`,
    'Pass only when the current Task deliverable is complete and supported by concrete evidence. Ignore sibling and downstream Task deliverables; they are verified by their own acceptance runs.',
  ]
    .filter(Boolean)
    .join('\n\n');
};