:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): keep trace feedback loading when a survey ID is malformed (#88264)
Carlos Marchal0e0d580eee21
diff --git a/products/ai_observability/frontend/feedback-view/FeedbackViewDisplay.tsx b/products/ai_observability/frontend/feedback-view/FeedbackViewDisplay.tsxindex 2a3a2a091a4e..9a0e5cfa8f23 100644--- a/products/ai_observability/frontend/feedback-view/FeedbackViewDisplay.tsx+++ b/products/ai_observability/frontend/feedback-view/FeedbackViewDisplay.tsx@@ -5,12 +5,10 @@ import { LemonBanner, LemonCard, LemonSkeleton, LemonSwitch, Link } from '@posthimport { teamLogic } from 'scenes/teamLogic'import { urls } from 'scenes/urls'-import { SurveyEventProperties } from '~/types'-import { aiObservabilityTraceLogic } from '../aiObservabilityTraceLogic'import { feedbackViewLogic } from './feedbackViewLogic'import { SurveyResponseCard } from './survey-responses/SurveyResponseCard'-import { groupEventsBySubmission } from './survey-responses/utils'+import { getSurveyIdFromEvent, groupEventsBySubmission } from './survey-responses/utils'import { FeedbackSurveyWizard } from './wizard/FeedbackSurveyWizard'export function FeedbackViewDisplay(): JSX.Element {@@ -37,7 +35,13 @@ export function FeedbackViewDisplay(): JSX.Element {// only have 'survey shown' events here if there was no survey responseconst surveyShownEvents = (surveyEvents ?? []).filter((e) => e.event === 'survey shown')- const hasSurveyEvents = groupedResponses.length > 0 || surveyShownEvents.length > 0+ // A response whose `$survey_id` matches no survey in this project has no questions to render+ // against, so it is dropped above. Say so, instead of leaving the trace looking uninstrumented.+ const hasUnmatchedResponses = surveyResponseEvents.some((event) => {+ const surveyId = getSurveyIdFromEvent(event)+ return !surveyId || !surveys[surveyId]+ })+ const hasSurveyEvents = groupedResponses.length > 0 || surveyShownEvents.length > 0 || hasUnmatchedResponses// no survey events at all -> survey wizard CTAif (!hasSurveyEvents) {@@ -46,6 +50,13 @@ export function FeedbackViewDisplay(): JSX.Element {return (<div className="flex flex-col gap-3">+ {hasUnmatchedResponses && (+ <LemonBanner type="warning">+ Some feedback on this trace points to a survey that isn't in this project, so it can't be shown.+ Check that the <code>$survey_id</code> you send with <code>survey sent</code> matches a survey here.+ </LemonBanner>+ )}+{hasSurveyEvents && !currentTeam?.surveys_opt_in && (<LemonBanner type="warning"><div className="flex items-center justify-between gap-2">@@ -69,7 +80,8 @@ export function FeedbackViewDisplay(): JSX.Element {})}{surveyShownEvents.map((event) => {- const survey = surveys[event.properties?.[SurveyEventProperties.SURVEY_ID]]+ const surveyId = getSurveyIdFromEvent(event)+ const survey = surveyId ? surveys[surveyId] : undefinedreturn (<LemonCard key={event.id} className="p-3" hoverEffect={false}><span className="text-secondary">diff --git a/products/ai_observability/frontend/feedback-view/feedbackViewLogic.test.ts b/products/ai_observability/frontend/feedback-view/feedbackViewLogic.test.tsnew file mode 100644index 000000000000..675e9389cb28--- /dev/null+++ b/products/ai_observability/frontend/feedback-view/feedbackViewLogic.test.ts@@ -0,0 +1,41 @@+import { LLMTraceEvent } from '~/queries/schema/schema-general'++import { extractValidSurveyIds } from './feedbackViewLogic'++const surveyEvent = (surveyId: unknown): LLMTraceEvent => ({+ id: `event-${String(surveyId)}`,+ event: 'survey sent',+ properties: { $survey_id: surveyId },+ createdAt: '2024-01-01T00:00:00Z',+})++describe('feedbackViewLogic: extractValidSurveyIds', () => {+ const validId = '0199ed4a-5c03-0000-3220-df21df612e95'+ const otherValidId = '0199ed4a-5c03-0000-3220-df21df612e96'++ it('collects unique survey IDs', () => {+ expect(extractValidSurveyIds([surveyEvent(validId), surveyEvent(otherValidId), surveyEvent(validId)])).toEqual([+ validId,+ otherValidId,+ ])+ })++ it.each([+ ['a placeholder from a copied snippet', 'your-survey-id'],+ ['a survey name', 'AI response feedback'],+ ['a quoted UUID', `'${validId}'`],+ ['an empty string', ''],+ ['a missing property', undefined],+ ])('drops %s', (_description, surveyId) => {+ expect(extractValidSurveyIds([surveyEvent(surveyId)])).toEqual([])+ expect(extractValidSurveyIds([surveyEvent(surveyId), surveyEvent(validId)])).toEqual([validId])+ })++ it('normalizes case so the survey lookup matches', () => {+ expect(extractValidSurveyIds([surveyEvent(validId.toUpperCase())])).toEqual([validId])+ })++ it('returns nothing before the survey events load', () => {+ expect(extractValidSurveyIds(null)).toEqual([])+ })+})diff --git a/products/ai_observability/frontend/feedback-view/feedbackViewLogic.ts b/products/ai_observability/frontend/feedback-view/feedbackViewLogic.tsindex 88406ef4a261..27ecb0951086 100644--- a/products/ai_observability/frontend/feedback-view/feedbackViewLogic.ts+++ b/products/ai_observability/frontend/feedback-view/feedbackViewLogic.ts@@ -4,17 +4,41 @@ import { subscriptions } from 'kea-subscriptions'import api from 'lib/api'import { dayjs } from 'lib/dayjs'+import { isUUIDLike } from 'lib/utils/guards'import { LLMTrace, LLMTraceEvent } from '~/queries/schema/schema-general'import { hogql } from '~/queries/utils'-import { Survey, SurveyEventProperties } from '~/types'+import { Survey } from '~/types'import { aiObservabilityTraceDataLogic } from '../aiObservabilityTraceDataLogic'+import { getSurveyIdFromEvent } from './survey-responses/utils'export interface FeedbackViewLogicProps {traceId: string}+/**+ * Collects the survey IDs to look up from the trace's survey events.+ *+ * Feedback surveys on a trace are captured by the app under test, so `$survey_id` holds+ * whatever that app sent. The surveys API filters on a list of UUIDs and rejects the whole+ * list when one entry is malformed, which would leave the tab with no feedback at all, so+ * drop the IDs that cannot belong to a survey.+ */+export function extractValidSurveyIds(surveyEvents: LLMTraceEvent[] | null): string[] {+ if (!surveyEvents) {+ return []+ }+ const ids = new Set<string>()+ for (const event of surveyEvents) {+ const surveyId = getSurveyIdFromEvent(event)+ if (surveyId && isUUIDLike(surveyId)) {+ ids.add(surveyId)+ }+ }+ return Array.from(ids)+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface feedbackViewLogicValues {trace: LLMTrace | undefined // aiObservabilityTraceDataLogic@@ -149,21 +173,11 @@ export const feedbackViewLogic = kea<feedbackViewLogicType>([],}),selectors({+ // Inline so kea-typegen can infer the selector; a bare function reference drops+ // `surveyIds` from the generated values.surveyIds: [(s) => [s.surveyEvents],- (surveyEvents: LLMTraceEvent[] | null): string[] => {- if (!surveyEvents) {- return []- }- const ids = new Set<string>()- for (const event of surveyEvents) {- const surveyId = event.properties?.[SurveyEventProperties.SURVEY_ID]- if (surveyId) {- ids.add(surveyId)- }- }- return Array.from(ids)- },+ (surveyEvents: LLMTraceEvent[] | null): string[] => extractValidSurveyIds(surveyEvents),],}),listeners(({ actions }) => ({diff --git a/products/ai_observability/frontend/feedback-view/survey-responses/utils.ts b/products/ai_observability/frontend/feedback-view/survey-responses/utils.tsindex 3f9a54f480be..f828d5751e25 100644--- a/products/ai_observability/frontend/feedback-view/survey-responses/utils.ts+++ b/products/ai_observability/frontend/feedback-view/survey-responses/utils.ts@@ -1,3 +1,4 @@+import { isString } from 'lib/utils/guards'import { getSurveyResponseValue } from 'scenes/surveys/utils'import { LLMTraceEvent } from '~/queries/schema/schema-general'@@ -10,15 +11,27 @@ export interface GroupedResponse {isComplete: boolean}+/**+ * Reads the survey ID off a survey event, lowercased.+ *+ * `$survey_id` is captured by the instrumented app, so its case is whatever that app sent, while+ * survey IDs from the API are canonical lowercase. Match on the normalized form or an uppercase+ * ID looks like a survey that isn't in the project.+ */+export function getSurveyIdFromEvent(event: LLMTraceEvent): string | null {+ const surveyId = event.properties?.[SurveyEventProperties.SURVEY_ID]+ return isString(surveyId) ? surveyId.toLowerCase() : null+}+export function groupEventsBySubmission(events: LLMTraceEvent[], surveys: Record<string, Survey>): GroupedResponse[] {const submissionMap = new Map<string, GroupedResponse>()for (const event of events) {const props = event.properties || {}- const surveyId = props[SurveyEventProperties.SURVEY_ID]+ const surveyId = getSurveyIdFromEvent(event)const survey = surveyId ? surveys[surveyId] : null- if (!survey) {+ if (!surveyId || !survey) {continue}