Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(web): record reasons for failed sign-in events (#1030)
Gustavo Ye4bb7eff59633
diff --git a/src/web/public/analytics-events.html b/src/web/public/analytics-events.htmlindex 2109d2618..51dfb57d0 100644--- a/src/web/public/analytics-events.html+++ b/src/web/public/analytics-events.html@@ -6,6 +6,21 @@ function gtag() { window.dataLayer.push(arguments); }gtag('consent', 'default', { analytics_storage: 'granted', ad_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied' });gtag('js', new Date());const fields = { page_location: 'https://alook.ai/analytics/product', page_referrer: '', page_title: 'Alook' };+const authReasons = {+ PROVIDER_NOT_FOUND: 'Provider not found', ID_TOKEN_NOT_SUPPORTED: 'ID token not supported',+ INVALID_TOKEN: 'Invalid token', FAILED_TO_GET_USER_INFO: 'Failed to get user info',+ USER_EMAIL_NOT_FOUND: 'User email not found', EMAIL_NOT_VERIFIED: 'Email not verified',+ INVALID_EMAIL: 'Invalid email', INVALID_EMAIL_OR_PASSWORD: 'Invalid email or password',+ INVALID_OTP: 'Invalid OTP', OTP_EXPIRED: 'OTP expired', TOO_MANY_ATTEMPTS: 'Too many attempts',+ USER_NOT_FOUND: 'User not found', FAILED_TO_CREATE_USER: 'Failed to create user',+ FAILED_TO_CREATE_SESSION: 'Failed to create session', FAILED_TO_CREATE_VERIFICATION: 'Unable to create verification',+ INVALID_ORIGIN: 'Invalid origin', MISSING_OR_NULL_ORIGIN: 'Missing or null Origin',+ INVALID_CALLBACK_URL: 'Invalid callback URL', INVALID_REDIRECT_URL: 'Invalid redirect URL',+ INVALID_ERROR_CALLBACK_URL: 'Invalid error callback URL', INVALID_NEW_USER_CALLBACK_URL: 'Invalid new user callback URL',+ CALLBACK_URL_REQUIRED: 'Callback URL is required', VALIDATION_ERROR: 'Validation error',+ RATE_LIMIT_EXCEEDED: 'Too many requests', INTERNAL_SERVER_ERROR: 'Internal server error',+ NETWORK_ERROR: 'Network request failed', UNKNOWN_ERROR: 'No recognized error detail returned'+};gtag('config', 'G-STBCL8F4ZY', { send_page_view: false, allow_google_signals: false, allow_ad_personalization_signals: false, ...fields });window.sendProductEvent = function ({ event, params }) {if (!['landing_cta_clicked', 'login_page_view', 'sign_up', 'sign_in_success', 'sign_in_failed', 'onboarding_enter_clicked', 'onboarding_first_message', 'invitation_link_visit', 'community_onboarding_completed', 'human_invitation_sent', 'human_invitation_copied', 'invited_human_joined'].includes(event)) return;@@ -13,6 +28,15 @@ window.sendProductEvent = function ({ event, params }) {if (event === 'landing_cta_clicked' && typeof params?.cta_name === 'string') safe.cta_name = params.cta_name;if (['sign_up', 'sign_in_success', 'sign_in_failed'].includes(event) && ['email', 'email_otp', 'github', 'google', 'apple', 'development_password'].includes(params?.method)) safe.method = params.method;if (event === 'sign_in_failed' && ['send_code', 'verify_code', 'social_start', 'provider_callback'].includes(params?.failure_stage)) safe.failure_stage = params.failure_stage;+ if (event === 'sign_in_failed') {+ if (typeof params?.http_status === 'string' && /^[1-5][0-9]{2}$/.test(params.http_status)) safe.http_status = params.http_status;+ if (typeof params?.error_code === 'string' && Object.hasOwn(authReasons, params.error_code)) {+ safe.error_code = params.error_code;+ safe.error_reason = authReasons[params.error_code];+ } else if (params?.error_code === 'HTTP_ERROR' && safe.http_status) {+ safe.error_code = 'HTTP_ERROR'; safe.error_reason = 'HTTP ' + safe.http_status;+ }+ }gtag('event', event, { ...safe, ...fields, send_to: 'G-STBCL8F4ZY' });};</script>diff --git a/src/web/src/app/(auth)/sign-in/sign-in-client.dom.test.ts b/src/web/src/app/(auth)/sign-in/sign-in-client.dom.test.tsindex 680811e01..cd88d17d2 100644--- a/src/web/src/app/(auth)/sign-in/sign-in-client.dom.test.ts+++ b/src/web/src/app/(auth)/sign-in/sign-in-client.dom.test.ts@@ -53,7 +53,7 @@ import SignInPageClient from "./sign-in-client"describe("sign-in client Apple availability", () => {beforeEach(() => {- vi.clearAllMocks(); mocks.send.mockResolvedValue({}); mocks.verify.mockResolvedValue({ error: { message: "bad code" } }); mocks.verifyInput = undefined+ vi.clearAllMocks(); mocks.send.mockResolvedValue({}); mocks.verify.mockResolvedValue({ error: { code: "INVALID_OTP", status: 400 } }); mocks.verifyInput = undefinedmocks.searchParams = new URLSearchParams("redirect=%2Fc%2Fme")window.matchMedia = vi.fn(() => ({matches: false,@@ -68,20 +68,32 @@ describe("sign-in client Apple availability", () => {await setupUser().click(screen.getByRole("button", { name: "Send Code" }))expect(mocks.capture).toHaveBeenCalledTimes(1)await act(async () => { await mocks.verifyInput!("123456") })- expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "verify_code" })+ expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "verify_code", error: { code: "INVALID_OTP", status: 400 } })expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain("123456")expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain("bad code")})it("records send-code failure without mistaking it for successful authentication", async () => {- mocks.send.mockResolvedValueOnce({ error: { message: "private detail" } })+ mocks.send.mockResolvedValueOnce({ error: { status: 429 } })render(createElement(SignInPageClient, { isProd: true, appleEnabled: false }))await setupUser().click(screen.getByRole("button", { name: "Send Code" }))- expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "send_code" })+ expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "send_code", error: { status: 429 } })expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain("private detail")expect(JSON.stringify(mocks.capture.mock.calls)).not.toContain("sign_in_success")})+ it("forwards caught send and verification failures with their actual stage", async () => {+ const error = new TypeError("Failed to fetch")+ mocks.send.mockRejectedValueOnce(error)+ render(createElement(SignInPageClient, { isProd: true, appleEnabled: false }))+ await setupUser().click(screen.getByRole("button", { name: "Send Code" }))+ expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "send_code", error })+ await setupUser().click(screen.getByRole("button", { name: "Send Code" }))+ mocks.verify.mockRejectedValueOnce(error)+ await act(async () => { await mocks.verifyInput!("123456") })+ expect(mocks.capture).toHaveBeenLastCalledWith({ event: "sign_in_failed", method: "email_otp", failureStage: "verify_code", error })+ })+it.each([true, false])("passes server-derived appleEnabled=%s unchanged", (appleEnabled) => {render(createElement(SignInPageClient, { isProd: true, appleEnabled }))diff --git a/src/web/src/app/(auth)/sign-in/sign-in-client.tsx b/src/web/src/app/(auth)/sign-in/sign-in-client.tsxindex 02b025363..55fb1cebf 100644--- a/src/web/src/app/(auth)/sign-in/sign-in-client.tsx+++ b/src/web/src/app/(auth)/sign-in/sign-in-client.tsx@@ -51,8 +51,8 @@ function SignInForm({appleEnabled: boolean}) {useEffect(() => { void captureProductAnalytics({ event: "login_page_view" }) }, [])- const failed = (failureStage: "send_code" | "verify_code") => {- void captureProductAnalytics({ event: "sign_in_failed", method: "email_otp", failureStage })+ const failed = (failureStage: "send_code" | "verify_code", error: unknown) => {+ void captureProductAnalytics({ event: "sign_in_failed", method: "email_otp", failureStage, error })}const [email, setEmail] = useAtom(useCreateAtom(""))const [emailError, setEmailError] = useAtom(useCreateAtom(""))@@ -96,16 +96,16 @@ function SignInForm({})finishAction(observation, error ? "error" : "success")if (error) {- failed("send_code")+ failed("send_code", error)if (error.status !== 429) setEmailError(error.message ?? "Failed to send code")} else {setEmailError("")setOtpError("")setStep("code")}- } catch {+ } catch (error) {finishAction(observation, "error")- failed("send_code")+ failed("send_code", error)setEmailError("Failed to send code")}setLoading(false)@@ -125,7 +125,7 @@ function SignInForm({})finishAction(observation, error ? "error" : "success")if (error) {- failed("verify_code")+ failed("verify_code", error)setOtpError(error.message ?? "Invalid code")setCode("")} else {@@ -133,9 +133,9 @@ function SignInForm({window.location.href = postLoginUrlreturn}- } catch {+ } catch (error) {finishAction(observation, "error")- failed("verify_code")+ failed("verify_code", error)setOtpError("Invalid code")setCode("")}diff --git a/src/web/src/app/(auth)/sign-in/social-sign-in.dom.test.ts b/src/web/src/app/(auth)/sign-in/social-sign-in.dom.test.tsindex bbb2bb5c2..0103fb722 100644--- a/src/web/src/app/(auth)/sign-in/social-sign-in.dom.test.ts+++ b/src/web/src/app/(auth)/sign-in/social-sign-in.dom.test.ts@@ -64,6 +64,24 @@ const attempt = {}describe("social sign-in native boundary", () => {+ it.each(["google", "github"])("forwards %s rejection details and does not count successful starts as failures", async provider => {+ const error = { code: "PROVIDER_NOT_FOUND", message: "Provider not found", status: 404 }+ mocks.social.mockResolvedValueOnce({ error })+ render(createElement(SocialSignIn, { postLoginUrl: "/c/me" }))+ await setupUser().click(button(`native-oauth-${provider}`))+ expect(mocks.capture).toHaveBeenCalledExactlyOnceWith({ event: "sign_in_failed", method: provider, failureStage: "social_start", error })+ await setupUser().click(button(`native-oauth-${provider}`))+ expect(mocks.capture).toHaveBeenCalledOnce()+ })++ it("forwards a social fetch rejection instead of discarding the error", async () => {+ const error = new TypeError("Failed to fetch")+ mocks.social.mockRejectedValueOnce(error)+ render(createElement(SocialSignIn, { postLoginUrl: "/c/me" }))+ await setupUser().click(button("native-oauth-google"))+ expect(mocks.capture).toHaveBeenCalledExactlyOnceWith({ event: "sign_in_failed", method: "google", failureStage: "social_start", error })+ })+it("preserves normal browser social login and callback destination", async () => {const user = setupUser()render(createElement(SocialSignIn, { postLoginUrl: "/c/me?tab=1" }))diff --git a/src/web/src/app/(auth)/sign-in/social-sign-in.tsx b/src/web/src/app/(auth)/sign-in/social-sign-in.tsxindex fbd747e4b..72f5a4a25 100644--- a/src/web/src/app/(auth)/sign-in/social-sign-in.tsx+++ b/src/web/src/app/(auth)/sign-in/social-sign-in.tsx@@ -56,11 +56,11 @@ export function SocialSignIn({lastProvider.current = providerconst action = observed.currentif (!isTauri()) {- const failed = () => { void captureProductAnalytics({ event: "sign_in_failed", method: provider, failureStage: "social_start" }) }+ const failed = (error: unknown) => { void captureProductAnalytics({ event: "sign_in_failed", method: provider, failureStage: "social_start", error }) }void signIn.social({ provider, callbackURL: postLoginUrl }).then(result => {finishAction(action, result.error ? "error" : "observed")- if (result.error) failed()- }, () => { finishAction(action, "error"); failed() })+ if (result.error) failed(result.error)+ }, error => { finishAction(action, "error"); failed(error) })return}if (controller.current) void controller.current.start(provider, safeRedirectPath(postLoginUrl))diff --git a/src/web/src/lib/auth-failure.test.ts b/src/web/src/lib/auth-failure.test.tsnew file mode 100644index 000000000..493d886a1--- /dev/null+++ b/src/web/src/lib/auth-failure.test.ts@@ -0,0 +1,35 @@+import { describe, expect, it } from "vitest"+import { authFailureParams } from "./auth-failure"++describe("authentication failure details", () => {+ it.each([+ ["PROVIDER_NOT_FOUND", "Provider not found", 404],+ ["INVALID_ORIGIN", "Invalid origin", 403],+ ["INVALID_OTP", "Invalid OTP", 400],+ ["OTP_EXPIRED", "OTP expired", 400],+ ["TOO_MANY_ATTEMPTS", "Too many attempts", 403],+ ])("keeps the returned %s cause without forwarding arbitrary messages", (code, reason, status) => {+ expect(authFailureParams({ code, status, message: "[email protected] token=secret otp=123456" }))+ .toEqual({ error_code: code, error_reason: reason, http_status: String(status) })+ })++ it("records rate limiting even when the response has no authentication code", () => {+ expect(authFailureParams({ status: 429, message: "private response" }))+ .toEqual({ error_code: "RATE_LIMIT_EXCEEDED", error_reason: "Too many requests", http_status: "429" })+ })++ it.each(["Failed to fetch", "Load failed", "NetworkError when attempting to fetch resource."])("recognizes browser fetch rejection: %s", message => {+ expect(authFailureParams(new TypeError(message)))+ .toEqual({ error_code: "NETWORK_ERROR", error_reason: "Network request failed" })+ })++ it("does not claim an unknown HTTP error's root cause", () => {+ expect(authFailureParams({ code: "TOKEN_SECRET", message: "https://host/?code=secret", status: 503 }))+ .toEqual({ error_code: "HTTP_ERROR", error_reason: "HTTP 503", http_status: "503" })+ })++ it.each([null, "[email protected]", new Error("token=secret"), { code: "__proto__", status: 999 }])("handles an unrecognized error without leaking its contents", error => {+ expect(authFailureParams(error))+ .toEqual({ error_code: "UNKNOWN_ERROR", error_reason: "No recognized error detail returned" })+ })+})diff --git a/src/web/src/lib/auth-failure.ts b/src/web/src/lib/auth-failure.tsnew file mode 100644index 000000000..c150a50ec--- /dev/null+++ b/src/web/src/lib/auth-failure.ts@@ -0,0 +1,40 @@+const reasons: Record<string, string> = {+ PROVIDER_NOT_FOUND: "Provider not found",+ ID_TOKEN_NOT_SUPPORTED: "ID token not supported",+ INVALID_TOKEN: "Invalid token",+ FAILED_TO_GET_USER_INFO: "Failed to get user info",+ USER_EMAIL_NOT_FOUND: "User email not found",+ EMAIL_NOT_VERIFIED: "Email not verified",+ INVALID_EMAIL: "Invalid email",+ INVALID_EMAIL_OR_PASSWORD: "Invalid email or password",+ INVALID_OTP: "Invalid OTP",+ OTP_EXPIRED: "OTP expired",+ TOO_MANY_ATTEMPTS: "Too many attempts",+ USER_NOT_FOUND: "User not found",+ FAILED_TO_CREATE_USER: "Failed to create user",+ FAILED_TO_CREATE_SESSION: "Failed to create session",+ FAILED_TO_CREATE_VERIFICATION: "Unable to create verification",+ INVALID_ORIGIN: "Invalid origin",+ MISSING_OR_NULL_ORIGIN: "Missing or null Origin",+ INVALID_CALLBACK_URL: "Invalid callback URL",+ INVALID_REDIRECT_URL: "Invalid redirect URL",+ INVALID_ERROR_CALLBACK_URL: "Invalid error callback URL",+ INVALID_NEW_USER_CALLBACK_URL: "Invalid new user callback URL",+ CALLBACK_URL_REQUIRED: "Callback URL is required",+ VALIDATION_ERROR: "Validation error",+ RATE_LIMIT_EXCEEDED: "Too many requests",+ INTERNAL_SERVER_ERROR: "Internal server error",+}++export function authFailureParams(error: unknown): Record<string, string> {+ const input = error && typeof error === "object" ? error as Record<string, unknown> : {}+ const status = typeof input.status === "number" && Number.isInteger(input.status) && input.status >= 100 && input.status <= 599 ? input.status : undefined+ const code = typeof input.code === "string" && Object.hasOwn(reasons, input.code) ? input.code : undefined+ const network = input.name === "TypeError" && ["Failed to fetch", "Load failed", "NetworkError when attempting to fetch resource."].includes(String(input.message))+ const errorCode = code ?? (status === 429 ? "RATE_LIMIT_EXCEEDED" : network ? "NETWORK_ERROR" : status ? "HTTP_ERROR" : "UNKNOWN_ERROR")+ return {+ error_code: errorCode,+ error_reason: reasons[errorCode] ?? (network ? "Network request failed" : status ? `HTTP ${status}` : "No recognized error detail returned"),+ ...(status ? { http_status: String(status) } : {}),+ }+}diff --git a/src/web/src/lib/product-analytics-document.test.ts b/src/web/src/lib/product-analytics-document.test.tsindex 80a6bef5d..5419557ce 100644--- a/src/web/src/lib/product-analytics-document.test.ts+++ b/src/web/src/lib/product-analytics-document.test.ts@@ -2,6 +2,7 @@ import { readFileSync } from "node:fs"import { runInNewContext } from "node:vm"import { describe, expect, it } from "vitest"import { PRODUCT_ANALYTICS_EVENTS } from "./product-analytics-contract"+import { authFailureParams } from "./auth-failure"function documentSender() {const html = readFileSync(new URL("../../public/analytics-events.html", import.meta.url), "utf8")@@ -35,3 +36,29 @@ it("retains the existing allowed authentication dimensions in the actual event dexpect(Array.from(document.dataLayer[1])[2]).toMatchObject({ method: "github", failure_stage: "social_start" })expect(Array.from(document.dataLayer[2])[2]).toMatchObject({ cta_name: "get_started" })})++it.each([+ { code: "PROVIDER_NOT_FOUND", status: 404 },+ { code: "INVALID_ORIGIN", status: 403 },+ { code: "INVALID_OTP", status: 400 },+ { code: "OTP_EXPIRED", status: 400 },+ { status: 429 },+ { status: 503 },+ new TypeError("Failed to fetch"),+ new Error("[email protected] token=secret"),+])("keeps the normalized failure reason through the actual outgoing GA command: %j", error => {+ const document = documentSender()+ const params = authFailureParams(error)+ document.sendProductEvent({ event: "sign_in_failed", params: { method: "google", failure_stage: "social_start", ...params } })+ expect(Array.from(document.dataLayer[0])[2]).toMatchObject(params)+ expect(JSON.stringify(document.dataLayer)).not.toContain("[email protected]")+ expect(JSON.stringify(document.dataLayer)).not.toContain("token=secret")+})++it("does not trust arbitrary reason text even with an approved error code", () => {+ const document = documentSender()+ document.sendProductEvent({ event: "sign_in_failed", params: { error_code: "INVALID_OTP", error_reason: "[email protected]", http_status: "400?token=secret" } })+ expect(Array.from(document.dataLayer[0])[2]).toMatchObject({ error_code: "INVALID_OTP", error_reason: "Invalid OTP" })+ expect(JSON.stringify(document.dataLayer)).not.toContain("[email protected]")+ expect(JSON.stringify(document.dataLayer)).not.toContain("secret")+})diff --git a/src/web/src/lib/product-analytics.dom.test.ts b/src/web/src/lib/product-analytics.dom.test.tsindex 7acceb032..b2621b81e 100644--- a/src/web/src/lib/product-analytics.dom.test.ts+++ b/src/web/src/lib/product-analytics.dom.test.ts@@ -27,6 +27,13 @@ function loadFrame() {}describe("frontend product analytics", () => {+ it("sends the safe authentication reason through the existing analytics document", async () => {+ await captureProductAnalytics({ event: "sign_in_failed", method: "google", failureStage: "social_start", error: { code: "INVALID_ORIGIN", status: 403, message: "[email protected] token=secret" } })+ const { send } = loadFrame()+ expect(send).toHaveBeenCalledExactlyOnceWith({ event: "sign_in_failed", params: { method: "google", failure_stage: "social_start", error_code: "INVALID_ORIGIN", error_reason: "Invalid origin", http_status: "403" } })+ expect(fetch).not.toHaveBeenCalled()+ })+it("uses only a static GA4 document and never the backend collection endpoint", async () => {await captureProductAnalytics({ event: "login_page_view" })const { frame, send } = loadFrame()@@ -183,11 +190,11 @@ it.each(["active", "retired", "reactivated"] as const)("qualifies delayed onboarit.each([{ event: "sign_up", method: "email" },{ event: "sign_in_success", method: "google" },- { event: "sign_in_failed", method: "github", failureStage: "social_start" },+ { event: "sign_in_failed", method: "github", failureStage: "social_start", error: { code: "PROVIDER_NOT_FOUND", status: 404 } },] as const)("retains the existing $event dimensions", async input => {await captureProductAnalytics(input)const { send } = loadFrame()- expect(send).toHaveBeenCalledWith({ event: input.event, params: { method: input.method, ...("failureStage" in input ? { failure_stage: input.failureStage } : {}) } })+ expect(send).toHaveBeenCalledWith({ event: input.event, params: { method: input.method, ...("failureStage" in input ? { failure_stage: input.failureStage, error_code: "PROVIDER_NOT_FOUND", error_reason: "Provider not found", http_status: "404" } : {}) } })})it("does not send a detached event frame after consent is granted again", async () => {diff --git a/src/web/src/lib/product-analytics.ts b/src/web/src/lib/product-analytics.tsindex d5b5aeed5..e57a46f6c 100644--- a/src/web/src/lib/product-analytics.ts+++ b/src/web/src/lib/product-analytics.ts@@ -3,12 +3,13 @@import { isTauri } from "@alook/shared"import { googleCommand, isPublicAnalyticsPath, readAnalyticsConsent } from "@/lib/analytics-consent"import { LANDING_CTA_NAMES, PRODUCT_ANALYTICS_EVENTS } from "@/lib/product-analytics-contract"+import { authFailureParams } from "@/lib/auth-failure"export type ProductAnalyticsInput =| { event: "landing_cta_clicked"; ctaName: string }| { event: "login_page_view" }| { event: "sign_up" | "sign_in_success"; method: string }- | { event: "sign_in_failed"; method: string; failureStage: "send_code" | "verify_code" | "social_start" | "provider_callback" }+ | { event: "sign_in_failed"; method: string; failureStage: "send_code" | "verify_code" | "social_start" | "provider_callback"; error: unknown }| { event: "onboarding_enter_clicked"; serverId: string }| { event: "onboarding_first_message" }| { event: "invitation_link_visit"; token: string }@@ -47,6 +48,7 @@ export async function captureProductAnalytics(input: ProductAnalyticsInput, asseif (input.event === "landing_cta_clicked" && LANDING_CTA_NAMES.some(name => name === input.ctaName)) params.cta_name = input.ctaNameif ((input.event === "sign_up" || input.event === "sign_in_success" || input.event === "sign_in_failed") && ["email", "email_otp", "github", "google", "apple", "development_password"].includes(input.method)) params.method = input.methodif (input.event === "sign_in_failed" && ["send_code", "verify_code", "social_start", "provider_callback"].includes(input.failureStage)) params.failure_stage = input.failureStage+ if (input.event === "sign_in_failed") Object.assign(params, authFailureParams(input.error))if (isPublicAnalyticsPath(window.location.pathname)) {googleCommand("event", input.event, { ...params, send_to: "G-STBCL8F4ZY" })return