Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(cli): make --no-headless actually disable headless approval (#21189)

Arvin Xu5310a6f1d0f2
diff --git a/apps/cli/src/commands/agent.test.ts b/apps/cli/src/commands/agent.test.ts
index ea36183111f..6874a3ce7a9 100644
--- a/apps/cli/src/commands/agent.test.ts
+++ b/apps/cli/src/commands/agent.test.ts
@@ -53,6 +53,9 @@ const { mockTrpcClient } = vi.hoisted(() => ({
device: {
listDevices: { query: vi.fn() },
},
+ user: {
+ getUserState: { query: vi.fn() },
+ },
},
}));
@@ -134,6 +137,16 @@ describe('agent command', () => {
(fn as ReturnType<typeof vi.fn>).mockReset();
}
}
+ for (const method of Object.values(mockTrpcClient.user)) {
+ for (const fn of Object.values(method)) {
+ (fn as ReturnType<typeof vi.fn>).mockReset();
+ }
+ }
+ // `lh agent run --no-headless` reads the saved tool approval mode from
+ // user.getUserState before deciding what to send. After mockReset a bare
+ // vi.fn() returns undefined and `undefined.query(...)` throws before the
+ // CLI's own `.catch()` can absorb it, so default to "no saved setting".
+ mockTrpcClient.user.getUserState.query.mockResolvedValue(undefined);
});
afterEach(async () => {
diff --git a/apps/cli/src/commands/agent/run.test.ts b/apps/cli/src/commands/agent/run.test.ts
index cfca3b639e2..a547070acb8 100644
--- a/apps/cli/src/commands/agent/run.test.ts
+++ b/apps/cli/src/commands/agent/run.test.ts
@@ -3,9 +3,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { registerAgentRunCommand } from './run';
-const { mockExecAgent, mockGetTrpcClient } = vi.hoisted(() => ({
+const { mockExecAgent, mockGetTrpcClient, mockGetUserState } = vi.hoisted(() => ({
mockExecAgent: vi.fn(),
mockGetTrpcClient: vi.fn(),
+ mockGetUserState: vi.fn(),
}));
vi.mock('../../api/client', () => ({ getTrpcClient: mockGetTrpcClient }));
@@ -28,7 +29,11 @@ const runAgent = async (...args: string[]) => {
describe('lh agent run', () => {
beforeEach(() => {
mockExecAgent.mockResolvedValue({ operationId: 'op-1', success: true, topicId: 'tpc-1' });
- mockGetTrpcClient.mockResolvedValue({ aiAgent: { execAgent: { mutate: mockExecAgent } } });
+ mockGetUserState.mockResolvedValue(undefined);
+ mockGetTrpcClient.mockResolvedValue({
+ aiAgent: { execAgent: { mutate: mockExecAgent } },
+ user: { getUserState: { query: mockGetUserState } },
+ });
vi.spyOn(console, 'log').mockImplementation(() => {});
});
@@ -73,4 +78,83 @@ describe('lh agent run', () => {
expect(input).not.toHaveProperty('selectedToolIds');
expect(input).not.toHaveProperty('selectedSkillIds');
});
+
+ it('defaults to headless approval when --no-headless is absent', async () => {
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello');
+
+ expect(input.userInterventionConfig).toEqual({ approvalMode: 'headless' });
+ // The saved user state is only consulted for --no-headless runs.
+ expect(mockGetUserState).not.toHaveBeenCalled();
+ });
+
+ it('--no-headless sends the saved tool approval mode instead of headless', async () => {
+ mockGetUserState.mockResolvedValue({
+ settings: { tool: { humanIntervention: { approvalMode: 'manual' } } },
+ });
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ expect(input.userInterventionConfig).toEqual({ approvalMode: 'manual' });
+ });
+
+ it('--no-headless falls back to manual when the state read fails', async () => {
+ mockGetUserState.mockRejectedValue(new Error('network down'));
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ expect(input.userInterventionConfig).toEqual({ approvalMode: 'manual' });
+ });
+
+ it('--no-headless falls back to manual when no approval mode is saved', async () => {
+ mockGetUserState.mockResolvedValue({ settings: { tool: {} } });
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ expect(input.userInterventionConfig).toEqual({ approvalMode: 'manual' });
+ });
+
+ it('--no-headless keeps the saved allow list in allow-list mode', async () => {
+ mockGetUserState.mockResolvedValue({
+ settings: {
+ tool: {
+ humanIntervention: {
+ allowList: ['lobe-computer-use', 'my-mcp'],
+ approvalMode: 'allow-list',
+ },
+ },
+ },
+ });
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ // GeneralChatAgent defaults an omitted allowList to [], which would park
+ // again every tool the user previously approved.
+ expect(input.userInterventionConfig).toEqual({
+ allowList: ['lobe-computer-use', 'my-mcp'],
+ approvalMode: 'allow-list',
+ });
+ });
+
+ it('--no-headless sends an empty allow list when none is saved in allow-list mode', async () => {
+ mockGetUserState.mockResolvedValue({
+ settings: { tool: { humanIntervention: { approvalMode: 'allow-list' } } },
+ });
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ expect(input.userInterventionConfig).toEqual({ allowList: [], approvalMode: 'allow-list' });
+ });
+
+ it('--no-headless normalizes a persisted headless mode to auto-run', async () => {
+ // 'headless' is a backend-only value in the Web tool-intervention selector;
+ // sending it back would convert intervention-required calls into blocked
+ // results instead of parking them, defeating the explicit flag.
+ mockGetUserState.mockResolvedValue({
+ settings: { tool: { humanIntervention: { approvalMode: 'headless' } } },
+ });
+
+ const input = await runAgent('--agent-id', 'agt-1', '--prompt', 'Hello', '--no-headless');
+
+ expect(input.userInterventionConfig).toEqual({ approvalMode: 'auto-run' });
+ });
});
diff --git a/apps/cli/src/commands/agent/run.ts b/apps/cli/src/commands/agent/run.ts
index 1fb9d330c38..f61eabf9b97 100644
--- a/apps/cli/src/commands/agent/run.ts
+++ b/apps/cli/src/commands/agent/run.ts
@@ -1,5 +1,6 @@
import { readFileSync } from 'node:fs';
+import type { UserInterventionConfig } from '@lobechat/types';
import { type Command, InvalidArgumentError } from 'commander';
import pc from 'picocolors';
@@ -168,6 +169,41 @@ Exit codes:
// (undefined, true) β†’ headless mode is on and tool calls auto-execute.
if (options.headless !== false) {
input.userInterventionConfig = { approvalMode: 'headless' };
+ } else {
+ // --no-headless must actually disable headless mode. Omitting the
+ // config here is not enough: the server falls back to
+ // `{ approvalMode: 'headless' }` for every run that does not carry
+ // one (the default for cron and bot triggers), so an omitted config
+ // re-enables exactly what the flag promised to turn off. Resolve the
+ // user's saved tool approval mode instead, and fall back to `manual`
+ // β€” the server's documented default β€” when the state read fails or
+ // the setting is unset. Tools that need approval then park the run
+ // for a human, as this flag's help text promises.
+ const savedConfig = (
+ (await client.user.getUserState.query().catch(() => undefined)) as
+ | { settings?: { tool?: { humanIntervention?: UserInterventionConfig } } }
+ | undefined
+ )?.settings?.tool?.humanIntervention;
+ // The Web tool-intervention selector treats a persisted 'headless' as
+ // a backend-only value and shows 'auto-run' instead (settings
+ // selectors/toolIntervention.ts). Mirror that here so a legacy or raw
+ // persisted 'headless' can never leak back into a run the user
+ // explicitly asked to supervise with --no-headless β€” in headless mode
+ // the agent converts intervention-required calls into blocked results
+ // instead of parking them for a human.
+ const approvalMode =
+ savedConfig?.approvalMode === 'auto-run' || savedConfig?.approvalMode === 'allow-list'
+ ? savedConfig.approvalMode
+ : savedConfig?.approvalMode === 'headless'
+ ? 'auto-run'
+ : 'manual';
+ // In allow-list mode the saved list must travel with the mode:
+ // GeneralChatAgent defaults an omitted allowList to [], which would
+ // park again every tool the user previously approved.
+ input.userInterventionConfig = {
+ approvalMode,
+ ...(approvalMode === 'allow-list' ? { allowList: savedConfig?.allowList ?? [] } : {}),
+ };
}
const result = await client.aiAgent.execAgent.mutate(input as any);