Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: restart Pi agent on account switch to prevent stale auth token

Louis Beaumont9fe65160f5cf
diff --git a/apps/screenpipe-app-tauri/components/deeplink-handler.tsx b/apps/screenpipe-app-tauri/components/deeplink-handler.tsx
index 2ec5426cf..852cd27ab 100644
--- a/apps/screenpipe-app-tauri/components/deeplink-handler.tsx
+++ b/apps/screenpipe-app-tauri/components/deeplink-handler.tsx
@@ -13,6 +13,7 @@ import { listen, emit } from "@tauri-apps/api/event";
import { onOpenUrl } from "@tauri-apps/plugin-deep-link";
import { openSettingsWindow } from "@/lib/utils/window";
import { useTimelineStore } from "@/lib/hooks/use-timeline-store";
+import { homeDir, join } from "@tauri-apps/api/path";
export function DeeplinkHandler() {
const { toast } = useToast();
@@ -37,6 +38,19 @@ export function DeeplinkHandler() {
title: "logged in!",
description: "you have been logged in",
});
+ // Force-restart Pi with the new token so it picks up the new
+ // account immediately. Without this, Pi keeps running with the
+ // old/null token and the user hits the free-tier rate limit.
+ try {
+ const home = await homeDir();
+ const dir = await join(home, ".screenpipe", "pi-chat");
+ await commands.piStart("chat", dir, apiKey, null);
+ console.log("[deeplink] restarted Pi with new auth token");
+ } catch (e) {
+ // Pi may not be running yet — that's fine, it'll start
+ // with the correct token on first message
+ console.log("[deeplink] Pi restart after login skipped:", e);
+ }
} catch (error) {
console.error("failed to load user:", error);
toast({
diff --git a/apps/screenpipe-app-tauri/components/settings/account-section.tsx b/apps/screenpipe-app-tauri/components/settings/account-section.tsx
index 6e773ee09..45dba2d6c 100644
--- a/apps/screenpipe-app-tauri/components/settings/account-section.tsx
+++ b/apps/screenpipe-app-tauri/components/settings/account-section.tsx
@@ -201,8 +201,13 @@ export function AccountSection() {
<Button
variant="outline"
size="sm"
- onClick={() => {
+ onClick={async () => {
updateSettings({ user: undefined });
+ // Restart Pi with null token so it stops using the old
+ // account's quota. Next message will auto-start as anonymous.
+ try {
+ await commands.piUpdateConfig(null, null);
+ } catch {}
toast({ title: "logged out" });
}}
>
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 43eca6b19..223687bfc 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -1030,7 +1030,7 @@ export function StandaloneChat({ className }: { className?: string } = {}) {
const piRunningConfigRef = useRef<{ provider?: string; model?: string; token?: string | null }>({});
const piRestartTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
- // Restart Pi when user switches preset so the new model takes effect immediately.
+ // Restart Pi when user switches preset or account so the new model/token takes effect.
// Pi uses CLI args from startup, so config-only updates don't change the running model.
// Debounced to collapse rapid settings saves into a single restart.
useEffect(() => {
@@ -1039,12 +1039,26 @@ export function StandaloneChat({ className }: { className?: string } = {}) {
const currentToken = settings.user?.token ?? null;
const configChanged = running.provider !== undefined &&
(running.provider !== activePreset.provider || running.model !== activePreset.model);
+ // Always detect token changes — even on first mount. If Pi is already running
+ // with a stale/null token (e.g. user logged out then into a different account),
+ // we must restart it. Without this, switching accounts leaves Pi sending
+ // requests as anonymous, hitting the free tier rate limit.
const tokenChanged = running.token !== undefined && running.token !== currentToken;
if (!configChanged && !tokenChanged) {
- // First mount or same config — just record what we expect Pi to be running
+ // First mount — record what we expect Pi to be running, but also check
+ // if Pi is already alive with a potentially stale token and restart it.
if (running.provider === undefined) {
piRunningConfigRef.current = { provider: activePreset.provider, model: activePreset.model, token: currentToken };
+ // If Pi is running, proactively push the current token/config so it
+ // picks up any account change that happened while chat was unmounted.
+ if (piInfo?.running) {
+ const providerConfig = buildProviderConfig();
+ console.log("[Pi] First mount with running Pi — syncing config/token");
+ commands.piUpdateConfig(currentToken, providerConfig).catch((e) => {
+ console.error("[Pi] First-mount config sync failed:", e);
+ });
+ }
}
return;
}