The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(agent): no approval card for a call that cannot run
purple67ccb3d58c48
diff --git a/src/api/mcp/__tests__/no-card-for-a-call-that-cannot-run.test.ts b/src/api/mcp/__tests__/no-card-for-a-call-that-cannot-run.test.tsnew file mode 100644index 00000000..caf46eb7--- /dev/null+++ b/src/api/mcp/__tests__/no-card-for-a-call-that-cannot-run.test.ts@@ -0,0 +1,87 @@+/**+ * 3.0.4 box run (Gegenprobe 4): the Agent asked to write+ * C:\Users\user\Desktop\poem.txt, the user approved, and the call failed right+ * after with "Path escapes the allowed workspace". A card for a call the guard+ * refuses anyway is a question without a choice. Same for plain-chat artifact+ * mode, where file_write never touches the disk.+ *+ * Run: npx vitest run src/api/mcp/__tests__/no-card-for-a-call-that-cannot-run.test.ts+ */+import { describe, it, expect, vi, beforeEach } from 'vitest'+import { readFileSync } from 'node:fs'+import { resolve, dirname } from 'node:path'+import { fileURLToPath } from 'node:url'++const calls: { cmd: string; body: Record<string, unknown> }[] = []+let fsInfo: () => unknown = () => ({ isDir: false })++vi.mock('../../backend', () => ({+ backendCall: vi.fn(async (cmd: string, body: Record<string, unknown>) => {+ calls.push({ cmd, body })+ if (cmd === 'fs_info') return fsInfo()+ return { ok: true }+ }),+ fetchExternal: vi.fn(),+}))+vi.mock('../../agents/sub-agent', () => ({+ DELEGATE_TASK_TOOL_DEF: { name: 'delegate_task', description: '', category: 'system', inputSchema: {} },+ buildDelegateExecutor: () => async () => 'stub',+}))+vi.mock('../../../lib/workflow-engine', () => ({ WorkflowEngine: class {} }))++import { approvalIsMoot } from '../builtin-tools'+import { beginAgentRun, endAgentRun } from '../../agent-context'++const escapes = () => { throw new Error('Path escapes the allowed workspace') }++beforeEach(() => {+ calls.length = 0+ fsInfo = () => ({ isDir: false })+})++describe('a call the workspace guard refuses gets no card', () => {+ it('file_write outside the folder: no card, and the check went through the guard with this chat', async () => {+ fsInfo = escapes+ const run = beginAgentRun({ chatId: 'chat-1', conversationId: 'c1' })+ try {+ expect(await approvalIsMoot('file_write', { path: 'C:\\Users\\user\\Desktop\\poem.txt', content: 'x' }, run)).toBe(true)+ expect(calls).toEqual([{ cmd: 'fs_info', body: { path: 'C:\\Users\\user\\Desktop\\poem.txt', chatId: 'chat-1' } }])+ } finally { endAgentRun(run) }+ })++ it('NEGATIVE CONTROL: a path inside the folder keeps its card, existing or not', async () => {+ const run = beginAgentRun({ chatId: 'chat-1', conversationId: 'c1' })+ try {+ expect(await approvalIsMoot('file_write', { path: 'poem.txt', content: 'x' }, run)).toBe(false)+ fsInfo = () => { throw new Error('Path not found: /ws/poem.txt') }+ expect(await approvalIsMoot('file_write', { path: 'poem.txt', content: 'x' }, run)).toBe(false)+ fsInfo = () => { throw new Error('backend offline') }+ expect(await approvalIsMoot('file_edit', { path: 'a.ts' }, run)).toBe(false)+ } finally { endAgentRun(run) }+ })++ it('tools without a workspace path are never checked', async () => {+ fsInfo = escapes+ expect(await approvalIsMoot('shell_execute', { command: 'rm -rf /' })).toBe(false)+ expect(await approvalIsMoot('web_fetch', { url: 'https://x' })).toBe(false)+ expect(calls).toEqual([])+ })+})++describe('plain-chat artifact mode', () => {+ it('file_write there only shows a preview, so there is nothing to approve', async () => {+ const run = beginAgentRun({ chatId: 'chat-2', conversationId: 'c2', artifactMode: true })+ try {+ expect(await approvalIsMoot('file_write', { path: 'notes.md', content: 'x' }, run)).toBe(true)+ expect(calls).toEqual([])+ } finally { endAgentRun(run) }+ })+})++describe('wiring', () => {+ it('useAgentChat asks approvalIsMoot before it puts a card up', () => {+ const here = dirname(fileURLToPath(import.meta.url))+ const agent = readFileSync(resolve(here, '../../../hooks/useAgentChat.ts'), 'utf8')+ expect(agent).toContain("&& !(await approvalIsMoot(tc.function.name, tc.function.arguments ?? {}, run))")+ })+})diff --git a/src/api/mcp/builtin-tools.ts b/src/api/mcp/builtin-tools.tsindex 12b177d8..984ba711 100644--- a/src/api/mcp/builtin-tools.ts+++ b/src/api/mcp/builtin-tools.ts@@ -54,6 +54,35 @@ function chatCtx(run?: AgentRunContext): { chatId?: string; workingDirectory?: sreturn { chatId: id }}+/** The file tools whose `path` the workspace guard decides on. */+const WORKSPACE_PATH_TOOLS = new Set(['file_read', 'file_write', 'file_edit', 'file_list', 'file_search'])++/**+ * Whether asking the user about this call makes sense at all.+ *+ * Two cases where it does not, both from the 3.0.4 box run:+ * - plain-chat artifact mode: file_write only shows a preview with a Download+ * button, nothing touches the disk, so there is nothing to approve;+ * - a path the workspace guard refuses: the user approved+ * `C:\Users\user\Desktop\poem.txt` and got "Path escapes the allowed+ * workspace" right after. The guard (resolve_path, via fs_info) answers+ * before the card, the call runs straight into the refusal and the model+ * retries inside the folder.+ * Any other answer from the check (missing file, backend error) keeps the card.+ */+export async function approvalIsMoot(toolName: string, args: ToolArgs, run?: AgentRunContext): Promise<boolean> {+ if (toolName === 'file_write' && isChatArtifactMode(run)) return true+ if (!WORKSPACE_PATH_TOOLS.has(toolName)) return false+ const path = typeof args.path === 'string' ? args.path : ''+ if (!path.trim()) return false+ try {+ await backendCall('fs_info', { path, ...chatCtx(run) })+ return false+ } catch (e) {+ return /escapes the allowed workspace/i.test(e instanceof Error ? e.message : String(e))+ }+}+/*** Argument readers.*diff --git a/src/hooks/__tests__/agent-shell-gate.test.ts b/src/hooks/__tests__/agent-shell-gate.test.tsindex c6ea4e74..f0520d12 100644--- a/src/hooks/__tests__/agent-shell-gate.test.ts+++ b/src/hooks/__tests__/agent-shell-gate.test.ts@@ -77,7 +77,7 @@ describe('the opt-in, for the user who wants the cloud confirm back', () => {describe('wiring in useAgentChat', () => {it('the cloud arm rides ON TOP of the permission level, never replaces it', () => {- expect(agent).toContain("const needsApproval = permLevel !== 'auto' || cloudShellConfirm")+ expect(agent).toContain("const needsApproval = (permLevel !== 'auto' || cloudShellConfirm)")})it('the gate reads the SAME shared helper and setting as the Code tab', () => {diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.tsindex 2928cc5d..581e6f86 100644--- a/src/hooks/useAgentChat.ts+++ b/src/hooks/useAgentChat.ts@@ -53,6 +53,7 @@ import { sendsToALanBackend } from '../lib/lan-openai-slot'import { useSendSizeStore } from '../stores/sendSizeStore'import { resolveAgentNumCtx } from '../lib/agent-num-ctx'import { ensureBuiltinAgentCtx } from '../api/builtin-ensure'+import { approvalIsMoot } from '../api/mcp/builtin-tools'import { useMemoryStore } from '../stores/memoryStore'import { useVoiceStore } from '../stores/voiceStore'import { autoSpeak } from '../lib/ttsBridge'@@ -2413,7 +2414,8 @@ export function useAgentChat() {cloudOptIn: settings.codexCloudConfirmOptIn,providerId,})- const needsApproval = permLevel !== 'auto' || cloudShellConfirm+ const needsApproval = (permLevel !== 'auto' || cloudShellConfirm)+ && !(await approvalIsMoot(tc.function.name, tc.function.arguments ?? {}, run))// Exec-timeout parity with the Code tab (audit B8): without an// injected default the Rust side used its 120 s fallback, so the// same npm install that passed in Code died here after 2 minutes.