Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
fix(desktop): Ask Me / Allow All shows the same everywhere
Salman Qureshi3418788bfedd
diff --git a/apps/desktop/src/app/events.ts b/apps/desktop/src/app/events.tsindex bd9a18f0..cb9e794a 100644--- a/apps/desktop/src/app/events.ts+++ b/apps/desktop/src/app/events.ts@@ -94,6 +94,11 @@ export function handleEvent(store: ConversationStore, event: string, raw: stringcase "provider":useRuntimeState.getState().setProvider(String(data.provider ?? ""), String(data.model ?? ""));break;+ case "status":+ // The runtime's live status: a permission change made anywhere (a toggle here, /permissions+ // in a terminal, another device) reaches every toggle on screen.+ if (data.permission === "allow-all" || data.permission === "ask-me") useRuntimeState.getState().setAllowAll(data.permission === "allow-all");+ break;case "thinking":conv.setAgent("thinking");conv.setLive((l) => l);diff --git a/apps/desktop/src/app/permission.test.ts b/apps/desktop/src/app/permission.test.tsnew file mode 100644index 00000000..a3dff6ee--- /dev/null+++ b/apps/desktop/src/app/permission.test.ts@@ -0,0 +1,52 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++/**+ * Ask Me / Allow All is one setting with two toggles on screen (the+ * composer's picker and the status bar). Flipping either, or changing it in+ * a terminal, must show the same mode everywhere.+ */++const updateSettings = vi.fn(async (p: { bypassPermissions: boolean }) => ({ bypassPermissions: p.bypassPermissions, restrictUser: false, workspace: "" }));+const settings = vi.fn(async () => ({ bypassPermissions: false, restrictUser: false, workspace: "" }));+vi.mock("~/api/mercury", () => ({ chat: { settings: () => settings(), updateSettings: (p: { bypassPermissions: boolean }) => updateSettings(p) } }));++const { useRuntimeState } = await import("./store");+const { setPermissionMode, refreshPermissionMode } = await import("./permission");+const { conversations } = await import("./conversation");+const { handleEvent } = await import("./events");++describe("permission mode", () => {+ beforeEach(() => {+ useRuntimeState.setState({ allowAll: null });+ updateSettings.mockClear();+ });++ it("reads the runtime's mode once", async () => {+ await refreshPermissionMode();+ expect(useRuntimeState.getState().allowAll).toBe(false);+ });++ it("a flip from one toggle is what every toggle reads", async () => {+ useRuntimeState.setState({ allowAll: false });+ await setPermissionMode(true);+ expect(updateSettings).toHaveBeenCalledWith({ bypassPermissions: true });+ expect(useRuntimeState.getState().allowAll).toBe(true);+ });++ it("goes back if the runtime refuses the change", async () => {+ useRuntimeState.setState({ allowAll: false });+ updateSettings.mockRejectedValueOnce(new Error("offline"));+ await setPermissionMode(true);+ expect(useRuntimeState.getState().allowAll).toBe(false);+ });++ it("follows a change made elsewhere (terminal /permissions, another device) through the status event", () => {+ useRuntimeState.setState({ allowAll: false });+ handleEvent(conversations.chat, "status", JSON.stringify({ permission: "allow-all", provider: "Anthropic" }));+ expect(useRuntimeState.getState().allowAll).toBe(true);+ handleEvent(conversations.chat, "status", JSON.stringify({ permission: "ask-me" }));+ expect(useRuntimeState.getState().allowAll).toBe(false);+ handleEvent(conversations.chat, "status", JSON.stringify({ provider: "OpenAI" }));+ expect(useRuntimeState.getState().allowAll).toBe(false);+ });+});diff --git a/apps/desktop/src/app/permission.ts b/apps/desktop/src/app/permission.tsnew file mode 100644index 00000000..90d2255d--- /dev/null+++ b/apps/desktop/src/app/permission.ts@@ -0,0 +1,35 @@+import { useEffect } from "react";+import * as api from "~/api/mercury";+import { useRuntimeState } from "./store";++/**+ * Ask Me / Allow All, shared by every toggle (the composer's picker, the+ * status bar). Reads the runtime once, flips optimistically, and the+ * runtime's status events keep it in sync with changes made elsewhere.+ */+export function usePermissionMode(): { allowAll: boolean | null; setAllowAll: (next: boolean) => Promise<void> } {+ const allowAll = useRuntimeState((s) => s.allowAll);+ useEffect(() => {+ if (useRuntimeState.getState().allowAll === null) void refreshPermissionMode();+ }, []);+ return { allowAll, setAllowAll: setPermissionMode };+}++export async function refreshPermissionMode(): Promise<void> {+ try {+ const s = await api.chat.settings();+ useRuntimeState.getState().setAllowAll(s.bypassPermissions === true);+ } catch { /* keep what we have */ }+}++export async function setPermissionMode(next: boolean): Promise<void> {+ const st = useRuntimeState.getState();+ const before = st.allowAll;+ st.setAllowAll(next);+ try {+ const s = await api.chat.updateSettings({ bypassPermissions: next });+ useRuntimeState.getState().setAllowAll(s.bypassPermissions === true);+ } catch {+ if (before !== null) useRuntimeState.getState().setAllowAll(before);+ }+}diff --git a/apps/desktop/src/app/store.ts b/apps/desktop/src/app/store.tsindex de91bcc1..4b2ccafb 100644--- a/apps/desktop/src/app/store.ts+++ b/apps/desktop/src/app/store.ts@@ -52,12 +52,15 @@ interface RuntimeState {model: string;step: { total: number; current: number; tool: string } | null;prompts: PendingPrompt[];+ /** Ask Me (false) or Allow All (true); null until known. One copy for every toggle on screen. */+ allowAll: boolean | null;setConnected(connected: boolean): void;setAgent(agent: AgentState): void;setProvider(provider: string, model: string): void;setStep(step: RuntimeState["step"]): void;addPrompt(prompt: PendingPrompt): void;removePrompt(id: string): void;+ setAllowAll(allowAll: boolean): void;}export const useRuntimeState = create<RuntimeState>((set, get) => ({@@ -67,6 +70,8 @@ export const useRuntimeState = create<RuntimeState>((set, get) => ({model: "",step: null,prompts: [],+ allowAll: null,+ setAllowAll: (allowAll) => set({ allowAll }),setConnected: (connected) => set({ connected, agent: connected ? (get().agent === "offline" ? "idle" : get().agent) : "offline" }),setAgent: (agent) => set({ agent }),setProvider: (provider, model) => set({ provider, model }),diff --git a/apps/desktop/src/extensions/builtin-status.tsx b/apps/desktop/src/extensions/builtin-status.tsxindex 8ec1b878..f46e5c55 100644--- a/apps/desktop/src/extensions/builtin-status.tsx+++ b/apps/desktop/src/extensions/builtin-status.tsx@@ -2,7 +2,8 @@ import { forwardRef, useEffect, useState, type ComponentPropsWithoutRef } from "import { Bot, CalendarClock, Code2, Coins, Cpu, Layers, Server, Shield, ShieldCheck, Trash2 } from "lucide-react";import { cx } from "~/lib/cx";import * as api from "~/api/mercury";-import type { ChatSettings, CodeStatus } from "~/api/types";+import type { CodeStatus } from "~/api/types";+import { usePermissionMode } from "~/app/permission";import { useBots, useRuntimeState, useUi } from "~/app/store";import { refreshPulse, usePulse, type ScheduleInfo } from "~/app/pulse";import { http } from "~/api/client";@@ -223,14 +224,11 @@ function Model() {}function Approval() {- const [settings, setSettings] = useState<ChatSettings | null>(null);- const view = useUi((s) => s.view);- useEffect(() => { void api.chat.settings().then(setSettings).catch(() => {}); }, [view]);- if (!settings) return null;- const allowAll = settings.bypassPermissions;+ const { allowAll, setAllowAll } = usePermissionMode();+ if (allowAll === null) return null;return (<StatusButton title="Click to switch Ask Me / Allow All" tone={allowAll ? "accent" : "amber"}- onClick={() => void api.chat.updateSettings({ bypassPermissions: !allowAll }).then(setSettings).catch(() => {})}>+ onClick={() => void setAllowAll(!allowAll)}>{allowAll ? <ShieldCheck size={13} /> : <Shield size={13} />}<span>{allowAll ? "Allow All" : "Ask Me"}</span></StatusButton>diff --git a/apps/desktop/src/views/chat/Composer.tsx b/apps/desktop/src/views/chat/Composer.tsxindex 9f6270c5..255d4752 100644--- a/apps/desktop/src/views/chat/Composer.tsx+++ b/apps/desktop/src/views/chat/Composer.tsx@@ -2,7 +2,8 @@ import { ArrowUp, AudioLines, ChevronDown, Paperclip, Shield, ShieldCheck, Squarimport { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";import { cx } from "~/lib/cx";import * as api from "~/api/mercury";-import type { ChatSettings, ModelsResponse } from "~/api/types";+import type { ModelsResponse } from "~/api/types";+import { usePermissionMode } from "~/app/permission";import { useRuntimeState } from "~/app/store";import { startCall, useVoice } from "~/app/voice";import { sendMessage, stopTurn, type CodeTurn } from "~/app/events";@@ -349,19 +350,16 @@ function ModelPicker() {}function ModePicker() {- const [settings, setSettings] = useState<ChatSettings | null>(null);- useEffect(() => { void api.chat.settings().then(setSettings).catch(() => setSettings(null)); }, []);- const allowAll = settings?.bypassPermissions === true;- async function set(next: boolean) {- try { setSettings(await api.chat.updateSettings({ bypassPermissions: next })); } catch { /* keep */ }- }+ const { allowAll: mode, setAllowAll } = usePermissionMode();+ const allowAll = mode === true;+ const set = setAllowAll;return (<Menuside="top"trigger={<button className={cx("flex h-8 items-center gap-1.5 rounded-md px-2 text-sm hover:bg-ink/[0.06]", allowAll ? "text-accent" : "text-amber")} aria-label="Approval mode">{allowAll ? <ShieldCheck size={13} /> : <Shield size={13} />}- {settings ? (allowAll ? "Allow All" : "Ask Me") : "…"}+ {mode !== null ? (allowAll ? "Allow All" : "Ask Me") : "…"}</button>}>