π€― 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
β»οΈ refactor(server): split the 1,298-line goal router into sub-domain modules (#21097)
Arvin Xuf2c390079aa0
diff --git a/apps/server/src/routers/lambda/goal.ts b/apps/server/src/routers/lambda/goal.tsdeleted file mode 100644index 6845fc04d76..00000000000--- a/apps/server/src/routers/lambda/goal.ts+++ /dev/null@@ -1,1298 +0,0 @@-import { goalStatuses, ROLLOUT_MAX_CANARY_SIZE } from '@lobechat/const/goal';-import { MAX_GOAL_METRIC_CRITERIA, summarizeGoalSupervision } from '@lobechat/types';-import { TRPCError } from '@trpc/server';-import { z } from 'zod';--import { GoalModel } from '@/database/models/goal';-import { heteroAuthedProcedure, router } from '@/libs/trpc/lambda';-import { serverDatabase } from '@/libs/trpc/lambda/middleware';-import { GoalService } from '@/server/services/goal';-import { advanceGoal } from '@/server/services/goal/advanceGoal';-import { graphForClient } from '@/server/services/goal/graphCompat';-import { GoalManagerService, goalPlanSchema } from '@/server/services/goal/manager';-import {- DEFAULT_MANAGER_MAX_TURNS,- managerTurnsSpent,-} from '@/server/services/goal/recoveryPolicy';-import { GoalReportStore, type SubmitGoalReportInput } from '@/server/services/goal/reportStore';-import { scheduleGoalAdvance } from '@/server/services/goal/scheduler';-import { GoalStartService } from '@/server/services/goal/start';-import { GoalWaitService, goalWakeEventSchema } from '@/server/services/goal/wait';-import {- HeteroOperationPrincipalError,- resolveActiveHeteroOperationPrincipal,-} from '@/server/services/heterogeneousAgent/operationPrincipal';--import { assertWorkspaceRowManageable } from './_helpers/assertWorkspaceRowManageable';-import { goalGraphEditProcedures } from './goalGraphEdit';-import { goalProcedure, goalWriteProcedure, idInput, mapGoalError } from './goalProcedures';--/**- * A wrap-up report submitted from a run. The metadata passes through as a- * record: `GoalReportStore.submit` parses it with the report schema and checks- * every reference against the Goal graph.- */-const reportInput = idInput.extend({- operationId: z.string().min(1),- report: z.object({- content: z.string(),- metadata: z.record(z.string(), z.unknown()),- }),-});--/** Everything a goal is created from except who owns it. */-const conversationGoalInput = z.object({- config: z- .object({- acceptance: z- .object({- /** Numeric clauses gating acceptance; keyed by a series on this goal. */- metrics: z- .array(- z.object({- key: z.string().min(1).max(255),- op: z.enum(['gte', 'lte', 'gt', 'lt', 'eq']).optional(),- target: z.number(),- title: z.string().max(255).optional(),- }),- )- .max(MAX_GOAL_METRIC_CRITERIA)- .optional(),- })- .optional(),- // Bounds mirror `resolveMaxConcurrentTasks`, so a rejected value and- // a clamped one cannot disagree about what the cap may be.- exploration: z- .object({- instruction: z.string().min(1).max(8000),- maxExperiments: z.number().int().min(1).max(200),- })- .optional(),- manager: z- .object({- instruction: z.string().max(8000).optional(),- maxTurns: z.number().int().min(1).max(100).optional(),- })- .optional(),- maxConcurrentTasks: z.number().int().min(1).max(10).nullable().optional(),- supervision: z- .object({- enabled: z.boolean(),- maxIncidents: z.number().int().min(1).max(100).optional(),- })- .optional(),- /** Dedicated executor for the goal's Tasks; the goal agent supervises. */- taskAgentId: z.string().min(1).optional(),- recovery: z- .object({- maxAttemptsPerTask: z.number().int().positive().optional(),- maxStepsPerRun: z.number().int().positive().nullable().optional(),- operationLeaseTimeoutMs: z.number().int().min(60_000).optional(),- })- .optional(),- /**- * Batch roll-out policy for a goal that is one repeated mould. Omit it and- * the planner's own homogeneity claim decides; `off` forbids a batch even- * when the planner claims one.- */- rollout: z- .object({- canarySize: z.number().int().min(2).max(ROLLOUT_MAX_CANARY_SIZE).optional(),- minHomogeneousUnits: z.number().int().min(1).max(200).optional(),- trigger: z.enum(['canary', 'full', 'off']),- waveSize: z.number().int().min(1).max(50).optional(),- })- .optional(),- schedule: z- .object({- /** ISO-8601 instant; past it the coordinator stops dispatching. */- deadline: z.string().datetime().nullable().optional(),- })- .optional(),- })- .optional(),- maxRounds: z.number().int().positive().optional(),- maxTotalCost: z.number().positive().optional(),- /** Structured acceptance criteria β persisted rows that gate the terminal acceptance. */- criteria: z- .array(- z.object({- description: z.string().optional(),- instruction: z.string().optional(),- title: z.string().min(1),- }),- )- .optional(),- /**- * Open a conversation goal as a draft that runs only once `start` launches- * it. Sent by a CLI that has `lh goal start`; ignored for a standalone goal.- */- draft: z.boolean().optional(),- problemDescription: z.string().optional(),- projectId: z.string().optional(),- requirement: z.string().optional(),- title: z.string().min(1),- /** Seed task nodes; `dependsOn` names other seeds by title. */- tasks: z- .array(- z.union([- z.string().min(1),- z.object({- dependsOn: z.array(z.string().min(1)).optional(),- description: z.string().optional(),- title: z.string().min(1),- }),- ]),- )- .optional(),-});--const createGoalInput = conversationGoalInput.extend({- agentId: z.string().optional(),- /** Set by the `/goal` tool so the seeded graph is authored by the agent. */- createdByAgentId: z.string().optional(),-});--const bindTopicInput = idInput.extend({- /** Move a goal already bound to another topic or task. */- force: z.boolean().optional(),- /** Keep unfinished Tasks with their current agent when the goal agent changes. */- goalOnly: z.boolean().optional(),- /** The topic run doing the binding; its agent and topic are used. */- operationId: z.string().min(1),-});--const bindTopicResponse = ({- graph,- previousSubject,- reassignedTaskIds,- topicId,- turnToken,-}: Awaited<ReturnType<GoalService['bindTopic']>>) => ({- data: graph,- message:- `Goal bound to topic ${topicId}` +- (previousSubject.id && previousSubject.id !== topicId- ? ` (moved from ${previousSubject.type} ${previousSubject.id})`- : '') +- (reassignedTaskIds.length ? `; ${reassignedTaskIds.length} task(s) reassigned` : ''),- previousSubject,- reassignedTaskIds,- success: true,- turnToken,-});--/** `<ISO>|<event id>` β opaque to callers, so the pair can change shape later. */-const encodeEventCursor = (cursor: { createdAt: Date; id: string }) =>- `${cursor.createdAt.toISOString()}|${cursor.id}`;--const decodeEventCursor = (raw: string) => {- const separator = raw.lastIndexOf('|');- const createdAt = separator > 0 ? new Date(raw.slice(0, separator)) : new Date(Number.NaN);- const id = separator > 0 ? raw.slice(separator + 1) : '';- if (Number.isNaN(createdAt.getTime()) || !id) {- throw new TRPCError({ code: 'BAD_REQUEST', message: 'Invalid events cursor' });- }- return { createdAt, id };-};--/**- * Events paging is cursor-based, not offset-based: the feed is live and events- * are PREPENDED while a goal runs, so an offset taken on page one re-serves a row- * that a later insert pushed down.- */-const eventsInput = idInput.extend({- cursor: z.string().min(1).optional(),- entityType: z.enum(['goal', 'node', 'edge', 'decision', 'task']).optional(),- eventType: z- .enum([- 'created',- 'updated',- 'activated',- 'resolved',- 'rejected',- 'retired',- 'linked',- 'unlinked',- ])- .optional(),- limit: z.number().int().min(1).max(200).optional(),-});--const planContextInput = idInput.extend({- operationId: z.string().min(1).optional(),- // A refused `lh goal plan` re-reads this with the plan it just submitted, so- // the plan-specific guards it was refused on (`existing_work`,- // `acceptance_escalate_only`, `wait_unsettled`, `wait_until_past`) are- // re-evaluated. Without it the read would report `ok` and explain nothing.- plan: goalPlanSchema.optional(),- token: z.string().min(1).optional(),-});--type EventsInput = z.infer<typeof eventsInput>;-type PlanContextInput = z.infer<typeof planContextInput>;--/** Shared by the ordinary and the operation-authenticated read; the caller builds the manager. */-const readPlanContext = (manager: GoalManagerService, input: PlanContextInput) =>- manager.admission(input.id, {- operationId: input.operationId,- plan: input.plan,- token: input.token,- });--const readEvents = async (goalService: GoalService, input: EventsInput) => {- const page = await goalService.listEvents(input.id, {- cursor: input.cursor ? decodeEventCursor(input.cursor) : undefined,- entityType: input.entityType,- eventType: input.eventType,- limit: input.limit ?? 50,- });- return {- events: page.events,- nextCursor: page.nextCursor ? encodeEventCursor(page.nextCursor) : null,- };-};--/**- * The Goal reads a device / gateway planning run needs.- *- * Those runs hold a `hetero-operation` JWT, and `oidcAuth` deliberately refuses- * it on ordinary authed routes so a leaked sandbox token cannot be replayed- * elsewhere β which left a planner unable to read the very Goal it plans, and- * made `lh goal state` (and the refusal explanation) unauthorized in exactly the- * runs that need them. These mirror the ordinary reads for that caller only:- * writes stay on `goalWriteProcedure`, and scope comes from the operation's own- * principal.- *- * The capability is `hetero:ingest` rather than `goal:manage` because the token- * a takeover planning turn holds never carries `goal:manage` β that is granted- * only to the `/goal` message that creates a goal, so requiring it would leave- * the planner locked out. `hetero:ingest` is what the same token already uses to- * submit its plan.- *- * `allowTerminalOperation` is deliberately unset: reads of a run that has already- * ended are an ordinary-session job, so the token works exactly while its turn- * runs.- *- * Scope is the operation's principal AND its Goal: every endpoint calls- * `GoalManagerService.assertOperationOwnsTurn`, so a token reaches exactly the- * Goal whose current turn it holds β not any Goal in the workspace whose id it- * learns.- */-const goalOperationReadProcedure = heteroAuthedProcedure.use(serverDatabase).use(async (opts) => {- if (opts.ctx.heteroAuthKind !== 'operation' || !opts.ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'hetero:ingest',- claims: opts.ctx.heteroOperation,- db: opts.ctx.serverDB,- operationId: opts.ctx.heteroOperation.operation_id,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code: error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- return opts.next({- ctx: {- goalManager: new GoalManagerService(- opts.ctx.serverDB,- principal.userId,- principal.workspaceId,- ),- goalService: new GoalService(opts.ctx.serverDB, principal.userId, principal.workspaceId),- // The operation the token was minted for β not an id the caller supplies.- heteroOperationId: principal.operationId,- userId: principal.userId,- workspaceId: principal.workspaceId ?? null,- },- });-});--/** What `start` folds into a batch: the probes that run first, and the recipe every unit follows. */-const startRolloutInput = z- .object({- probeNodeIds: z.array(z.string().min(1)).min(2).max(10),- recipe: z.string().trim().min(1).max(8000),- trigger: z.enum(['canary', 'full']).optional(),- unitNodeIds: z.array(z.string().min(1)).max(60).optional(),- variants: z- .array(- z.object({- axis: z.string().trim().min(1).max(120),- values: z.array(z.string().trim().min(1).max(120)).min(1).max(20),- }),- )- .max(10)- .optional(),- })- .strict();-const startInput = idInput.extend({ rollout: startRolloutInput.optional() });--export const goalRouter = router({- wake: goalWriteProcedure- .input(goalWakeEventSchema.extend({ id: z.string() }))- .mutation(async ({ ctx, input }) => {- const { id, ...event } = input;- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');- const data = await new GoalWaitService(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- ).deliver(id, event);- return { data, success: true };- }),- // A plan is an operation result; the turn token further restricts ingestion to its Goal.- submitOperationPlan: heteroAuthedProcedure- .use(serverDatabase)- .input(- idInput.extend({- token: z.string().min(1),- operationId: z.string().min(1),- plan: goalPlanSchema,- }),- )- .mutation(async ({ ctx, input }) => {- if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'hetero:ingest',- claims: ctx.heteroOperation,- db: ctx.serverDB,- operationId: input.operationId,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code:- error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- const data = await new GoalManagerService(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).submit(input.id, input.token, input.operationId, input.plan);- await scheduleGoalAdvance({- goalId: input.id,- userId: principal.userId,- workspaceId: principal.workspaceId,- });- return { data, success: true };- }),- // The wrap-up report of a heterogeneous agent's run: server tools never reach- // a device run, so its report arrives through the CLI instead of the tool.- submitOperationReport: heteroAuthedProcedure- .use(serverDatabase)- .input(reportInput)- .mutation(async ({ ctx, input }) => {- if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'hetero:ingest',- claims: ctx.heteroOperation,- db: ctx.serverDB,- operationId: input.operationId,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code:- error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- const data = await new GoalReportStore(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).submitFromOperation(input.id, input.report as SubmitGoalReportInput, input.operationId);- return { data, success: true };- }),- submitReport: goalWriteProcedure.input(reportInput).mutation(async ({ ctx, input }) => {- const data = await new GoalReportStore(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- ).submitFromOperation(input.id, input.report as SubmitGoalReportInput, input.operationId);- return { data, success: true };- }),- /** Launch a draft goal; until now the coordinator has left its graph alone. */- start: goalWriteProcedure.input(startInput).mutation(async ({ ctx, input }) => {- // A draft is its creator's arrangement: in a shared workspace only they or- // an owner may launch it (and, with a rollout, fold its seed nodes away).- const goal = await ctx.goalModel.findById(input.id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');- try {- const data = await new GoalStartService(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- ).start(input.id, { rollout: input.rollout });- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'start',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: 'Goal started', success: true };- } catch (error) {- mapGoalError(error, 'start');- }- }),- /** `start` from the goal agent's own run, authenticated by its operation token. */- startOperation: heteroAuthedProcedure- .use(serverDatabase)- .input(startInput.extend({ operationId: z.string().min(1) }))- .mutation(async ({ ctx, input: { operationId, ...input } }) => {- if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'hetero:ingest',- claims: ctx.heteroOperation,- db: ctx.serverDB,- operationId,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code:- error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- try {- const data = await new GoalStartService(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).start(input.id, { operationId, rollout: input.rollout });- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'start',- userId: principal.userId,- workspaceId: principal.workspaceId,- });- return { data, message: 'Goal started', success: true };- } catch (error) {- mapGoalError(error, 'start');- }- }),- submitPlan: goalWriteProcedure- .input(- idInput.extend({- token: z.string().min(1),- operationId: z.string().min(1),- plan: goalPlanSchema,- }),- )- .mutation(async ({ ctx, input }) => {- const data = await new GoalManagerService(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- ).submit(input.id, input.token, input.operationId, input.plan);- await scheduleGoalAdvance({- goalId: input.id,- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, success: true };- }),- ...goalGraphEditProcedures,-- /**- * `/goal` from a conversation run authenticated by its operation token (device- * and gateway runs). The agent and conversation come from the operation, and- * the returned `turnToken` lets that run plan the goal immediately.- */- createConversationGoal: heteroAuthedProcedure- .use(serverDatabase)- .input(conversationGoalInput.extend({ operationId: z.string().min(1) }))- .mutation(async ({ ctx, input: { operationId, ...input } }) => {- if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'goal:manage',- claims: ctx.heteroOperation,- db: ctx.serverDB,- operationId,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code:- error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- try {- const { graph, turnToken } = await new GoalService(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).createFromConversation(operationId, input);- await scheduleGoalAdvance({- goalId: graph.goal.id,- trigger: 'create',- userId: principal.userId,- workspaceId: principal.workspaceId,- });- return { data: graph, message: 'Goal created', success: true, turnToken };- } catch (error) {- mapGoalError(error, 'create');- }- }),-- /**- * Attach an existing goal to the topic of a run authenticated by its- * operation token (device and gateway runs), as if the goal had been created- * there with `/goal`. Takes the same `goal:manage` capability as creating one.- */- bindOperationTopic: heteroAuthedProcedure- .use(serverDatabase)- .input(bindTopicInput)- .mutation(async ({ ctx, input: { id, operationId, force, goalOnly } }) => {- if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {- throw new TRPCError({- code: 'UNAUTHORIZED',- message: 'An operation-bound token is required',- });- }- let principal;- try {- principal = await resolveActiveHeteroOperationPrincipal({- capability: 'goal:manage',- claims: ctx.heteroOperation,- db: ctx.serverDB,- operationId,- });- } catch (error) {- if (!(error instanceof HeteroOperationPrincipalError)) throw error;- throw new TRPCError({- cause: error,- code:- error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',- message: error.message,- });- }- try {- const goal = await new GoalModel(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- // A token carries no workspace role, so only the goal's creator may- // move it β the strict side of the rule `setAgent` applies.- assertWorkspaceRowManageable(principal, goal.userId, 'goal');- const result = await new GoalService(- ctx.serverDB,- principal.userId,- principal.workspaceId,- ).bindTopic(id, operationId, { force, goalOnly });- await scheduleGoalAdvance({- goalId: id,- trigger: 'manual',- userId: principal.userId,- workspaceId: principal.workspaceId,- });- return bindTopicResponse(result);- } catch (error) {- mapGoalError(error, 'bindTopic');- }- }),-- /**- * Attach an existing goal to the topic run with this id (a desktop- * run signed in as the user). Agent and topic come from the operation;- * a local run without a server row also names them, checked like `create`.- */- bindTopic: goalWriteProcedure- .input(- bindTopicInput.extend({- agentId: z.string().min(1).optional(),- topicId: z.string().min(1).optional(),- }),- )- .mutation(async ({ ctx, input: { id, operationId, agentId, topicId, force, goalOnly } }) => {- try {- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');- const result = await ctx.goalService.bindTopic(id, operationId, {- force,- goalOnly,- localRun: agentId && topicId ? { agentId, topicId } : undefined,- });- await scheduleGoalAdvance({- goalId: id,- trigger: 'manual',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return bindTopicResponse(result);- } catch (error) {- mapGoalError(error, 'bindTopic');- }- }),-- create: goalWriteProcedure- .input(- createGoalInput.extend({- /**- * Create the goal from the conversation run with this id (`/goal` on a- * desktop run signed in as the user). Agent and conversation come from- * the operation, so `agentId` / `createdByAgentId` are ignored.- */- conversationOperationId: z.string().min(1).optional(),- /**- * A local desktop run's operation lives only on the client, so it also- * names its conversation; the server checks the topic belongs to the- * caller and to this agent before accepting it.- */- conversationTopicId: z.string().min(1).optional(),- }),- )- .mutation(- async ({ ctx, input: { conversationOperationId, conversationTopicId, ...input } }) => {- try {- if (conversationOperationId) {- const { agentId, createdByAgentId: _createdBy, ...goalInput } = input;- const { graph, turnToken } = await ctx.goalService.createFromConversation(- conversationOperationId,- goalInput,- conversationTopicId && agentId- ? { agentId, topicId: conversationTopicId }- : undefined,- );- await scheduleGoalAdvance({- goalId: graph.goal.id,- trigger: 'create',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data: graph, message: 'Goal created', success: true, turnToken };- }- const { draft: _draft, ...standaloneInput } = input;- const data = await ctx.goalService.create(standaloneInput);- // A goal is not a document β creating one means starting it. The- // coordinator takes it from here without a client holding a loop open.- await scheduleGoalAdvance({- goalId: data.goal.id,- trigger: 'create',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: 'Goal created', success: true };- } catch (error) {- mapGoalError(error, 'create');- }- },- ),-- /**- * Answer a goal's clarification round at once. One advance follows all the- * answers, so the re-plan never starts with half of them.- */- answerClarifications: goalWriteProcedure- .input(- idInput.extend({- answers: z- .array(- z.object({- decisionId: z.string().uuid(),- optionId: z.string(),- resolution: z.string().optional(),- }),- )- .min(1),- }),- )- .mutation(async ({ ctx, input }) => {- try {- await ctx.goalService.answerClarifications(input.id, input.answers);- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'decide',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { message: 'Clarifications answered', success: true };- } catch (error) {- mapGoalError(error, 'answer clarifications for');- }- }),-- decide: goalWriteProcedure- .input(- idInput.extend({- decisionId: z.string().uuid(),- optionId: z.string(),- resolution: z.string().optional(),- }),- )- .mutation(async ({ ctx, input }) => {- try {- const data = await ctx.goalService.decide(- input.id,- input.decisionId,- input.optionId,- input.resolution,- );- // Answering the gate is what unblocks the Task; carry on from here.- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'decide',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: 'Decision resolved', success: true };- } catch (error) {- mapGoalError(error, 'decide');- }- }),-- retireNodes: goalWriteProcedure- .input(- idInput.extend({- nodeIds: z.array(z.string().uuid()).min(1),- reason: z.string().optional(),- }),- )- .mutation(async ({ ctx, input }) => {- try {- // Same ownership rule as restart/delete: retiring cancels the nodes'- // Tasks and recovery gates, so visibility is not manageability.- const goal = await ctx.goalModel.findById(input.id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- const data = await ctx.goalService.retireNodes(input.id, input.nodeIds, input.reason);- // Retiring can unpark the goal or clear the last unfinished task. Advance- // as the goal's owner: a workspace owner retiring a colleague's nodes- // cannot see that colleague's private Tasks, so an advance under the- // caller would read their bound nodes as `missing_task`.- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'manual',- userId: goal.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: `Retired ${data.retiredNodeIds.length} node(s)`, success: true };- } catch (error) {- mapGoalError(error, 'retireNodes');- }- }),-- /** Declare or clear the numeric clauses gating this goal's acceptance. */- setMetricCriteria: goalWriteProcedure- .input(- idInput.extend({- metrics: z- .array(- z.object({- key: z.string().min(1).max(255),- op: z.enum(['gte', 'lte', 'gt', 'lt', 'eq']).optional(),- target: z.number(),- title: z.string().max(255).optional(),- }),- )- .max(MAX_GOAL_METRIC_CRITERIA),- /** `merge` upserts by key server-side; `replace` (default) swaps the list. */- mode: z.enum(['merge', 'replace']).optional(),- }),- )- .mutation(async ({ ctx, input }) => {- try {- const goal = await ctx.goalModel.findById(input.id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');- const data = await ctx.goalService.setMetricCriteria(input.id, input.metrics, input.mode);- // Relaxing a clause can free a goal parked short of acceptance.- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'observe',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: 'Metric criteria updated', success: true };- } catch (error) {- mapGoalError(error, 'setMetricCriteria');- }- }),-- /**- * Record a measurement against this goal and let the coordinator react.- *- * The graph otherwise only moves when a Task settles, but a long-horizon- * goal advances when the *world* changes β a follower count, a conversion- * rate. This is that entry point: it writes to the generic metrics layer- * (subject = this goal) and schedules an advance, so a goal whose numeric- * acceptance was the last thing outstanding closes on the observation- * instead of waiting up to a sweep window.- */- recordObservation: goalWriteProcedure- .input(- idInput.extend({- key: z.string().min(1).max(255),- kind: z.enum(['gauge', 'counter']).optional(),- observedAt: z.coerce.date().optional(),- title: z.string().optional(),- unit: z.string().optional(),- value: z.number(),- }),- )- .mutation(async ({ ctx, input }) => {- try {- const { id, ...observation } = input;- const { shouldAdvance, ...data } = await ctx.goalService.recordObservation(id, observation);- // A goal parked short of its measured acceptance is only worth waking- // when the measurement actually cleared the gate; otherwise the advance- // would tick straight back out as `goal_paused`.- if (shouldAdvance) {- await scheduleGoalAdvance({- goalId: id,- trigger: 'observe',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- }- return { data, message: 'Observation recorded', success: true };- } catch (error) {- mapGoalError(error, 'recordObservation');- }- }),-- /**- * Run the coordinator now and report where it stopped.- *- * The goal advances on its own β this is the "don't wait for the next event"- * nudge, so the surface can hand off in one call instead of holding a tick- * loop open in the browser.- */- advance: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {- try {- const { result, ticks } = await advanceGoal({- goalId: input.id,- trigger: 'manual',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data: { ...result, ticks }, message: result.message, success: true };- } catch (error) {- mapGoalError(error, 'advance');- }- }),-- /**- * Delete a goal and, by FK cascade, its whole graph. Anything still running- * is stopped first; the graph Tasks themselves are deliberately left in place- * β they are ordinary tasks with their own history and acceptance.- */- delete: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {- try {- // `agent:update` says the member may change goals; it does not say whose.- // Without this any member could delete a colleague's goal and cascade its- // whole graph away, which is the same rule tasks already enforce.- const goal = await ctx.goalModel.findById(input.id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- await ctx.goalService.delete(input.id);- return { message: 'Goal deleted', success: true };- } catch (error) {- mapGoalError(error, 'delete');- }- }),-- /** Clarifications still waiting on the user, grouped by goal. */- pendingForIsland: goalProcedure.query(async ({ ctx }) => {- try {- return { data: await ctx.goalService.pendingForIsland(), success: true };- } catch (error) {- mapGoalError(error, 'list pending decisions for');- }- }),-- pendingClarifications: goalProcedure.query(async ({ ctx }) => {- try {- return { data: await ctx.goalService.pendingClarifications(), success: true };- } catch (error) {- mapGoalError(error, 'list clarifications for');- }- }),-- graph: goalProcedure- .input(- idInput.extend({- /** Graph features this client renders (see `GOAL_GRAPH_CAPABILITY`). */- capabilities: z.array(z.string()).max(20).optional(),- }),- )- .query(async ({ ctx, input }) => {- try {- return {- data: graphForClient(await ctx.goalService.graph(input.id), input.capabilities),- success: true,- };- } catch (error) {- mapGoalError(error, 'graph');- }- }),-- /**- * Why `submitPlan` would accept or refuse a plan right now, without writing.- *- * `lh goal state` renders it and a refused `lh goal plan` re-reads it, so the- * CLI explains a refusal from the same predicates the write path uses β the- * manager and review snapshots a client cannot see from the graph alone.- */- planContext: goalProcedure.input(planContextInput).query(async ({ ctx, input }) => {- try {- const manager = new GoalManagerService(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- );- return { data: await readPlanContext(manager, input), success: true };- } catch (error) {- mapGoalError(error, 'read the plan context of');- }- }),-- /**- * The goal's audit trail, newest first, paged past the capped copy `graph`- * bundles. `lh goal events` reads it.- */- events: goalProcedure.input(eventsInput).query(async ({ ctx, input }) => {- try {- return { data: await readEvents(ctx.goalService, input), success: true };- } catch (error) {- mapGoalError(error, 'list events of');- }- }),-- // The same three reads for a planning run that holds an operation token, which- // the ordinary `goalProcedure` above refuses β see `goalOperationReadProcedure`.-- graphOperation: goalOperationReadProcedure.input(idInput).query(async ({ ctx, input }) => {- try {- await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);- return { data: await ctx.goalService.graph(input.id), success: true };- } catch (error) {- mapGoalError(error, 'graph');- }- }),-- planContextOperation: goalOperationReadProcedure- .input(planContextInput)- .query(async ({ ctx, input }) => {- try {- await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);- return { data: await readPlanContext(ctx.goalManager, input), success: true };- } catch (error) {- mapGoalError(error, 'read the plan context of');- }- }),-- eventsOperation: goalOperationReadProcedure.input(eventsInput).query(async ({ ctx, input }) => {- try {- await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);- return { data: await readEvents(ctx.goalService, input), success: true };- } catch (error) {- mapGoalError(error, 'list events of');- }- }),-- /**- * List goals with their graph roll-up: how many Tasks are done, how many- * decision gates wait on a human, and what the exploration has cost.- */- list: goalProcedure- .input(- z.object({- agentId: z.string().optional(),- limit: z.number().optional(),- offset: z.number().optional(),- projectId: z.string().optional(),- statuses: z.array(z.enum(goalStatuses)).optional(),- /** Goals created from this conversation. */- topicId: z.string().optional(),- }),- )- .query(async ({ input, ctx }) => ctx.goalModel.list(input)),-- pause: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {- try {- return { data: await ctx.goalService.pause(input.id), message: 'Goal paused', success: true };- } catch (error) {- mapGoalError(error, 'pause');- }- }),-- resume: goalWriteProcedure- .input(- idInput.extend({- /**- * The owner confirms the planning turn the Goal paused on has ended, so- * it is settled before resuming instead of pausing the Goal again.- */- confirmExit: z.boolean().optional(),- }),- )- .mutation(async ({ ctx, input }) => {- try {- if (input.confirmExit)- await new GoalManagerService(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- ).confirmTurnExit(input.id);- const data = await ctx.goalService.resume(input.id);- await scheduleGoalAdvance({- goalId: input.id,- trigger: 'resume',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- // Resuming does not give the main Agent more turns, so a goal it paused- // for running out would stop again on the next tick. Say how to continue- // it instead of leaving the caller to replace it with a new goal.- const message = managerTurnsSpent(data.config)- ? `Goal resumed, but its main Agent has used all ${data.config?.manager?.maxTurns ?? DEFAULT_MANAGER_MAX_TURNS} turns and it will pause again. Raise the cap with: lh goal set-budget ${input.id} --max-manager-turns <n>`- : 'Goal resumed';- return { data, message, success: true };- } catch (error) {- mapGoalError(error, 'resume');- }- }),-- /**- * End the goal by hand β achieved or canceled β and interrupt its live runs.- * Reopening a closed goal is `resume`.- */- close: goalWriteProcedure- .input(idInput.extend({ status: z.enum(['achieved', 'canceled']) }))- .mutation(async ({ ctx, input: { id, status } }) => {- try {- // Closing cancels live runs, so it carries the same ownership check as restart.- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- const data = await ctx.goalService.close(id, status);- return { data, message: `Goal ${status}`, success: true };- } catch (error) {- mapGoalError(error, 'close');- }- }),-- /**- * Start every unfinished Task node over (cancel stale runs, back to- * `backlog`), optionally under a different agent, and kick the coordinator- * so the goal begins moving without a second gesture.- */- restart: goalWriteProcedure- .input(idInput.extend({ agentId: z.string().min(1).optional() }))- .mutation(async ({ ctx, input: { id, agentId } }) => {- try {- // `agent:update` says the member may change goals; it does not say- // whose. Without this any member could restart a colleague's visible- // goal β cancel its live runs and reset its tasks.- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- const data = await ctx.goalService.restart(id, { agentId });- await scheduleGoalAdvance({- goalId: id,- trigger: 'restart',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return {- data,- message: `Restarted ${data.restartedTaskIds.length} task(s)`,- success: true,- };- } catch (error) {- mapGoalError(error, 'restart');- }- }),-- /** Rebind which persisted verify criteria gate this goal's terminal acceptance. */- setAcceptanceCriteria: goalWriteProcedure- .input(idInput.extend({ criteriaIds: z.array(z.string()) }))- .mutation(async ({ ctx, input }) => {- try {- await ctx.goalService.setAcceptanceCriteria(input.id, input.criteriaIds);- return { success: true };- } catch (error) {- mapGoalError(error, 'setAcceptanceCriteria');- }- }),-- /**- * Hand the goal to a different agent β the one that supervises and plans it.- * When that agent also does the goal's Tasks, unfinished ones follow by- * default (`goalOnly` keeps them); Tasks mid-run switch on their next attempt.- */- setAgent: goalWriteProcedure- .input(idInput.extend({ agentId: z.string().min(1), goalOnly: z.boolean().optional() }))- .mutation(async ({ ctx, input: { id, agentId, goalOnly } }) => {- try {- // Same ownership rule as restart/delete: visibility is not- // manageability, so only the goal's owner may hand it to a new agent.- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- const data = await ctx.goalService.setAgent(id, agentId, { goalOnly });- const reassigned = data.reassignedTaskIds.length;- return {- data,- message: reassigned- ? `Goal agent updated; ${reassigned} task(s) reassigned`- : 'Goal agent updated',- success: true,- };- } catch (error) {- mapGoalError(error, 'setAgent');- }- }),-- /**- * Route the goal's Tasks to a dedicated executor (`null` hands them back to- * the goal agent). Unfinished Tasks follow unless `goalOnly` is set.- */- setTaskAgent: goalWriteProcedure- .input(- idInput.extend({ agentId: z.string().min(1).nullable(), goalOnly: z.boolean().optional() }),- )- .mutation(async ({ ctx, input: { id, agentId, goalOnly } }) => {- try {- const goal = await ctx.goalModel.findById(id);- if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });- assertWorkspaceRowManageable(ctx, goal.userId, 'goal');-- const data = await ctx.goalService.setTaskAgent(id, agentId, { goalOnly });- const reassigned = data.reassignedTaskIds.length;- return {- data,- message: reassigned- ? `Task agent updated; ${reassigned} task(s) reassigned`- : 'Task agent updated',- success: true,- };- } catch (error) {- mapGoalError(error, 'setTaskAgent');- }- }),-- setBudget: goalWriteProcedure- .input(- idInput.extend({- /** ISO-8601 calendar-time budget; null clears the deadline. */- deadline: z.string().datetime().nullable().optional(),- maxAttemptsPerTask: z.number().int().positive().optional(),- /** null restores the default concurrency. */- maxConcurrentTasks: z.number().int().min(1).max(10).nullable().optional(),- maxExperiments: z.number().int().min(1).max(200).optional(),- /** Main Agent turn cap; only for a goal that has a main Agent. */- maxManagerTurns: z.number().int().min(1).max(100).optional(),- maxRounds: z.number().int().positive().nullable().optional(),- /** null removes the per-run step cap. */- maxStepsPerRun: z.number().int().positive().nullable().optional(),- maxTotalCost: z.number().positive().nullable().optional(),- }),- )- .mutation(async ({ ctx, input: { id, ...budget } }) => {- try {- const data = await ctx.goalService.setBudget(id, budget);- // Raising a budget is how a user un-sticks a goal that stopped on one;- // it should start moving again without a second gesture.- await scheduleGoalAdvance({- goalId: id,- trigger: 'budget',- userId: ctx.userId,- workspaceId: ctx.workspaceId ?? undefined,- });- return { data, message: 'Goal budget updated', success: true };- } catch (error) {- mapGoalError(error, 'setBudget');- }- }),-- supervision: goalProcedure.input(idInput).query(async ({ ctx, input }) => {- const graph = await ctx.goalService.graph(input.id);- const state = graph.goal.config?.supervisorState;- return {- data: {- enabled: graph.goal.config?.supervision?.enabled ?? false,- state,- summary: summarizeGoalSupervision(state),- },- success: true,- };- }),-- tick: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {- try {- return { data: await ctx.goalService.tick(input.id), success: true };- } catch (error) {- mapGoalError(error, 'tick');- }- }),-- updateRequirement: goalWriteProcedure- .input(idInput.extend({ requirement: z.string().min(1) }))- .mutation(async ({ ctx, input: { id, requirement } }) => {- try {- return { data: await ctx.goalService.updateRequirement(id, requirement), success: true };- } catch (error) {- mapGoalError(error, 'updateRequirement');- }- }),-});diff --git a/apps/server/src/routers/lambda/goal/configuration.ts b/apps/server/src/routers/lambda/goal/configuration.tsnew file mode 100644index 00000000000..f4d8e6ea5e0--- /dev/null+++ b/apps/server/src/routers/lambda/goal/configuration.ts@@ -0,0 +1,196 @@+import { MAX_GOAL_METRIC_CRITERIA } from '@lobechat/types';+import { TRPCError } from '@trpc/server';+import { z } from 'zod';++import { scheduleGoalAdvance } from '@/server/services/goal/scheduler';++import { assertWorkspaceRowManageable } from '../_helpers/assertWorkspaceRowManageable';+import { goalWriteProcedure, idInput, mapGoalError } from '../goalProcedures';++export const configurationProcedures = {+ /** Declare or clear the numeric clauses gating this goal's acceptance. */+ setMetricCriteria: goalWriteProcedure+ .input(+ idInput.extend({+ metrics: z+ .array(+ z.object({+ key: z.string().min(1).max(255),+ op: z.enum(['gte', 'lte', 'gt', 'lt', 'eq']).optional(),+ target: z.number(),+ title: z.string().max(255).optional(),+ }),+ )+ .max(MAX_GOAL_METRIC_CRITERIA),+ /** `merge` upserts by key server-side; `replace` (default) swaps the list. */+ mode: z.enum(['merge', 'replace']).optional(),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ const goal = await ctx.goalModel.findById(input.id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');+ const data = await ctx.goalService.setMetricCriteria(input.id, input.metrics, input.mode);+ // Relaxing a clause can free a goal parked short of acceptance.+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'observe',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: 'Metric criteria updated', success: true };+ } catch (error) {+ mapGoalError(error, 'setMetricCriteria');+ }+ }),+ /**+ * Record a measurement against this goal and let the coordinator react.+ *+ * The graph otherwise only moves when a Task settles, but a long-horizon+ * goal advances when the *world* changes β a follower count, a conversion+ * rate. This is that entry point: it writes to the generic metrics layer+ * (subject = this goal) and schedules an advance, so a goal whose numeric+ * acceptance was the last thing outstanding closes on the observation+ * instead of waiting up to a sweep window.+ */+ recordObservation: goalWriteProcedure+ .input(+ idInput.extend({+ key: z.string().min(1).max(255),+ kind: z.enum(['gauge', 'counter']).optional(),+ observedAt: z.coerce.date().optional(),+ title: z.string().optional(),+ unit: z.string().optional(),+ value: z.number(),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ const { id, ...observation } = input;+ const { shouldAdvance, ...data } = await ctx.goalService.recordObservation(id, observation);+ // A goal parked short of its measured acceptance is only worth waking+ // when the measurement actually cleared the gate; otherwise the advance+ // would tick straight back out as `goal_paused`.+ if (shouldAdvance) {+ await scheduleGoalAdvance({+ goalId: id,+ trigger: 'observe',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ }+ return { data, message: 'Observation recorded', success: true };+ } catch (error) {+ mapGoalError(error, 'recordObservation');+ }+ }),+ /** Rebind which persisted verify criteria gate this goal's terminal acceptance. */+ setAcceptanceCriteria: goalWriteProcedure+ .input(idInput.extend({ criteriaIds: z.array(z.string()) }))+ .mutation(async ({ ctx, input }) => {+ try {+ await ctx.goalService.setAcceptanceCriteria(input.id, input.criteriaIds);+ return { success: true };+ } catch (error) {+ mapGoalError(error, 'setAcceptanceCriteria');+ }+ }),+ /**+ * Hand the goal to a different agent β the one that supervises and plans it.+ * When that agent also does the goal's Tasks, unfinished ones follow by+ * default (`goalOnly` keeps them); Tasks mid-run switch on their next attempt.+ */+ setAgent: goalWriteProcedure+ .input(idInput.extend({ agentId: z.string().min(1), goalOnly: z.boolean().optional() }))+ .mutation(async ({ ctx, input: { id, agentId, goalOnly } }) => {+ try {+ // Same ownership rule as restart/delete: visibility is not+ // manageability, so only the goal's owner may hand it to a new agent.+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ const data = await ctx.goalService.setAgent(id, agentId, { goalOnly });+ const reassigned = data.reassignedTaskIds.length;+ return {+ data,+ message: reassigned+ ? `Goal agent updated; ${reassigned} task(s) reassigned`+ : 'Goal agent updated',+ success: true,+ };+ } catch (error) {+ mapGoalError(error, 'setAgent');+ }+ }),+ /**+ * Route the goal's Tasks to a dedicated executor (`null` hands them back to+ * the goal agent). Unfinished Tasks follow unless `goalOnly` is set.+ */+ setTaskAgent: goalWriteProcedure+ .input(+ idInput.extend({ agentId: z.string().min(1).nullable(), goalOnly: z.boolean().optional() }),+ )+ .mutation(async ({ ctx, input: { id, agentId, goalOnly } }) => {+ try {+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ const data = await ctx.goalService.setTaskAgent(id, agentId, { goalOnly });+ const reassigned = data.reassignedTaskIds.length;+ return {+ data,+ message: reassigned+ ? `Task agent updated; ${reassigned} task(s) reassigned`+ : 'Task agent updated',+ success: true,+ };+ } catch (error) {+ mapGoalError(error, 'setTaskAgent');+ }+ }),+ setBudget: goalWriteProcedure+ .input(+ idInput.extend({+ /** ISO-8601 calendar-time budget; null clears the deadline. */+ deadline: z.string().datetime().nullable().optional(),+ maxAttemptsPerTask: z.number().int().positive().optional(),+ /** null restores the default concurrency. */+ maxConcurrentTasks: z.number().int().min(1).max(10).nullable().optional(),+ maxExperiments: z.number().int().min(1).max(200).optional(),+ /** Main Agent turn cap; only for a goal that has a main Agent. */+ maxManagerTurns: z.number().int().min(1).max(100).optional(),+ maxRounds: z.number().int().positive().nullable().optional(),+ /** null removes the per-run step cap. */+ maxStepsPerRun: z.number().int().positive().nullable().optional(),+ maxTotalCost: z.number().positive().nullable().optional(),+ }),+ )+ .mutation(async ({ ctx, input: { id, ...budget } }) => {+ try {+ const data = await ctx.goalService.setBudget(id, budget);+ // Raising a budget is how a user un-sticks a goal that stopped on one;+ // it should start moving again without a second gesture.+ await scheduleGoalAdvance({+ goalId: id,+ trigger: 'budget',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: 'Goal budget updated', success: true };+ } catch (error) {+ mapGoalError(error, 'setBudget');+ }+ }),+ updateRequirement: goalWriteProcedure+ .input(idInput.extend({ requirement: z.string().min(1) }))+ .mutation(async ({ ctx, input: { id, requirement } }) => {+ try {+ return { data: await ctx.goalService.updateRequirement(id, requirement), success: true };+ } catch (error) {+ mapGoalError(error, 'updateRequirement');+ }+ }),+};diff --git a/apps/server/src/routers/lambda/goal/creation.ts b/apps/server/src/routers/lambda/goal/creation.tsnew file mode 100644index 00000000000..3c1cc8f8b14--- /dev/null+++ b/apps/server/src/routers/lambda/goal/creation.ts@@ -0,0 +1,355 @@+import { ROLLOUT_MAX_CANARY_SIZE } from '@lobechat/const/goal';+import { MAX_GOAL_METRIC_CRITERIA } from '@lobechat/types';+import { TRPCError } from '@trpc/server';+import { z } from 'zod';++import { GoalModel } from '@/database/models/goal';+import { heteroAuthedProcedure } from '@/libs/trpc/lambda';+import { serverDatabase } from '@/libs/trpc/lambda/middleware';+import { GoalService } from '@/server/services/goal';+import { scheduleGoalAdvance } from '@/server/services/goal/scheduler';+import {+ HeteroOperationPrincipalError,+ resolveActiveHeteroOperationPrincipal,+} from '@/server/services/heterogeneousAgent/operationPrincipal';++import { assertWorkspaceRowManageable } from '../_helpers/assertWorkspaceRowManageable';+import { goalWriteProcedure, idInput, mapGoalError } from '../goalProcedures';++/** Everything a goal is created from except who owns it. */+const conversationGoalInput = z.object({+ config: z+ .object({+ acceptance: z+ .object({+ /** Numeric clauses gating acceptance; keyed by a series on this goal. */+ metrics: z+ .array(+ z.object({+ key: z.string().min(1).max(255),+ op: z.enum(['gte', 'lte', 'gt', 'lt', 'eq']).optional(),+ target: z.number(),+ title: z.string().max(255).optional(),+ }),+ )+ .max(MAX_GOAL_METRIC_CRITERIA)+ .optional(),+ })+ .optional(),+ // Bounds mirror `resolveMaxConcurrentTasks`, so a rejected value and+ // a clamped one cannot disagree about what the cap may be.+ exploration: z+ .object({+ instruction: z.string().min(1).max(8000),+ maxExperiments: z.number().int().min(1).max(200),+ })+ .optional(),+ manager: z+ .object({+ instruction: z.string().max(8000).optional(),+ maxTurns: z.number().int().min(1).max(100).optional(),+ })+ .optional(),+ maxConcurrentTasks: z.number().int().min(1).max(10).nullable().optional(),+ supervision: z+ .object({+ enabled: z.boolean(),+ maxIncidents: z.number().int().min(1).max(100).optional(),+ })+ .optional(),+ /** Dedicated executor for the goal's Tasks; the goal agent supervises. */+ taskAgentId: z.string().min(1).optional(),+ recovery: z+ .object({+ maxAttemptsPerTask: z.number().int().positive().optional(),+ maxStepsPerRun: z.number().int().positive().nullable().optional(),+ operationLeaseTimeoutMs: z.number().int().min(60_000).optional(),+ })+ .optional(),+ /**+ * Batch roll-out policy for a goal that is one repeated mould. Omit it and+ * the planner's own homogeneity claim decides; `off` forbids a batch even+ * when the planner claims one.+ */+ rollout: z+ .object({+ canarySize: z.number().int().min(2).max(ROLLOUT_MAX_CANARY_SIZE).optional(),+ minHomogeneousUnits: z.number().int().min(1).max(200).optional(),+ trigger: z.enum(['canary', 'full', 'off']),+ waveSize: z.number().int().min(1).max(50).optional(),+ })+ .optional(),+ schedule: z+ .object({+ /** ISO-8601 instant; past it the coordinator stops dispatching. */+ deadline: z.string().datetime().nullable().optional(),+ })+ .optional(),+ })+ .optional(),+ maxRounds: z.number().int().positive().optional(),+ maxTotalCost: z.number().positive().optional(),+ /** Structured acceptance criteria β persisted rows that gate the terminal acceptance. */+ criteria: z+ .array(+ z.object({+ description: z.string().optional(),+ instruction: z.string().optional(),+ title: z.string().min(1),+ }),+ )+ .optional(),+ /**+ * Open a conversation goal as a draft that runs only once `start` launches+ * it. Sent by a CLI that has `lh goal start`; ignored for a standalone goal.+ */+ draft: z.boolean().optional(),+ problemDescription: z.string().optional(),+ projectId: z.string().optional(),+ requirement: z.string().optional(),+ title: z.string().min(1),+ /** Seed task nodes; `dependsOn` names other seeds by title. */+ tasks: z+ .array(+ z.union([+ z.string().min(1),+ z.object({+ dependsOn: z.array(z.string().min(1)).optional(),+ description: z.string().optional(),+ title: z.string().min(1),+ }),+ ]),+ )+ .optional(),+});++const createGoalInput = conversationGoalInput.extend({+ agentId: z.string().optional(),+ /** Set by the `/goal` tool so the seeded graph is authored by the agent. */+ createdByAgentId: z.string().optional(),+});++const bindTopicInput = idInput.extend({+ /** Move a goal already bound to another topic or task. */+ force: z.boolean().optional(),+ /** Keep unfinished Tasks with their current agent when the goal agent changes. */+ goalOnly: z.boolean().optional(),+ /** The topic run doing the binding; its agent and topic are used. */+ operationId: z.string().min(1),+});++const bindTopicResponse = ({+ graph,+ previousSubject,+ reassignedTaskIds,+ topicId,+ turnToken,+}: Awaited<ReturnType<GoalService['bindTopic']>>) => ({+ data: graph,+ message:+ `Goal bound to topic ${topicId}` ++ (previousSubject.id && previousSubject.id !== topicId+ ? ` (moved from ${previousSubject.type} ${previousSubject.id})`+ : '') ++ (reassignedTaskIds.length ? `; ${reassignedTaskIds.length} task(s) reassigned` : ''),+ previousSubject,+ reassignedTaskIds,+ success: true,+ turnToken,+});++export const creationProcedures = {+ /**+ * `/goal` from a conversation run authenticated by its operation token (device+ * and gateway runs). The agent and conversation come from the operation, and+ * the returned `turnToken` lets that run plan the goal immediately.+ */+ createConversationGoal: heteroAuthedProcedure+ .use(serverDatabase)+ .input(conversationGoalInput.extend({ operationId: z.string().min(1) }))+ .mutation(async ({ ctx, input: { operationId, ...input } }) => {+ if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'goal:manage',+ claims: ctx.heteroOperation,+ db: ctx.serverDB,+ operationId,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code:+ error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ try {+ const { graph, turnToken } = await new GoalService(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).createFromConversation(operationId, input);+ await scheduleGoalAdvance({+ goalId: graph.goal.id,+ trigger: 'create',+ userId: principal.userId,+ workspaceId: principal.workspaceId,+ });+ return { data: graph, message: 'Goal created', success: true, turnToken };+ } catch (error) {+ mapGoalError(error, 'create');+ }+ }),+ /**+ * Attach an existing goal to the topic of a run authenticated by its+ * operation token (device and gateway runs), as if the goal had been created+ * there with `/goal`. Takes the same `goal:manage` capability as creating one.+ */+ bindOperationTopic: heteroAuthedProcedure+ .use(serverDatabase)+ .input(bindTopicInput)+ .mutation(async ({ ctx, input: { id, operationId, force, goalOnly } }) => {+ if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'goal:manage',+ claims: ctx.heteroOperation,+ db: ctx.serverDB,+ operationId,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code:+ error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ try {+ const goal = await new GoalModel(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ // A token carries no workspace role, so only the goal's creator may+ // move it β the strict side of the rule `setAgent` applies.+ assertWorkspaceRowManageable(principal, goal.userId, 'goal');+ const result = await new GoalService(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).bindTopic(id, operationId, { force, goalOnly });+ await scheduleGoalAdvance({+ goalId: id,+ trigger: 'manual',+ userId: principal.userId,+ workspaceId: principal.workspaceId,+ });+ return bindTopicResponse(result);+ } catch (error) {+ mapGoalError(error, 'bindTopic');+ }+ }),+ /**+ * Attach an existing goal to the topic run with this id (a desktop+ * run signed in as the user). Agent and topic come from the operation;+ * a local run without a server row also names them, checked like `create`.+ */+ bindTopic: goalWriteProcedure+ .input(+ bindTopicInput.extend({+ agentId: z.string().min(1).optional(),+ topicId: z.string().min(1).optional(),+ }),+ )+ .mutation(async ({ ctx, input: { id, operationId, agentId, topicId, force, goalOnly } }) => {+ try {+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');+ const result = await ctx.goalService.bindTopic(id, operationId, {+ force,+ goalOnly,+ localRun: agentId && topicId ? { agentId, topicId } : undefined,+ });+ await scheduleGoalAdvance({+ goalId: id,+ trigger: 'manual',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return bindTopicResponse(result);+ } catch (error) {+ mapGoalError(error, 'bindTopic');+ }+ }),+ create: goalWriteProcedure+ .input(+ createGoalInput.extend({+ /**+ * Create the goal from the conversation run with this id (`/goal` on a+ * desktop run signed in as the user). Agent and conversation come from+ * the operation, so `agentId` / `createdByAgentId` are ignored.+ */+ conversationOperationId: z.string().min(1).optional(),+ /**+ * A local desktop run's operation lives only on the client, so it also+ * names its conversation; the server checks the topic belongs to the+ * caller and to this agent before accepting it.+ */+ conversationTopicId: z.string().min(1).optional(),+ }),+ )+ .mutation(+ async ({ ctx, input: { conversationOperationId, conversationTopicId, ...input } }) => {+ try {+ if (conversationOperationId) {+ const { agentId, createdByAgentId: _createdBy, ...goalInput } = input;+ const { graph, turnToken } = await ctx.goalService.createFromConversation(+ conversationOperationId,+ goalInput,+ conversationTopicId && agentId+ ? { agentId, topicId: conversationTopicId }+ : undefined,+ );+ await scheduleGoalAdvance({+ goalId: graph.goal.id,+ trigger: 'create',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data: graph, message: 'Goal created', success: true, turnToken };+ }+ const { draft: _draft, ...standaloneInput } = input;+ const data = await ctx.goalService.create(standaloneInput);+ // A goal is not a document β creating one means starting it. The+ // coordinator takes it from here without a client holding a loop open.+ await scheduleGoalAdvance({+ goalId: data.goal.id,+ trigger: 'create',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: 'Goal created', success: true };+ } catch (error) {+ mapGoalError(error, 'create');+ }+ },+ ),+};diff --git a/apps/server/src/routers/lambda/goal/index.procedures.test.ts b/apps/server/src/routers/lambda/goal/index.procedures.test.tsnew file mode 100644index 00000000000..dbafccd2f78--- /dev/null+++ b/apps/server/src/routers/lambda/goal/index.procedures.test.ts@@ -0,0 +1,70 @@+// @vitest-environment node+import { describe, expect, it } from 'vitest';++import { goalRouter } from '.';++/**+ * Pins the router's public surface: every procedure path, its kind, and its+ * middleware chain length (which encodes the procedure builder and its input+ * parsers). Splitting the router across files must keep all three.+ */+describe('goalRouter procedures', () => {+ const procedures = (goalRouter as any)._def.procedures as Record<string, any>;++ it('exposes exactly the expected procedure paths', () => {+ expect(Object.keys(procedures).sort()).toEqual(expected().map(([name]) => name));+ });++ it.each(expected())('%s is a %s with %i middlewares', (name, type, middlewares) => {+ expect(procedures[name]._def.type).toBe(type);+ expect(procedures[name]._def.middlewares).toHaveLength(middlewares);+ });+});++function expected(): Array<+ [name: string, type: 'mutation' | 'query' | 'subscription', middlewares: number]+> {+ return [+ ['addEdge', 'mutation', 13],+ ['addNode', 'mutation', 13],+ ['advance', 'mutation', 13],+ ['answerClarifications', 'mutation', 13],+ ['bindOperationTopic', 'mutation', 8],+ ['bindTopic', 'mutation', 13],+ ['close', 'mutation', 13],+ ['create', 'mutation', 13],+ ['createConversationGoal', 'mutation', 8],+ ['decide', 'mutation', 13],+ ['delete', 'mutation', 13],+ ['events', 'query', 12],+ ['eventsOperation', 'query', 9],+ ['graph', 'query', 12],+ ['graphOperation', 'query', 9],+ ['list', 'query', 12],+ ['pause', 'mutation', 13],+ ['pendingClarifications', 'query', 11],+ ['pendingForIsland', 'query', 11],+ ['planContext', 'query', 12],+ ['planContextOperation', 'query', 9],+ ['recordObservation', 'mutation', 13],+ ['removeEdge', 'mutation', 13],+ ['restart', 'mutation', 13],+ ['resume', 'mutation', 13],+ ['retireNodes', 'mutation', 13],+ ['setAcceptanceCriteria', 'mutation', 13],+ ['setAgent', 'mutation', 13],+ ['setBudget', 'mutation', 13],+ ['setMetricCriteria', 'mutation', 13],+ ['setTaskAgent', 'mutation', 13],+ ['start', 'mutation', 13],+ ['startOperation', 'mutation', 8],+ ['submitOperationPlan', 'mutation', 8],+ ['submitOperationReport', 'mutation', 8],+ ['submitPlan', 'mutation', 13],+ ['submitReport', 'mutation', 13],+ ['supervision', 'query', 12],+ ['tick', 'mutation', 13],+ ['updateRequirement', 'mutation', 13],+ ['wake', 'mutation', 13],+ ];+}diff --git a/apps/server/src/routers/lambda/goal/index.ts b/apps/server/src/routers/lambda/goal/index.tsnew file mode 100644index 00000000000..76bcdfe44d3--- /dev/null+++ b/apps/server/src/routers/lambda/goal/index.ts@@ -0,0 +1,17 @@+import { router } from '@/libs/trpc/lambda';++import { goalGraphEditProcedures } from '../goalGraphEdit';+import { configurationProcedures } from './configuration';+import { creationProcedures } from './creation';+import { lifecycleProcedures } from './lifecycle';+import { planningProcedures } from './planning';+import { readProcedures } from './reads';++export const goalRouter = router({+ ...lifecycleProcedures,+ ...planningProcedures,+ ...goalGraphEditProcedures,+ ...creationProcedures,+ ...configurationProcedures,+ ...readProcedures,+});diff --git a/apps/server/src/routers/lambda/goal/lifecycle.ts b/apps/server/src/routers/lambda/goal/lifecycle.tsnew file mode 100644index 00000000000..121b107ad20--- /dev/null+++ b/apps/server/src/routers/lambda/goal/lifecycle.ts@@ -0,0 +1,269 @@+import { TRPCError } from '@trpc/server';+import { z } from 'zod';++import { heteroAuthedProcedure } from '@/libs/trpc/lambda';+import { serverDatabase } from '@/libs/trpc/lambda/middleware';+import { advanceGoal } from '@/server/services/goal/advanceGoal';+import { GoalManagerService } from '@/server/services/goal/manager';+import {+ DEFAULT_MANAGER_MAX_TURNS,+ managerTurnsSpent,+} from '@/server/services/goal/recoveryPolicy';+import { scheduleGoalAdvance } from '@/server/services/goal/scheduler';+import { GoalStartService } from '@/server/services/goal/start';+import { GoalWaitService, goalWakeEventSchema } from '@/server/services/goal/wait';+import {+ HeteroOperationPrincipalError,+ resolveActiveHeteroOperationPrincipal,+} from '@/server/services/heterogeneousAgent/operationPrincipal';++import { assertWorkspaceRowManageable } from '../_helpers/assertWorkspaceRowManageable';+import { goalWriteProcedure, idInput, mapGoalError } from '../goalProcedures';++/** What `start` folds into a batch: the probes that run first, and the recipe every unit follows. */+const startRolloutInput = z+ .object({+ probeNodeIds: z.array(z.string().min(1)).min(2).max(10),+ recipe: z.string().trim().min(1).max(8000),+ trigger: z.enum(['canary', 'full']).optional(),+ unitNodeIds: z.array(z.string().min(1)).max(60).optional(),+ variants: z+ .array(+ z.object({+ axis: z.string().trim().min(1).max(120),+ values: z.array(z.string().trim().min(1).max(120)).min(1).max(20),+ }),+ )+ .max(10)+ .optional(),+ })+ .strict();++const startInput = idInput.extend({ rollout: startRolloutInput.optional() });++export const lifecycleProcedures = {+ wake: goalWriteProcedure+ .input(goalWakeEventSchema.extend({ id: z.string() }))+ .mutation(async ({ ctx, input }) => {+ const { id, ...event } = input;+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');+ const data = await new GoalWaitService(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ ).deliver(id, event);+ return { data, success: true };+ }),+ /** Launch a draft goal; until now the coordinator has left its graph alone. */+ start: goalWriteProcedure.input(startInput).mutation(async ({ ctx, input }) => {+ // A draft is its creator's arrangement: in a shared workspace only they or+ // an owner may launch it (and, with a rollout, fold its seed nodes away).+ const goal = await ctx.goalModel.findById(input.id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');+ try {+ const data = await new GoalStartService(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ ).start(input.id, { rollout: input.rollout });+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'start',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: 'Goal started', success: true };+ } catch (error) {+ mapGoalError(error, 'start');+ }+ }),+ /** `start` from the goal agent's own run, authenticated by its operation token. */+ startOperation: heteroAuthedProcedure+ .use(serverDatabase)+ .input(startInput.extend({ operationId: z.string().min(1) }))+ .mutation(async ({ ctx, input: { operationId, ...input } }) => {+ if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'hetero:ingest',+ claims: ctx.heteroOperation,+ db: ctx.serverDB,+ operationId,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code:+ error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ try {+ const data = await new GoalStartService(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).start(input.id, { operationId, rollout: input.rollout });+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'start',+ userId: principal.userId,+ workspaceId: principal.workspaceId,+ });+ return { data, message: 'Goal started', success: true };+ } catch (error) {+ mapGoalError(error, 'start');+ }+ }),+ /**+ * Run the coordinator now and report where it stopped.+ *+ * The goal advances on its own β this is the "don't wait for the next event"+ * nudge, so the surface can hand off in one call instead of holding a tick+ * loop open in the browser.+ */+ advance: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {+ try {+ const { result, ticks } = await advanceGoal({+ goalId: input.id,+ trigger: 'manual',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data: { ...result, ticks }, message: result.message, success: true };+ } catch (error) {+ mapGoalError(error, 'advance');+ }+ }),+ /**+ * Delete a goal and, by FK cascade, its whole graph. Anything still running+ * is stopped first; the graph Tasks themselves are deliberately left in place+ * β they are ordinary tasks with their own history and acceptance.+ */+ delete: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {+ try {+ // `agent:update` says the member may change goals; it does not say whose.+ // Without this any member could delete a colleague's goal and cascade its+ // whole graph away, which is the same rule tasks already enforce.+ const goal = await ctx.goalModel.findById(input.id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ await ctx.goalService.delete(input.id);+ return { message: 'Goal deleted', success: true };+ } catch (error) {+ mapGoalError(error, 'delete');+ }+ }),+ pause: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {+ try {+ return { data: await ctx.goalService.pause(input.id), message: 'Goal paused', success: true };+ } catch (error) {+ mapGoalError(error, 'pause');+ }+ }),+ resume: goalWriteProcedure+ .input(+ idInput.extend({+ /**+ * The owner confirms the planning turn the Goal paused on has ended, so+ * it is settled before resuming instead of pausing the Goal again.+ */+ confirmExit: z.boolean().optional(),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ if (input.confirmExit)+ await new GoalManagerService(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ ).confirmTurnExit(input.id);+ const data = await ctx.goalService.resume(input.id);+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'resume',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ // Resuming does not give the main Agent more turns, so a goal it paused+ // for running out would stop again on the next tick. Say how to continue+ // it instead of leaving the caller to replace it with a new goal.+ const message = managerTurnsSpent(data.config)+ ? `Goal resumed, but its main Agent has used all ${data.config?.manager?.maxTurns ?? DEFAULT_MANAGER_MAX_TURNS} turns and it will pause again. Raise the cap with: lh goal set-budget ${input.id} --max-manager-turns <n>`+ : 'Goal resumed';+ return { data, message, success: true };+ } catch (error) {+ mapGoalError(error, 'resume');+ }+ }),+ /**+ * End the goal by hand β achieved or canceled β and interrupt its live runs.+ * Reopening a closed goal is `resume`.+ */+ close: goalWriteProcedure+ .input(idInput.extend({ status: z.enum(['achieved', 'canceled']) }))+ .mutation(async ({ ctx, input: { id, status } }) => {+ try {+ // Closing cancels live runs, so it carries the same ownership check as restart.+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ const data = await ctx.goalService.close(id, status);+ return { data, message: `Goal ${status}`, success: true };+ } catch (error) {+ mapGoalError(error, 'close');+ }+ }),+ /**+ * Start every unfinished Task node over (cancel stale runs, back to+ * `backlog`), optionally under a different agent, and kick the coordinator+ * so the goal begins moving without a second gesture.+ */+ restart: goalWriteProcedure+ .input(idInput.extend({ agentId: z.string().min(1).optional() }))+ .mutation(async ({ ctx, input: { id, agentId } }) => {+ try {+ // `agent:update` says the member may change goals; it does not say+ // whose. Without this any member could restart a colleague's visible+ // goal β cancel its live runs and reset its tasks.+ const goal = await ctx.goalModel.findById(id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ const data = await ctx.goalService.restart(id, { agentId });+ await scheduleGoalAdvance({+ goalId: id,+ trigger: 'restart',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return {+ data,+ message: `Restarted ${data.restartedTaskIds.length} task(s)`,+ success: true,+ };+ } catch (error) {+ mapGoalError(error, 'restart');+ }+ }),+ tick: goalWriteProcedure.input(idInput).mutation(async ({ ctx, input }) => {+ try {+ return { data: await ctx.goalService.tick(input.id), success: true };+ } catch (error) {+ mapGoalError(error, 'tick');+ }+ }),+};diff --git a/apps/server/src/routers/lambda/goal/planning.ts b/apps/server/src/routers/lambda/goal/planning.tsnew file mode 100644index 00000000000..318fff2a781--- /dev/null+++ b/apps/server/src/routers/lambda/goal/planning.ts@@ -0,0 +1,233 @@+import { TRPCError } from '@trpc/server';+import { z } from 'zod';++import { heteroAuthedProcedure } from '@/libs/trpc/lambda';+import { serverDatabase } from '@/libs/trpc/lambda/middleware';+import { GoalManagerService, goalPlanSchema } from '@/server/services/goal/manager';+import { GoalReportStore, type SubmitGoalReportInput } from '@/server/services/goal/reportStore';+import { scheduleGoalAdvance } from '@/server/services/goal/scheduler';+import {+ HeteroOperationPrincipalError,+ resolveActiveHeteroOperationPrincipal,+} from '@/server/services/heterogeneousAgent/operationPrincipal';++import { assertWorkspaceRowManageable } from '../_helpers/assertWorkspaceRowManageable';+import { goalWriteProcedure, idInput, mapGoalError } from '../goalProcedures';++/**+ * A wrap-up report submitted from a run. The metadata passes through as a+ * record: `GoalReportStore.submit` parses it with the report schema and checks+ * every reference against the Goal graph.+ */+const reportInput = idInput.extend({+ operationId: z.string().min(1),+ report: z.object({+ content: z.string(),+ metadata: z.record(z.string(), z.unknown()),+ }),+});++export const planningProcedures = {+ // A plan is an operation result; the turn token further restricts ingestion to its Goal.+ submitOperationPlan: heteroAuthedProcedure+ .use(serverDatabase)+ .input(+ idInput.extend({+ token: z.string().min(1),+ operationId: z.string().min(1),+ plan: goalPlanSchema,+ }),+ )+ .mutation(async ({ ctx, input }) => {+ if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'hetero:ingest',+ claims: ctx.heteroOperation,+ db: ctx.serverDB,+ operationId: input.operationId,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code:+ error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ const data = await new GoalManagerService(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).submit(input.id, input.token, input.operationId, input.plan);+ await scheduleGoalAdvance({+ goalId: input.id,+ userId: principal.userId,+ workspaceId: principal.workspaceId,+ });+ return { data, success: true };+ }),+ // The wrap-up report of a heterogeneous agent's run: server tools never reach+ // a device run, so its report arrives through the CLI instead of the tool.+ submitOperationReport: heteroAuthedProcedure+ .use(serverDatabase)+ .input(reportInput)+ .mutation(async ({ ctx, input }) => {+ if (ctx.heteroAuthKind !== 'operation' || !ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'hetero:ingest',+ claims: ctx.heteroOperation,+ db: ctx.serverDB,+ operationId: input.operationId,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code:+ error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ const data = await new GoalReportStore(+ ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ).submitFromOperation(input.id, input.report as SubmitGoalReportInput, input.operationId);+ return { data, success: true };+ }),+ submitReport: goalWriteProcedure.input(reportInput).mutation(async ({ ctx, input }) => {+ const data = await new GoalReportStore(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ ).submitFromOperation(input.id, input.report as SubmitGoalReportInput, input.operationId);+ return { data, success: true };+ }),+ submitPlan: goalWriteProcedure+ .input(+ idInput.extend({+ token: z.string().min(1),+ operationId: z.string().min(1),+ plan: goalPlanSchema,+ }),+ )+ .mutation(async ({ ctx, input }) => {+ const data = await new GoalManagerService(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ ).submit(input.id, input.token, input.operationId, input.plan);+ await scheduleGoalAdvance({+ goalId: input.id,+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, success: true };+ }),+ /**+ * Answer a goal's clarification round at once. One advance follows all the+ * answers, so the re-plan never starts with half of them.+ */+ answerClarifications: goalWriteProcedure+ .input(+ idInput.extend({+ answers: z+ .array(+ z.object({+ decisionId: z.string().uuid(),+ optionId: z.string(),+ resolution: z.string().optional(),+ }),+ )+ .min(1),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ await ctx.goalService.answerClarifications(input.id, input.answers);+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'decide',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { message: 'Clarifications answered', success: true };+ } catch (error) {+ mapGoalError(error, 'answer clarifications for');+ }+ }),+ decide: goalWriteProcedure+ .input(+ idInput.extend({+ decisionId: z.string().uuid(),+ optionId: z.string(),+ resolution: z.string().optional(),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ const data = await ctx.goalService.decide(+ input.id,+ input.decisionId,+ input.optionId,+ input.resolution,+ );+ // Answering the gate is what unblocks the Task; carry on from here.+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'decide',+ userId: ctx.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: 'Decision resolved', success: true };+ } catch (error) {+ mapGoalError(error, 'decide');+ }+ }),+ retireNodes: goalWriteProcedure+ .input(+ idInput.extend({+ nodeIds: z.array(z.string().uuid()).min(1),+ reason: z.string().optional(),+ }),+ )+ .mutation(async ({ ctx, input }) => {+ try {+ // Same ownership rule as restart/delete: retiring cancels the nodes'+ // Tasks and recovery gates, so visibility is not manageability.+ const goal = await ctx.goalModel.findById(input.id);+ if (!goal) throw new TRPCError({ code: 'NOT_FOUND', message: 'Goal not found' });+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal');++ const data = await ctx.goalService.retireNodes(input.id, input.nodeIds, input.reason);+ // Retiring can unpark the goal or clear the last unfinished task. Advance+ // as the goal's owner: a workspace owner retiring a colleague's nodes+ // cannot see that colleague's private Tasks, so an advance under the+ // caller would read their bound nodes as `missing_task`.+ await scheduleGoalAdvance({+ goalId: input.id,+ trigger: 'manual',+ userId: goal.userId,+ workspaceId: ctx.workspaceId ?? undefined,+ });+ return { data, message: `Retired ${data.retiredNodeIds.length} node(s)`, success: true };+ } catch (error) {+ mapGoalError(error, 'retireNodes');+ }+ }),+};diff --git a/apps/server/src/routers/lambda/goal/reads.ts b/apps/server/src/routers/lambda/goal/reads.tsnew file mode 100644index 00000000000..3c76b4f5a3b--- /dev/null+++ b/apps/server/src/routers/lambda/goal/reads.ts@@ -0,0 +1,276 @@+import { goalStatuses } from '@lobechat/const/goal';+import { summarizeGoalSupervision } from '@lobechat/types';+import { TRPCError } from '@trpc/server';+import { z } from 'zod';++import { heteroAuthedProcedure } from '@/libs/trpc/lambda';+import { serverDatabase } from '@/libs/trpc/lambda/middleware';+import { GoalService } from '@/server/services/goal';+import { graphForClient } from '@/server/services/goal/graphCompat';+import { GoalManagerService, goalPlanSchema } from '@/server/services/goal/manager';+import {+ HeteroOperationPrincipalError,+ resolveActiveHeteroOperationPrincipal,+} from '@/server/services/heterogeneousAgent/operationPrincipal';++import { goalProcedure, idInput, mapGoalError } from '../goalProcedures';++/** `<ISO>|<event id>` β opaque to callers, so the pair can change shape later. */+const encodeEventCursor = (cursor: { createdAt: Date; id: string }) =>+ `${cursor.createdAt.toISOString()}|${cursor.id}`;++const decodeEventCursor = (raw: string) => {+ const separator = raw.lastIndexOf('|');+ const createdAt = separator > 0 ? new Date(raw.slice(0, separator)) : new Date(Number.NaN);+ const id = separator > 0 ? raw.slice(separator + 1) : '';+ if (Number.isNaN(createdAt.getTime()) || !id) {+ throw new TRPCError({ code: 'BAD_REQUEST', message: 'Invalid events cursor' });+ }+ return { createdAt, id };+};++/**+ * Events paging is cursor-based, not offset-based: the feed is live and events+ * are PREPENDED while a goal runs, so an offset taken on page one re-serves a row+ * that a later insert pushed down.+ */+const eventsInput = idInput.extend({+ cursor: z.string().min(1).optional(),+ entityType: z.enum(['goal', 'node', 'edge', 'decision', 'task']).optional(),+ eventType: z+ .enum([+ 'created',+ 'updated',+ 'activated',+ 'resolved',+ 'rejected',+ 'retired',+ 'linked',+ 'unlinked',+ ])+ .optional(),+ limit: z.number().int().min(1).max(200).optional(),+});++const planContextInput = idInput.extend({+ operationId: z.string().min(1).optional(),+ // A refused `lh goal plan` re-reads this with the plan it just submitted, so+ // the plan-specific guards it was refused on (`existing_work`,+ // `acceptance_escalate_only`, `wait_unsettled`, `wait_until_past`) are+ // re-evaluated. Without it the read would report `ok` and explain nothing.+ plan: goalPlanSchema.optional(),+ token: z.string().min(1).optional(),+});++type EventsInput = z.infer<typeof eventsInput>;++type PlanContextInput = z.infer<typeof planContextInput>;++/** Shared by the ordinary and the operation-authenticated read; the caller builds the manager. */+const readPlanContext = (manager: GoalManagerService, input: PlanContextInput) =>+ manager.admission(input.id, {+ operationId: input.operationId,+ plan: input.plan,+ token: input.token,+ });++const readEvents = async (goalService: GoalService, input: EventsInput) => {+ const page = await goalService.listEvents(input.id, {+ cursor: input.cursor ? decodeEventCursor(input.cursor) : undefined,+ entityType: input.entityType,+ eventType: input.eventType,+ limit: input.limit ?? 50,+ });+ return {+ events: page.events,+ nextCursor: page.nextCursor ? encodeEventCursor(page.nextCursor) : null,+ };+};++/**+ * The Goal reads a device / gateway planning run needs.+ *+ * Those runs hold a `hetero-operation` JWT, and `oidcAuth` deliberately refuses+ * it on ordinary authed routes so a leaked sandbox token cannot be replayed+ * elsewhere β which left a planner unable to read the very Goal it plans, and+ * made `lh goal state` (and the refusal explanation) unauthorized in exactly the+ * runs that need them. These mirror the ordinary reads for that caller only:+ * writes stay on `goalWriteProcedure`, and scope comes from the operation's own+ * principal.+ *+ * The capability is `hetero:ingest` rather than `goal:manage` because the token+ * a takeover planning turn holds never carries `goal:manage` β that is granted+ * only to the `/goal` message that creates a goal, so requiring it would leave+ * the planner locked out. `hetero:ingest` is what the same token already uses to+ * submit its plan.+ *+ * `allowTerminalOperation` is deliberately unset: reads of a run that has already+ * ended are an ordinary-session job, so the token works exactly while its turn+ * runs.+ *+ * Scope is the operation's principal AND its Goal: every endpoint calls+ * `GoalManagerService.assertOperationOwnsTurn`, so a token reaches exactly the+ * Goal whose current turn it holds β not any Goal in the workspace whose id it+ * learns.+ */+const goalOperationReadProcedure = heteroAuthedProcedure.use(serverDatabase).use(async (opts) => {+ if (opts.ctx.heteroAuthKind !== 'operation' || !opts.ctx.heteroOperation) {+ throw new TRPCError({+ code: 'UNAUTHORIZED',+ message: 'An operation-bound token is required',+ });+ }+ let principal;+ try {+ principal = await resolveActiveHeteroOperationPrincipal({+ capability: 'hetero:ingest',+ claims: opts.ctx.heteroOperation,+ db: opts.ctx.serverDB,+ operationId: opts.ctx.heteroOperation.operation_id,+ });+ } catch (error) {+ if (!(error instanceof HeteroOperationPrincipalError)) throw error;+ throw new TRPCError({+ cause: error,+ code: error.status === 401 ? 'UNAUTHORIZED' : error.status === 409 ? 'CONFLICT' : 'FORBIDDEN',+ message: error.message,+ });+ }+ return opts.next({+ ctx: {+ goalManager: new GoalManagerService(+ opts.ctx.serverDB,+ principal.userId,+ principal.workspaceId,+ ),+ goalService: new GoalService(opts.ctx.serverDB, principal.userId, principal.workspaceId),+ // The operation the token was minted for β not an id the caller supplies.+ heteroOperationId: principal.operationId,+ userId: principal.userId,+ workspaceId: principal.workspaceId ?? null,+ },+ });+});++export const readProcedures = {+ /** Clarifications still waiting on the user, grouped by goal. */+ pendingForIsland: goalProcedure.query(async ({ ctx }) => {+ try {+ return { data: await ctx.goalService.pendingForIsland(), success: true };+ } catch (error) {+ mapGoalError(error, 'list pending decisions for');+ }+ }),+ pendingClarifications: goalProcedure.query(async ({ ctx }) => {+ try {+ return { data: await ctx.goalService.pendingClarifications(), success: true };+ } catch (error) {+ mapGoalError(error, 'list clarifications for');+ }+ }),+ graph: goalProcedure+ .input(+ idInput.extend({+ /** Graph features this client renders (see `GOAL_GRAPH_CAPABILITY`). */+ capabilities: z.array(z.string()).max(20).optional(),+ }),+ )+ .query(async ({ ctx, input }) => {+ try {+ return {+ data: graphForClient(await ctx.goalService.graph(input.id), input.capabilities),+ success: true,+ };+ } catch (error) {+ mapGoalError(error, 'graph');+ }+ }),+ /**+ * Why `submitPlan` would accept or refuse a plan right now, without writing.+ *+ * `lh goal state` renders it and a refused `lh goal plan` re-reads it, so the+ * CLI explains a refusal from the same predicates the write path uses β the+ * manager and review snapshots a client cannot see from the graph alone.+ */+ planContext: goalProcedure.input(planContextInput).query(async ({ ctx, input }) => {+ try {+ const manager = new GoalManagerService(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ );+ return { data: await readPlanContext(manager, input), success: true };+ } catch (error) {+ mapGoalError(error, 'read the plan context of');+ }+ }),+ /**+ * The goal's audit trail, newest first, paged past the capped copy `graph`+ * bundles. `lh goal events` reads it.+ */+ events: goalProcedure.input(eventsInput).query(async ({ ctx, input }) => {+ try {+ return { data: await readEvents(ctx.goalService, input), success: true };+ } catch (error) {+ mapGoalError(error, 'list events of');+ }+ }),++ // The same three reads for a planning run that holds an operation token, which+ // the ordinary `goalProcedure` above refuses β see `goalOperationReadProcedure`.+ graphOperation: goalOperationReadProcedure.input(idInput).query(async ({ ctx, input }) => {+ try {+ await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);+ return { data: await ctx.goalService.graph(input.id), success: true };+ } catch (error) {+ mapGoalError(error, 'graph');+ }+ }),+ planContextOperation: goalOperationReadProcedure+ .input(planContextInput)+ .query(async ({ ctx, input }) => {+ try {+ await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);+ return { data: await readPlanContext(ctx.goalManager, input), success: true };+ } catch (error) {+ mapGoalError(error, 'read the plan context of');+ }+ }),+ eventsOperation: goalOperationReadProcedure.input(eventsInput).query(async ({ ctx, input }) => {+ try {+ await ctx.goalManager.assertOperationOwnsTurn(input.id, ctx.heteroOperationId);+ return { data: await readEvents(ctx.goalService, input), success: true };+ } catch (error) {+ mapGoalError(error, 'list events of');+ }+ }),+ /**+ * List goals with their graph roll-up: how many Tasks are done, how many+ * decision gates wait on a human, and what the exploration has cost.+ */+ list: goalProcedure+ .input(+ z.object({+ agentId: z.string().optional(),+ limit: z.number().optional(),+ offset: z.number().optional(),+ projectId: z.string().optional(),+ statuses: z.array(z.enum(goalStatuses)).optional(),+ /** Goals created from this conversation. */+ topicId: z.string().optional(),+ }),+ )+ .query(async ({ input, ctx }) => ctx.goalModel.list(input)),+ supervision: goalProcedure.input(idInput).query(async ({ ctx, input }) => {+ const graph = await ctx.goalService.graph(input.id);+ const state = graph.goal.config?.supervisorState;+ return {+ data: {+ enabled: graph.goal.config?.supervision?.enabled ?? false,+ state,+ summary: summarizeGoalSupervision(state),+ },+ success: true,+ };+ }),+};