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(app): keep enterprise recording alive during auth checks

Louis Beaumont340b100f9b3e
diff --git a/apps/screenpipe-app-tauri/components/app-entitlement-gate.test.tsx b/apps/screenpipe-app-tauri/components/app-entitlement-gate.test.tsx
index b869ee550..a2af66099 100644
--- a/apps/screenpipe-app-tauri/components/app-entitlement-gate.test.tsx
+++ b/apps/screenpipe-app-tauri/components/app-entitlement-gate.test.tsx
@@ -19,6 +19,7 @@ const mocks = vi.hoisted(() => ({
piUpdateConfig: vi.fn().mockResolvedValue(undefined),
platform: vi.fn(() => "windows"),
arch: vi.fn(() => "x86_64"),
+ windowLabel: "main",
loadUser: vi.fn().mockResolvedValue(undefined),
updateSettings: vi.fn().mockResolvedValue(undefined),
state: { isSettingsLoaded: true, user: null as any },
@@ -76,7 +77,7 @@ vi.mock("@tauri-apps/plugin-os", () => ({
// detects via getCurrentWindow().label. Stand in as the primary "main" window so
// the sequenced stop -> settle -> spawn actually runs under test.
vi.mock("@tauri-apps/api/window", () => ({
- getCurrentWindow: () => ({ label: "main" }),
+ getCurrentWindow: () => ({ label: mocks.windowLabel }),
}));
import { AppEntitlementGate } from "./app-entitlement-gate";
@@ -109,6 +110,7 @@ describe("AppEntitlementGate", () => {
vi.stubEnv("TAURI_ENV_DEBUG", "false");
vi.stubEnv("NEXT_PUBLIC_SCREENPIPE_DEV_BILLING_BYPASS", "false");
mocks.state = { isSettingsLoaded: true, user: null };
+ mocks.windowLabel = "main";
mocks.enterprise = {
isEnterprise: false,
isEnterpriseBuildResolved: true,
@@ -158,6 +160,91 @@ describe("AppEntitlementGate", () => {
await waitFor(() => expect(mocks.stopScreenpipe).toHaveBeenCalled());
});
+ it("keeps recording while enterprise authentication is still checking", async () => {
+ mocks.state.user = baseUser({
+ app_entitled: true,
+ subscription_plan: "enterprise",
+ entitlement: {
+ active: true,
+ plan: "enterprise",
+ source: "enterprise",
+ checked_at: minsAgo(1),
+ features: { app: true },
+ },
+ });
+ mocks.enterprise = {
+ isEnterprise: true,
+ isEnterpriseBuildResolved: true,
+ authenticationState: "checking",
+ authenticationError: null,
+ isEnterpriseAuthenticated: false,
+ };
+
+ const { rerender } = render(
+ <AppEntitlementGate>{protectedApp}</AppEntitlementGate>,
+ );
+
+ expect(screen.getByText(/checking enterprise access/i)).toBeInTheDocument();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(mocks.stopScreenpipe).not.toHaveBeenCalled();
+ expect(mocks.capture).not.toHaveBeenCalledWith(
+ "app_entitlement_gate_shown",
+ expect.anything(),
+ );
+
+ mocks.enterprise.authenticationState = "authenticated";
+ mocks.enterprise.isEnterpriseAuthenticated = true;
+ rerender(<AppEntitlementGate>{protectedApp}</AppEntitlementGate>);
+
+ expect(screen.getByTestId("protected-app")).toBeInTheDocument();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(mocks.stopScreenpipe).not.toHaveBeenCalled();
+ expect(mocks.spawnScreenpipe).not.toHaveBeenCalled();
+ });
+
+ it("never lets a secondary enterprise webview stop the shared recorder", async () => {
+ mocks.windowLabel = "search";
+ mocks.enterprise = {
+ isEnterprise: true,
+ isEnterpriseBuildResolved: true,
+ authenticationState: "account",
+ authenticationError: null,
+ isEnterpriseAuthenticated: false,
+ };
+
+ render(<AppEntitlementGate>{protectedApp}</AppEntitlementGate>);
+
+ expect(screen.queryByTestId("protected-app")).not.toBeInTheDocument();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(mocks.stopScreenpipe).not.toHaveBeenCalled();
+ });
+
+ it("resumes recording when a real enterprise gate authenticates", async () => {
+ mocks.enterprise = {
+ isEnterprise: true,
+ isEnterpriseBuildResolved: true,
+ authenticationState: "account",
+ authenticationError: null,
+ isEnterpriseAuthenticated: false,
+ };
+
+ const { rerender } = render(
+ <AppEntitlementGate>{protectedApp}</AppEntitlementGate>,
+ );
+ await waitFor(() => expect(mocks.stopScreenpipe).toHaveBeenCalledTimes(1));
+
+ mocks.enterprise.authenticationState = "authenticated";
+ mocks.enterprise.isEnterpriseAuthenticated = true;
+ rerender(<AppEntitlementGate>{protectedApp}</AppEntitlementGate>);
+
+ await waitFor(() => expect(mocks.spawnScreenpipe).toHaveBeenCalledWith(null));
+ expect(mocks.capture).toHaveBeenCalledWith(
+ "enterprise_auth_recording_restored",
+ { authentication_state: "authenticated" },
+ );
+ expect(screen.getByTestId("protected-app")).toBeInTheDocument();
+ });
+
it("asks a signed-out user to sign in and never reveals the app", () => {
mocks.state.user = null;
render(<AppEntitlementGate>{protectedApp}</AppEntitlementGate>);
diff --git a/apps/screenpipe-app-tauri/components/app-entitlement-gate.tsx b/apps/screenpipe-app-tauri/components/app-entitlement-gate.tsx
index 54491ed5e..eabded283 100644
--- a/apps/screenpipe-app-tauri/components/app-entitlement-gate.tsx
+++ b/apps/screenpipe-app-tauri/components/app-entitlement-gate.tsx
@@ -120,7 +120,9 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
const [devSubmitting, setDevSubmitting] = useState(false);
const [devError, setDevError] = useState<string | null>(null);
const stoppedForGateRef = useRef(false);
+ const recorderStoppedByGateRef = useRef(false);
const prevEntitledRef = useRef<boolean | null>(null);
+ const prevEnterpriseAuthenticatedRef = useRef<boolean | null>(null);
const skipNextResumeForE2ESeedRef = useRef(false);
const resumingRef = useRef(false);
const everEntitledRef = useRef(false);
@@ -184,6 +186,8 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
: isEnterprise
? !isEnterpriseAuthenticated
: shouldGateForEnterpriseApp || shouldGateForEntitlement;
+ const enterpriseAuthenticationPending =
+ isEnterprise && authenticationState === "checking";
const email = user?.email || "this account";
const enterpriseOrgName = enterpriseAccount?.org_name || "your workspace";
const planLabel = useMemo(
@@ -218,7 +222,7 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
// re-fire this on every settings broadcast — 33k events from 36 users in 30d.
// Reset the latch only when the gate clears so a genuine re-gate still counts.
useEffect(() => {
- if (!isSettingsLoaded || !shouldGate) {
+ if (!isSettingsLoaded || !shouldGate || enterpriseAuthenticationPending) {
gateReportedRef.current = false;
return;
}
@@ -242,7 +246,7 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
? "enterprise_app"
: "entitlement",
});
- }, [isSettingsLoaded, shouldGate, shouldGateForEnterpriseLogin, shouldGateForEnterpriseApp, isEnterprise, tokenPending, failOpenForTransientAccessLoss, user?.app_entitled, user?.subscription_plan, user?.token]);
+ }, [isSettingsLoaded, shouldGate, shouldGateForEnterpriseLogin, shouldGateForEnterpriseApp, isEnterprise, enterpriseAuthenticationPending, tokenPending, failOpenForTransientAccessLoss, user?.app_entitled, user?.subscription_plan, user?.token]);
// When failing open on a pending token, keep trying to re-read it from the
// secret store. Once the store heals (the periodic WAL checkpoint clears the
@@ -279,16 +283,33 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
// "checking access" shell, but that transient state must never stop the
// recorder. Otherwise opening the overlay can tear down the local API just
// before the consumer/enterprise result arrives.
- if (!isSettingsLoaded || !isEnterpriseBuildResolved || !shouldGate) {
+ if (!isSettingsLoaded || !isEnterpriseBuildResolved) return;
+ if (!shouldGate) {
stoppedForGateRef.current = false;
return;
}
+ // Enterprise credentials are restored asynchronously in every webview.
+ // `checking` means "verification in progress", not "access denied". The
+ // old behavior stopped the recorder here, then failed to resume because an
+ // already-entitled enterprise account never flips `isEntitled` false→true.
+ if (enterpriseAuthenticationPending) return;
+ // Only the primary content window owns recorder lifecycle. Search, overlay,
+ // notification, and settings webviews still render the gate but must never
+ // tear down the shared localhost engine.
+ if (!isPrimaryWindow()) return;
if (stoppedForGateRef.current) return;
stoppedForGateRef.current = true;
+ recorderStoppedByGateRef.current = true;
commands.stopScreenpipe().catch((err) => {
console.warn("failed to stop screenpipe after entitlement gate:", err);
});
- }, [isSettingsLoaded, isEnterpriseBuildResolved, shouldGate]);
+ }, [
+ enterpriseAuthenticationPending,
+ isEnterprise,
+ isEnterpriseBuildResolved,
+ isSettingsLoaded,
+ shouldGate,
+ ]);
const openPricing = useCallback(() => {
posthog.capture("app_entitlement_choose_plan_clicked", {
@@ -444,6 +465,48 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
// raced a reconnect's in-flight teardown and wedged the engine at "Starting
// capture session" (port never rebound). See the recording-settings
// "Apply & Restart" path for the canonical sequence.
+ const resumeRecordingAfterGate = useCallback((requireGateStop: boolean) => {
+ if (requireGateStop && !recorderStoppedByGateRef.current) return;
+ if (!isPrimaryWindow() || resumingRef.current) return;
+ resumingRef.current = true;
+ void (async () => {
+ try {
+ await commands.stopScreenpipe();
+ await new Promise((r) => setTimeout(r, 500));
+ await commands.spawnScreenpipe(null);
+ recorderStoppedByGateRef.current = false;
+ } catch (err) {
+ console.warn("failed to restart screenpipe after access restored:", err);
+ } finally {
+ resumingRef.current = false;
+ }
+ })();
+ }, []);
+
+ // A genuine enterprise gate (missing/invalid account or key) is allowed to
+ // stop capture. If the user then authenticates successfully, resume even
+ // though their paid entitlement was already true before the gate appeared.
+ useEffect(() => {
+ if (!isSettingsLoaded || !isEnterprise) {
+ prevEnterpriseAuthenticatedRef.current = null;
+ return;
+ }
+ const previouslyAuthenticated = prevEnterpriseAuthenticatedRef.current;
+ prevEnterpriseAuthenticatedRef.current = isEnterpriseAuthenticated;
+ if (previouslyAuthenticated !== false || !isEnterpriseAuthenticated) return;
+ if (!recorderStoppedByGateRef.current) return;
+ posthog.capture("enterprise_auth_recording_restored", {
+ authentication_state: authenticationState,
+ });
+ resumeRecordingAfterGate(true);
+ }, [
+ authenticationState,
+ isEnterprise,
+ isEnterpriseAuthenticated,
+ isSettingsLoaded,
+ resumeRecordingAfterGate,
+ ]);
+
useEffect(() => {
if (!isSettingsLoaded || devBypass) return;
if (skipNextResumeForE2ESeedRef.current) {
@@ -460,24 +523,8 @@ export function AppEntitlementGate({ children }: { children: React.ReactNode })
posthog.capture("app_entitlement_restored", {
plan: user?.subscription_plan ?? null,
});
- // Single owner: only the primary window restarts the engine, so secondary
- // webviews don't fire overlapping spawns that race each other.
- if (!isPrimaryWindow()) return;
- // Collapse rapid re-fires into one restart in flight.
- if (resumingRef.current) return;
- resumingRef.current = true;
- void (async () => {
- try {
- await commands.stopScreenpipe();
- await new Promise((r) => setTimeout(r, 500));
- await commands.spawnScreenpipe(null);
- } catch (err) {
- console.warn("failed to restart screenpipe after entitlement restored:", err);
- } finally {
- resumingRef.current = false;
- }
- })();
- }, [devBypass, isEntitled, isSettingsLoaded]);
+ resumeRecordingAfterGate(false);
+ }, [devBypass, isEntitled, isSettingsLoaded, resumeRecordingAfterGate]);
const devLoginBlock = isDevLoginEnabled() ? (
<div className="mt-5 border-t border-border pt-4">