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(card-ask): emit card_ask_shown once per opening (#6128)

Louis Beaumont6e5212ec3ae7
diff --git a/apps/screenpipe-app-tauri/components/__tests__/card-ask-modal.test.tsx b/apps/screenpipe-app-tauri/components/__tests__/card-ask-modal.test.tsx
index a991482ee..06734a604 100644
--- a/apps/screenpipe-app-tauri/components/__tests__/card-ask-modal.test.tsx
+++ b/apps/screenpipe-app-tauri/components/__tests__/card-ask-modal.test.tsx
@@ -165,3 +165,33 @@ describe("CardAskModal", () => {
await waitFor(() => expect(openUrl).toHaveBeenCalledTimes(1));
});
});
+
+describe("CardAskModal shown-event idempotence", () => {
+ // Regression: `isFirstAsk` is derived from a mutable ref in useCardAsk and is
+ // in this effect's dependency array. On a user's second ask it flips
+ // true -> false, the deps change while the modal is still open, and the
+ // effect re-runs — emitting card_ask_shown twice for ONE modal.
+ //
+ // Observed in production: one user, trigger grant_expiry, two events at the
+ // same second from one machine. It inflates the exposure denominator, so
+ // every rate computed from card_ask_shown reads low.
+ it("emits card_ask_shown once per opening when isFirstAsk flips", () => {
+ const { rerender } = render(
+ <CardAskModal {...base} trigger="grant_expiry" isFirstAsk={true} />,
+ );
+ expect(names().filter((n) => n === "card_ask_shown")).toHaveLength(1);
+
+ // Same modal, still open, isFirstAsk recomputed to false.
+ rerender(<CardAskModal {...base} trigger="grant_expiry" isFirstAsk={false} />);
+ expect(names().filter((n) => n === "card_ask_shown")).toHaveLength(1);
+ });
+
+ it("still reports a genuinely new opening", () => {
+ const { rerender } = render(
+ <CardAskModal {...base} trigger="first_value" isFirstAsk={true} />,
+ );
+ rerender(<CardAskModal {...base} trigger={null} isFirstAsk={true} />);
+ rerender(<CardAskModal {...base} trigger="grant_expiry" isFirstAsk={false} />);
+ expect(names().filter((n) => n === "card_ask_shown")).toHaveLength(2);
+ });
+});
diff --git a/apps/screenpipe-app-tauri/components/card-ask-modal.tsx b/apps/screenpipe-app-tauri/components/card-ask-modal.tsx
index 9c52b86fd..fdabbb15d 100644
--- a/apps/screenpipe-app-tauri/components/card-ask-modal.tsx
+++ b/apps/screenpipe-app-tauri/components/card-ask-modal.tsx
@@ -76,11 +76,22 @@ export function CardAskModal({
const [busy, setBusy] = useState(false);
const open = trigger !== null && arm !== null;
+ // Which opening has already been reported. `isFirstAsk` is derived from a
+ // mutable ref in useCardAsk, so it can flip true -> false on a later render
+ // while this modal stays open — on a user's second ask it always does. That
+ // changes this effect's deps and re-runs it, emitting a second
+ // `card_ask_shown` for a single modal and inflating the exposure
+ // denominator every rate is computed from.
+ const reportedTriggerRef = useRef<CardAskTrigger | null>(null);
+
useEffect(() => {
if (!open || !trigger || !arm) {
shownAtRef.current = null;
+ reportedTriggerRef.current = null;
return;
}
+ if (reportedTriggerRef.current === trigger) return;
+ reportedTriggerRef.current = trigger;
shownAtRef.current = Date.now();
cardAskEvents.shown({ arm, trigger, os, isFirstAsk });
}, [open, trigger, arm, os, isFirstAsk]);