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.

4.9 KB
Raw Blame
import type { GoalAdvanceTrigger } from '@lobechat/agent-tracing';
import type { GoalTickOutcome, GoalTickResult } from '@lobechat/types';
import debug from 'debug';

import { getServerDB } from '@/database/server';

import { GoalAdvanceRecorder, TERMINAL_GOAL_STATUSES } from './goalTraceRecorder';
import { GoalService } from './index';
import type { GoalTickOptions } from './traceObservation';

const log = debug('lobe-server:goal-advance');

/**
 * Outcomes the coordinator cannot get past by ticking again.
 *
 * `waiting_external` is one of them here, and that is the whole point of the
 * event-driven design: it means a graph Task is now executing, so the next move
 * belongs to that task's completion, not to a poll loop holding a worker open.
 * The one exception is a tick that just started a run itself: another slot may
 * still be free, so the advance ticks again (see `startedRun`).
 */
const STOP_OUTCOMES = new Set<GoalTickOutcome>([
  'achieved',
  'failed',
  'no_progress',
  'waiting_external',
  'waiting_human',
]);

/**
 * Safety limit for one advance. Dispatching a Task takes two ticks and an
 * advance fills every free concurrency slot, so a healthy run is a
 * handful; anything near this is a loop that is not converging, and the sweep
 * will come back to it.
 */
export const MAX_TICKS_PER_ADVANCE = 20;

export interface AdvanceGoalParams {
  goalId: string;
  /** What caused this advance. Recorded on the trajectory so a run can be sliced by source. */
  trigger?: GoalAdvanceTrigger;
  userId: string;
  workspaceId?: string;
}

export interface AdvanceGoalOutcome {
  goalId: string;
  result: GoalTickResult;
  ticks: number;
}

/**
 * Advance a goal until it is waiting on something that is not another tick.
 *
 * This is the server-side driver: a goal moves because something happened to
 * it, not because a client is holding a loop open. Runs as the goal's own
 * owner, so it sees exactly what that user's coordinator would see.
 */
export const advanceGoal = async ({
  goalId,
  trigger = 'unknown',
  userId,
  workspaceId,
}: AdvanceGoalParams): Promise<AdvanceGoalOutcome> => {
  const db = await getServerDB();
  const service = new GoalService(db, userId, workspaceId);
  const recorder = new GoalAdvanceRecorder(db, goalId, trigger);

  // A tick that starts a run reports `waiting_external` β€” that Task is now
  // executing β€” but the other free concurrency slots are not. Stopping there
  // started one Task per advance: a released wave of five ran one unit and left
  // the rest in backlog until the stalled-goal sweep (15 minutes) came back.
  // So a tick that started a run is followed by another, until a tick starts
  // nothing (the cap is reached, or nothing else is ready).
  let startedRun: boolean;
  const record = recorder.onDecision;
  const onDecision: GoalTickOptions['onDecision'] = (observation) => {
    if (observation.effects.some((effect) => effect.type === 'started_run')) startedRun = true;
    record?.(observation);
  };

  let result: GoalTickResult | undefined;
  // The run this advance last started. Reported in place of the tick that only
  // found the cap reached, so a caller still learns which Task it started.
  let lastStart: GoalTickResult | undefined;
  try {
    for (let ticks = 1; ticks <= MAX_TICKS_PER_ADVANCE; ticks++) {
      startedRun = false;
      result = await service.tick(goalId, { onDecision });
      log('goal %s tick %d β†’ %s (%s)', goalId, ticks, result.outcome, result.message);
      if (startedRun && result.outcome === 'waiting_external') {
        lastStart = result;
        continue;
      }
      if (STOP_OUTCOMES.has(result.outcome)) {
        await settleTrace(recorder, service, goalId);
        const reported = result.outcome === 'waiting_external' && lastStart ? lastStart : result;
        return { goalId, result: reported, ticks };
      }
    }
  } catch (error) {
    // Record what the advance managed to do before it threw. A trajectory that
    // stops mid-advance with the failure attached is the whole point of keeping
    // one β€” losing it is exactly the blind spot this replaces.
    await recorder.flush(error);
    throw error;
  }

  log('goal %s hit the per-advance tick limit', goalId);
  await settleTrace(recorder, service, goalId);
  return { goalId, result: result!, ticks: MAX_TICKS_PER_ADVANCE };
};

/**
 * Write the advance, then close the trajectory if the goal itself is done.
 *
 * The status is re-read rather than inferred from the outcome: `failed` is
 * reported both by a goal that is genuinely over and by a tick that could not
 * find a responsible task, and only the first should finalize.
 */
const settleTrace = async (
  recorder: GoalAdvanceRecorder,
  service: GoalService,
  goalId: string,
): Promise<void> => {
  await recorder.flush();
  if (!recorder.enabled) return;

  const status = await service.status(goalId).catch(() => undefined);
  if (status && TERMINAL_GOAL_STATUSES.has(status)) await recorder.finalize(status);
};