Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(settings): confirm full access before saving new task default

zqchris5919eef8107f
diff --git a/apps/desktop/src/renderer/components/settings/NewChatDefaultPermissionSection.tsx b/apps/desktop/src/renderer/components/settings/NewChatDefaultPermissionSection.tsx
index 9d7ec4a5a4..aa0432a1b6 100644
--- a/apps/desktop/src/renderer/components/settings/NewChatDefaultPermissionSection.tsx
+++ b/apps/desktop/src/renderer/components/settings/NewChatDefaultPermissionSection.tsx
@@ -1,5 +1,11 @@
import { useTranslation } from 'react-i18next';
+import { TriangleAlert } from 'lucide-react';
+import { requiresFullAccessConfirmation } from '@cindy/maker-shared/permission-mode';
+import { FullAccessConfirmContent } from '@/components/new-chat/FullAccessConfirmContent';
import { PermissionSelector } from '@/components/new-chat/PermissionSelector';
+import { useConfirmDialog } from '@/components/ui/confirm-dialog-provider';
+import { getDataOwnerGeneration, isDataOwnerGenerationCurrent } from '@/contexts/dataOwnerGeneration';
+import type { PermissionMode } from '@/lib/userPreferences.types';
import { setNewChatDefaultPermissionMode, useNewMakerDraft } from '@/state/newMakerDraft';
import { DefaultOverrideControls } from './DefaultOverrideControls';
@@ -7,8 +13,26 @@ const DEFAULT_MODES = ['auto', 'bypassPermissions'] as const;
export function NewChatDefaultPermissionSection() {
const { t } = useTranslation();
+ const { confirm } = useConfirmDialog();
const draft = useNewMakerDraft();
const override = draft.newChatDefaultPermissionMode;
+ const changePermissionMode = async (nextMode: PermissionMode) => {
+ const owner = getDataOwnerGeneration();
+ if (requiresFullAccessConfirmation(override ?? 'auto', nextMode)) {
+ const confirmed = await confirm({
+ title: t('newChat.chatInput.fullAccessConfirmation.title'),
+ description: t('newChat.chatInput.fullAccessConfirmation.description'),
+ content: <FullAccessConfirmContent />,
+ describeContent: true,
+ maxWidth: 440,
+ confirmText: t('newChat.chatInput.fullAccessConfirmation.confirm'),
+ cancelText: t('newChat.chatInput.fullAccessConfirmation.cancel'),
+ confirmIcon: <TriangleAlert size={14} />,
+ });
+ if (!confirmed || !isDataOwnerGenerationCurrent(owner)) return;
+ }
+ setNewChatDefaultPermissionMode(nextMode);
+ };
return (
<div className="flex flex-col gap-3">
<div className="flex items-center justify-between gap-3">
@@ -21,7 +45,7 @@ export function NewChatDefaultPermissionSection() {
<PermissionSelector permissionMode={override ?? 'auto'}
vendorKey="cc" triggerVariant="field" allowedModes={DEFAULT_MODES}
ariaContext={t('settings.newChatDefaults.title')}
- onPermissionModeChange={setNewChatDefaultPermissionMode} />
+ onPermissionModeChange={changePermissionMode} />
</div>
);
}
diff --git a/apps/desktop/src/renderer/components/settings/__tests__/NewChatDefaultPermissionSection.test.tsx b/apps/desktop/src/renderer/components/settings/__tests__/NewChatDefaultPermissionSection.test.tsx
new file mode 100644
index 0000000000..2ed366dd9c
--- /dev/null
+++ b/apps/desktop/src/renderer/components/settings/__tests__/NewChatDefaultPermissionSection.test.tsx
@@ -0,0 +1,81 @@
+// @vitest-environment jsdom
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
+
+const state = vi.hoisted(() => ({
+ mode: null as 'auto' | 'bypassPermissions' | null,
+ save: vi.fn(),
+ confirm: vi.fn(),
+}));
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+vi.mock('@/state/newMakerDraft', () => ({
+ useNewMakerDraft: () => ({ newChatDefaultPermissionMode: state.mode }),
+ setNewChatDefaultPermissionMode: state.save,
+}));
+vi.mock('@/components/ui/confirm-dialog-provider', () => ({
+ useConfirmDialog: () => ({ confirm: state.confirm }),
+}));
+vi.mock('@/hooks/useAgentCapabilities', () => ({ useAgentCapabilities: () => ({
+ capabilities: { permissionModes: [
+ { id: 'auto', displayName: 'Auto-review' },
+ { id: 'bypassPermissions', displayName: 'Full access' },
+ ] },
+}) }));
+
+import { NewChatDefaultPermissionSection } from '../NewChatDefaultPermissionSection';
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ state.mode = null;
+ setDataOwnerGeneration('owner-a', 1);
+});
+afterEach(cleanup);
+
+function select(mode: 'auto' | 'bypassPermissions') {
+ fireEvent.click(screen.getByRole('button', { name: /settings.newChatDefaults.title/ }));
+ fireEvent.click(screen.getByRole('option', {
+ name: `newChat.permissionSelector.modes.claude-code.${mode}.label`,
+ }));
+}
+
+// Unit regression #7839: the actual settings picker must retain its value until the shared
+// Full-access confirmation resolves. Store tests cannot exercise this UI authorization boundary.
+it.each([false, true])('saves full access only when confirmation resolves %s', async (accepted) => {
+ let answer!: (value: boolean) => void;
+ state.confirm.mockImplementation(() => new Promise<boolean>(resolve => { answer = resolve; }));
+ render(<NewChatDefaultPermissionSection />);
+ select('bypassPermissions');
+ expect(state.confirm).toHaveBeenCalledOnce();
+ expect(state.save).not.toHaveBeenCalled();
+ const options = state.confirm.mock.calls[0][0];
+ expect(options.describeContent).toBe(true);
+ const content = render(options.content);
+ for (const kind of ['files', 'commands', 'network']) {
+ expect(screen.getByText(`newChat.chatInput.fullAccessConfirmation.items.${kind}.description`)).toBeTruthy();
+ }
+ content.unmount();
+ await act(async () => answer(accepted));
+ if (accepted) expect(state.save).toHaveBeenCalledExactlyOnceWith('bypassPermissions');
+ else expect(state.save).not.toHaveBeenCalled();
+});
+
+it('saves a downgrade and reset without an upgrade confirmation', () => {
+ state.mode = 'bypassPermissions';
+ render(<NewChatDefaultPermissionSection />);
+ select('auto');
+ expect(state.save).toHaveBeenCalledWith('auto');
+ fireEvent.click(screen.getByRole('button', { name: 'settings.defaults.restore' }));
+ expect(state.save).toHaveBeenLastCalledWith(null);
+ expect(state.confirm).not.toHaveBeenCalled();
+});
+
+it('does not apply an old owner’s confirmation to the newly active owner', async () => {
+ let answer!: (value: boolean) => void;
+ state.confirm.mockImplementation(() => new Promise<boolean>(resolve => { answer = resolve; }));
+ render(<NewChatDefaultPermissionSection />);
+ select('bypassPermissions');
+ setDataOwnerGeneration('owner-b', 2);
+ await act(async () => answer(true));
+ expect(state.save).not.toHaveBeenCalled();
+});