🤯 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
🐛 fix(goal): grant goal:manage to runs the user sent, not only /goal (#22726)
Arvin Xu608b71d17e44
diff --git a/apps/server/src/routers/lambda/goal/reads.ts b/apps/server/src/routers/lambda/goal/reads.tsindex 68201834110..db497f6a54c 100644--- a/apps/server/src/routers/lambda/goal/reads.ts+++ b/apps/server/src/routers/lambda/goal/reads.ts@@ -100,7 +100,7 @@ const readEvents = async (goalService: GoalService, input: EventsInput) => {** 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+ * only to runs the user sent and `/goal` runs, so requiring it would leave* the planner locked out. `hetero:ingest` is what the same token already uses to* submit its plan.*diff --git a/apps/server/src/services/aiAgent/pipeline/heteroDispatch/index.branches.test.ts b/apps/server/src/services/aiAgent/pipeline/heteroDispatch/index.branches.test.tsindex c7ce7318e00..c48860e1fb4 100644--- a/apps/server/src/services/aiAgent/pipeline/heteroDispatch/index.branches.test.ts+++ b/apps/server/src/services/aiAgent/pipeline/heteroDispatch/index.branches.test.ts@@ -237,6 +237,54 @@ describe('Channel history context', () => {);});+describe('goal management capability', () => {+ it.each([+ // The user typed this turn, so "run a goal" works without the `/goal` prefix.+ ['a chat turn the user sent', { trigger: 'chat' }, true],+ ['a run the user scheduled', { trigger: 'scheduled', userMessageId: undefined }, true],+ // A server-started `chat` delivery (background results) writes no user row.+ ['a chat delivery with no user turn', { trigger: 'chat', userMessageId: undefined }, false],+ // Goal planning and supervision turns carry no trigger: no goal spawns goals.+ ['a goal planning turn', { trigger: undefined }, false],+ ['a bot message', { trigger: 'bot' }, false],+ ['an inbox wakeup', { trigger: 'inbox' }, false],+ ])('for %s: %s', async (_, overrides, granted) => {+ const { deps } = createDeps();+ await dispatchHeteroAgent(+ deps,+ createCtx(+ {+ boundDeviceId: 'dev-1',+ executionTarget: 'device',+ heterogeneousProvider: { type: 'claude-code' },+ },+ { prompt: 'run a goal to polish the mailbox', ...overrides },+ ),+ createInput('claude-code'),+ );+ const signed = mocks.signHeteroOperationJWT.mock.calls[0][0];+ expect(signed.capabilities.includes('goal:manage')).toBe(granted);+ });++ it('keeps it out of a Task run the user did not type', async () => {+ const { deps } = createDeps();+ await dispatchHeteroAgent(+ deps,+ createCtx(+ {+ boundDeviceId: 'dev-1',+ executionTarget: 'device',+ heterogeneousProvider: { type: 'claude-code' },+ },+ { prompt: 'Implement this step of the goal', trigger: 'chat' },+ ),+ createInput('claude-code', { operationTaskId: 'task_1' }),+ );+ const signed = mocks.signHeteroOperationJWT.mock.calls[0][0];+ expect(signed.capabilities).not.toContain('goal:manage');+ });+});+describe('dispatchHeteroAgent — preparation failures', () => {it('fails before dispatch when the operation JWT cannot be signed', async () => {mocks.signHeteroOperationJWT.mockRejectedValueOnce(new Error('no key'));diff --git a/apps/server/src/services/aiAgent/pipeline/heteroDispatch/prepare.ts b/apps/server/src/services/aiAgent/pipeline/heteroDispatch/prepare.tsindex f7cb93c5f90..0ba52852b2d 100644--- a/apps/server/src/services/aiAgent/pipeline/heteroDispatch/prepare.ts+++ b/apps/server/src/services/aiAgent/pipeline/heteroDispatch/prepare.ts@@ -10,7 +10,11 @@ import {formatContextSelections,formatPageSelections,} from '@lobechat/prompts';-import { applyTopicModelToHeterogeneousProvider, buildHeteroExecArgs } from '@lobechat/types';+import {+ applyTopicModelToHeterogeneousProvider,+ buildHeteroExecArgs,+ RequestTrigger,+} from '@lobechat/types';import debug from 'debug';import { signHeteroOperationJWT } from '@/libs/trpc/utils/internalJwt';@@ -47,6 +51,7 @@ export const prepareHeteroRun = async (| 'prompt'| 'shareGate'| 'topicId'+ | 'userMessageId'| 'userMessageMetadata'| 'trigger'>,@@ -83,13 +88,18 @@ export const prepareHeteroRun = async (let operationJwt: string;try {operationJwt = await signHeteroOperationJWT({- // A `/goal` run also gets `goal:manage` so it can create the goal the agent- // supervises; the server still derives the agent and topic from this- // operation, never from the CLI. A Task run also gets `acceptance:evidence`- // for its own verification round.+ // A run the user sent also gets `goal:manage` so it can create+ // the goal the agent supervises; the server still derives the agent and+ // topic from this operation, never from the CLI. A Task run also gets+ // `acceptance:evidence` for its own verification round.capabilities: heteroOperationCapabilities(prompt, {channel: isChannelOperation,taskId: operationTaskId,+ // A scheduled run replays the user's own message off history, so it has+ // no fresh user row; server-started `chat` deliveries have none either.+ userTurn:+ (ctx.trigger === RequestTrigger.Chat && !!ctx.userMessageId) ||+ ctx.trigger === RequestTrigger.Scheduled,}),operationId,userId: deps.userId,diff --git a/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.test.ts b/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.test.tsindex 3214989a9f0..e01893ea007 100644--- a/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.test.ts+++ b/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.test.ts@@ -13,9 +13,33 @@ describe('heteroOperationCapabilities', () => {expect(heteroOperationCapabilities('/goal ship the report')).toContain('goal:manage');});+ it('grants goal management to a turn the user sent, however they word it', () => {+ expect(+ heteroOperationCapabilities('你能否跑一个goal,把邮箱体验补齐', { userTurn: true }),+ ).toContain('goal:manage');+ });++ it('keeps goal management out of user-looking Task and Channel runs', () => {+ // A Task brief is generated and a Channel message can come from anyone, so+ // neither is the owner asking; a Task run creating goals would let goals nest.+ const options = { userTurn: true };+ expect(+ heteroOperationCapabilities('run a goal', { ...options, taskId: 'task_1' }),+ ).not.toContain('goal:manage');+ expect(heteroOperationCapabilities('run a goal', { ...options, channel: true })).not.toContain(+ 'goal:manage',+ );+ });++ it('still honours an explicit /goal on a Task run', () => {+ expect(heteroOperationCapabilities('/goal ship it', { taskId: 'task_1' })).toContain(+ 'goal:manage',+ );+ });+it('keeps goal management out of an ordinary run', () => {- // Every hetero run used to carry it, so text met during a normal coding run- // could drive `lh goal create --conversation` and start uncapped work.+ // Every hetero run used to carry it, so a server-started run (Task, goal+ // planning, wakeups) could drive `lh goal create` and start uncapped work.expect(heteroOperationCapabilities('fix the build')).toEqual(['hetero:ingest','hetero:finish',diff --git a/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.ts b/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.tsindex 38ceff3f1a6..a07433a8c9b 100644--- a/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.ts+++ b/apps/server/src/services/aiAgent/pipeline/heteroOperationCapabilities.ts@@ -8,9 +8,14 @@ import type { HeteroOperationCapability } from '@/libs/trpc/utils/internalJwt';* `goal:manage` lets the CLI create a goal the agent supervises from this* conversation (`lh goal create --conversation`), or bind an existing one to it* (`lh goal bind-topic`, which the `/goal` instructions route a named goal to). The token is exposed to the- * always-installed CLI, so it is granted only when the user asked for a goal- * with `/goal`: an ordinary coding run that meets instructions telling it to- * run that command must not be able to launch persistent follow-on work.+ * always-installed CLI, so it follows who started the run, not what it says:+ * a turn the user sent (a chat message, or one they scheduled) gets it however+ * they word the ask, since users say "run a goal" as often as they type `/goal`.+ * Runs the server starts on its own — Task runs, goal planning and supervision+ * turns, background-result deliveries, cron, inbox and SCM wakeups — and+ * Channel or bot messages, whose sender may not be the owner, get it only from+ * an explicit `/goal`. That keeps a goal from spawning goals of its own. A goal+ * a user-sent run creates binds to that user's topic, where its tray shows it.** `acceptance:evidence` lets a Task run file evidence into its own verification* round (`lh acceptance run result submit|ingest --operation`). That round is@@ -22,12 +27,19 @@ import type { HeteroOperationCapability } from '@/libs/trpc/utils/internalJwt';*/export const heteroOperationCapabilities = (prompt: unknown,- options: { channel?: boolean; taskId?: string } = {},+ options: {+ channel?: boolean;+ taskId?: string;+ /** The run answers a message the user sent: a chat turn or one they scheduled. */+ userTurn?: boolean;+ } = {},): HeteroOperationCapability[] => ['hetero:ingest',...(options.channel ? (['channel:read'] as const) : []),'hetero:finish','hetero:intervention:read',- ...(isGoalPrompt(prompt) ? (['goal:manage'] as const) : []),+ ...(isGoalPrompt(prompt) || (options.userTurn && !options.channel && !options.taskId)+ ? (['goal:manage'] as const)+ : []),...(options.taskId ? (['acceptance:evidence'] as const) : []),];diff --git a/apps/server/src/services/goal/manager.README.md b/apps/server/src/services/goal/manager.README.mdindex 1ce12d6bf17..44a8c200d07 100644--- a/apps/server/src/services/goal/manager.README.md+++ b/apps/server/src/services/goal/manager.README.md@@ -25,7 +25,7 @@ needs `--force`; the previous topic joins `previousTopicIds`, and the move isrecorded as a goal event. Finished goals and goals with a planning turn in flightelsewhere are refused. Like `create --topic`, the topic comes fromthe run's operation; an operation-token run (device or gateway) needs the-`goal:manage` capability that `/goal` grants.+`goal:manage` capability that a user-sent run (or any `/goal` run) carries.A topic goal (`create --topic`, `/goal`) opens as a `draft` when the CLI asksfor one (`draft: true`, sent by every CLI that has `lh goal start`; an older CLIdiff --git a/packages/builtin-tool-goal/src/conversationGoalPrompt.test.ts b/packages/builtin-tool-goal/src/conversationGoalPrompt.test.tsindex 35064b31536..b3f7b178f49 100644--- a/packages/builtin-tool-goal/src/conversationGoalPrompt.test.ts+++ b/packages/builtin-tool-goal/src/conversationGoalPrompt.test.ts@@ -54,8 +54,8 @@ describe('withConversationGoalPrompt', () => {});it('binds a goal the user names instead of creating a duplicate', () => {- // `goal:manage` reaches a device or gateway run only through `/goal`, so- // these instructions are the only way such a run can bind an existing goal.+ // `goal:manage` reaches a device or gateway run only from a user-sent or+ // `/goal` turn, and these instructions tell it to bind an existing one.expect(conversationGoalPrompt).toContain('lh goal bind-topic <goalId> --json');expect(conversationGoalPrompt).toContain('do not create a new goal');expect(conversationGoalPrompt).toContain('only allow creating or binding this goal');diff --git a/packages/builtin-tool-goal/src/conversationGoalPrompt.ts b/packages/builtin-tool-goal/src/conversationGoalPrompt.tsindex 6c8f500f790..595881d6876 100644--- a/packages/builtin-tool-goal/src/conversationGoalPrompt.ts+++ b/packages/builtin-tool-goal/src/conversationGoalPrompt.ts@@ -10,7 +10,7 @@ import { isGoalPrompt } from './goalPrompt';* which the server adopts as the goal's first planning turn. A `/goal` that* names an existing goal binds it here (`lh goal bind-topic`) instead: the* `goal:manage` capability a device or gateway run needs for that is granted- * only to `/goal` runs, so this is the bind's only entry for them.+ * only to runs the user sent (or any `/goal` run), which covers this one.*/export const conversationGoalPrompt = `The user sent this request with LobeHub's /goal command (the prefix was removed before it reached you; it is not your own /goal command). They are asking you to turn the request into a long-running LobeHub Goal that you supervise from this conversation, not to do the work yourself in this run.