Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(desktop): Reset saved keys reports an unreadable folder and keeps waiting across warnings

Bugfixer 2314ff04d907c
diff --git a/electron/main.mjs b/electron/main.mjs
index e724d0aaee..5154cacc28 100644
--- a/electron/main.mjs
+++ b/electron/main.mjs
@@ -2795,7 +2795,6 @@ ipcMain.handle("credential-store:reset", localOnly("credential-store:reset", (ev
return response === 0;
},
quitting: () => desktopShutdownStarted,
- exists: () => fs.existsSync(CREDENTIALS_FILE),
moveAside: () => fs.renameSync(CREDENTIALS_FILE, `${CREDENTIALS_FILE}.unreadable-${Date.now()}`),
failed: async (error) => {
slog(`saved keys reset failed: ${error?.message ?? error}`);
diff --git a/electron/secure-credentials.mjs b/electron/secure-credentials.mjs
index 0f346acd43..d37a0e62a3 100644
--- a/electron/secure-credentials.mjs
+++ b/electron/secure-credentials.mjs
@@ -43,15 +43,18 @@ export async function readSecureCredentials({
/** "Reset saved keys" for a store that cannot be read: after the person confirms, the file is moved aside (kept, not
* deleted) and the app restarts with an empty store it can write again. A store that was read is never touched, and
* nothing moves once the app has begun to quit while the person was deciding, since no restart would follow. */
-export async function resetUnreadableCredentials({ unreadable, confirm, quitting, exists, moveAside, failed, restart }) {
+export async function resetUnreadableCredentials({ unreadable, confirm, quitting, moveAside, failed, restart }) {
if (!unreadable) return "readable";
if (!(await confirm())) return "cancelled";
if (quitting()) return "quitting";
try {
- if (exists()) moveAside();
+ moveAside();
} catch (error) {
- await failed(error);
- return "failed";
+ // Only a file that is really gone counts as moved; a folder that can no longer be read is a failure to report.
+ if (error?.code !== "ENOENT") {
+ await failed(error);
+ return "failed";
+ }
}
restart();
return "reset";
diff --git a/electron/secure-credentials.test.mjs b/electron/secure-credentials.test.mjs
index ec4490c250..ac4e877535 100644
--- a/electron/secure-credentials.test.mjs
+++ b/electron/secure-credentials.test.mjs
@@ -72,7 +72,6 @@ describe("resetUnreadableCredentials", () => {
unreadable: true,
confirm: vi.fn(async () => true),
quitting: () => false,
- exists: () => true,
moveAside: vi.fn(),
failed: vi.fn(async () => {}),
restart: vi.fn(),
@@ -117,9 +116,18 @@ describe("resetUnreadableCredentials", () => {
});
it("still restarts when the file is already gone", async () => {
- const d = resetDeps({ exists: () => false });
+ const gone = Object.assign(new Error("ENOENT: no such file or directory"), { code: "ENOENT" });
+ const d = resetDeps({ moveAside: vi.fn(() => { throw gone; }) });
await expect(resetUnreadableCredentials(d)).resolves.toBe("reset");
- expect(d.moveAside).not.toHaveBeenCalled();
+ expect(d.failed).not.toHaveBeenCalled();
expect(d.restart).toHaveBeenCalledTimes(1);
});
+
+ it("reports a folder it can no longer read instead of restarting as if the file were gone", async () => {
+ const denied = Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" });
+ const d = resetDeps({ moveAside: vi.fn(() => { throw denied; }) });
+ await expect(resetUnreadableCredentials(d)).resolves.toBe("failed");
+ expect(d.failed).toHaveBeenCalledWith(denied);
+ expect(d.restart).not.toHaveBeenCalled();
+ });
});
diff --git a/src/components/SavedKeysReset.test.ts b/src/components/SavedKeysReset.test.ts
index bbaff17f9a..faf47080c3 100644
--- a/src/components/SavedKeysReset.test.ts
+++ b/src/components/SavedKeysReset.test.ts
@@ -9,7 +9,7 @@ import { SavedKeysReset } from "./SavedKeysReset";
let host: HTMLDivElement;
let root: Root;
-let answer: () => void;
+let answer: (() => void) | undefined;
const reset = vi.fn(() => new Promise<void>((resolve) => { answer = resolve; }));
beforeEach(() => {
@@ -19,7 +19,9 @@ beforeEach(() => {
document.body.append(host);
root = createRoot(host);
});
-afterEach(() => {
+afterEach(async () => {
+ // The open question is shared by every button, so settle it before the next test.
+ await act(async () => { answer?.(); });
act(() => root.unmount());
host.remove();
delete (window as unknown as { ogb?: unknown }).ogb;
@@ -36,12 +38,24 @@ describe("Reset saved keys", () => {
await act(async () => { button.click(); button.click(); });
expect(reset).toHaveBeenCalledTimes(1);
expect(button.disabled).toBe(true);
- await act(async () => { answer(); });
+ await act(async () => { answer?.(); });
expect(button.disabled).toBe(false);
await act(async () => { button.click(); });
expect(reset).toHaveBeenCalledTimes(2);
});
+ it("keeps waiting for the open question when the warning it sits in is swapped for another", async () => {
+ await act(async () => { root.render(createElement(SavedKeysReset, { key: "inside the stale warning" })); });
+ await act(async () => { (host.querySelector("button") as HTMLButtonElement).click(); });
+ await act(async () => { root.render(createElement(SavedKeysReset, { key: "own warning line", notice: true })); });
+ const replacement = host.querySelector("button") as HTMLButtonElement;
+ expect(replacement.disabled).toBe(true);
+ await act(async () => { replacement.click(); });
+ expect(reset).toHaveBeenCalledTimes(1);
+ await act(async () => { answer?.(); });
+ expect(replacement.disabled).toBe(false);
+ });
+
it("comes with its own warning line when there is no other notice to sit in", async () => {
const button = await show(true);
expect(button).not.toBeNull();
diff --git a/src/components/SavedKeysReset.tsx b/src/components/SavedKeysReset.tsx
index c434a89d5b..3656d801e4 100644
--- a/src/components/SavedKeysReset.tsx
+++ b/src/components/SavedKeysReset.tsx
@@ -1,33 +1,35 @@
import { TriangleAlert } from "lucide-react";
-import { useEffect, useRef, useState } from "react";
+import { useEffect, useState } from "react";
import { t } from "@/lib/i18n";
import { savedKeysUnreadable } from "@/lib/saved-keys-reset";
+/** The desktop's open "Reset saved keys" question, shared by every button so a remounted one cannot ask again. */
+let asking: Promise<void> | null = null;
+
/** "Reset saved keys", shown only when this app's desktop could not read its saved keys this launch. The desktop asks
* the person before it moves the unreadable file aside and restarts; the button waits for that answer, so a second
* click cannot open a second question. With `notice`, it comes in its own warning line, for a page with no other
* warning to sit in. */
export function SavedKeysReset({ notice = false }: { notice?: boolean }) {
const [unreadable, setUnreadable] = useState(false);
- const [pending, setPending] = useState(false);
- const asking = useRef(false);
+ const [pending, setPending] = useState(() => asking !== null);
useEffect(() => {
let live = true;
void savedKeysUnreadable().then((answer) => { if (live) setUnreadable(answer); });
+ // The warning this button sits in can be swapped for another while the question is open: the new button waits too.
+ void asking?.finally(() => { if (live) setPending(false); });
return () => { live = false; };
}, []);
if (!unreadable) return null;
const reset = () => {
- if (asking.current) return;
- asking.current = true;
+ if (asking) return;
setPending(true);
- void Promise.resolve(window.ogb?.credentialStore?.reset())
- .catch(() => {})
- .finally(() => {
- asking.current = false;
- setPending(false);
- });
+ const request = Promise.resolve(window.ogb?.credentialStore?.reset()).then(() => {}, () => {});
+ asking = request.finally(() => {
+ asking = null;
+ setPending(false);
+ });
};
const button = (
<button
diff --git a/src/types/ogb.d.ts b/src/types/ogb.d.ts
index 61ca2a4060..790c7412d2 100644
--- a/src/types/ogb.d.ts
+++ b/src/types/ogb.d.ts
@@ -446,9 +446,10 @@ const __APP_VERSION__: string;
/** This app's own login item, which opens the window. `available`: false in a build that cannot add one. */
openAtLogin?: { state(): Promise<{ available: boolean; on: boolean }>; set(on: boolean): Promise<{ available: boolean; on: boolean }> };
/** Saved keys this launch could not read (this app's own page only). `reset` asks the person, moves the unreadable
- * file aside and restarts; it resolves "cancelled" when they say no,
- * "failed" (after telling them) when the file could not be moved, and "readable" (doing nothing) when the keys could be read. */
- credentialStore?: { unreadable(): Promise<boolean>; reset(): Promise<"reset" | "cancelled" | "failed" | "readable"> };
+ * file aside and restarts; it resolves "cancelled" when they say no, "quitting" (doing nothing) when the app began
+ * to quit while they decided, "failed" (after telling them) when the file could not be moved, and "readable"
+ * (doing nothing) when the keys could be read. */
+ credentialStore?: { unreadable(): Promise<boolean>; reset(): Promise<"reset" | "cancelled" | "quitting" | "failed" | "readable"> };
/** Save a provider credential through Electron's OS-backed store. */
setCredential?(
name: "composioApiKey" | "xaiApiKey" | "boxToken" | "opencodeGoApiKey" | "ttsKey" | "fishAudioKey" | "geminiTtsKey" | "jevApiKey" | "openrouterDeciderKey" | "workersAiToken" | "openaiImageApiKey" | "customImageApiKey" | "openaiLiveKey" | "tregApiKey" | "postizApiKey" | "geminiImageApiKey",