🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(annotation): handle deletes gracefully and protect against race conditions (#6848)
marliessophie8b94e8a3bd9e
diff --git a/web/src/features/scores/components/AnnotateDrawerContent.tsx b/web/src/features/scores/components/AnnotateDrawerContent.tsxindex 5730a5d261..01e4ee38b0 100644--- a/web/src/features/scores/components/AnnotateDrawerContent.tsx+++ b/web/src/features/scores/components/AnnotateDrawerContent.tsx@@ -251,6 +251,9 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({}, [emptySelectedConfigIds, scores, configs, scoreTarget, form]);const pendingCreates = useRef(new Map<number, Promise<APIScoreV2>>());+ const pendingDeletes = useRef(new Set<string>());+ // Track when deletion was initiated for each score ID+ const deletionTimestamps = useRef(new Map<string, number>());const description = formatAnnotateDescription(scoreTarget);async function handleScoreChange(@@ -259,6 +262,24 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({value: number,stringValue: string | null,) {+ // Check if this score is currently being deleted+ if (score.scoreId && pendingDeletes.current.has(score.scoreId)) {+ // Skip updates for scores that are being deleted+ return;+ }++ // Check if there was a recent deletion request for this score+ if (score.scoreId && deletionTimestamps.current.has(score.scoreId)) {+ const deleteTime = deletionTimestamps.current.get(score.scoreId) || 0;+ const now = Date.now();+ // If deletion was requested in the last 5 seconds, ignore updates+ if (now - deleteTime < 5000) {+ return;+ }+ // Otherwise clear the old timestamp+ deletionTimestamps.current.delete(score.scoreId);+ }+// Optimistically update the UIsetOptimisticScore({index,@@ -450,6 +471,7 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({index: fields.length,value: null,stringValue: null,+ scoreId: null,name,dataType,configId: id,@@ -800,6 +822,7 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({index,value: numValue,stringValue: null,+ scoreId: null,});field.onChange(numValue);}}@@ -935,19 +958,50 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({loading={deleteMutation.isLoading}onClick={async () => {if (score.scoreId) {+ // Record deletion timestamp+ deletionTimestamps.current.set(+ score.scoreId,+ Date.now(),+ );+setOptimisticScore({index,value: null,stringValue: null,+ scoreId: null,});- await deleteMutation.mutateAsync({- id: score.scoreId,- projectId,- });- capture("score:delete", analyticsData);- form.clearErrors(- `scoreData.${index}.value`,++ // Track pending delete+ pendingDeletes.current.add(+ score.scoreId,);++ try {+ await deleteMutation.mutateAsync({+ id: score.scoreId,+ projectId,+ });+ capture(+ "score:delete",+ analyticsData,+ );+ form.clearErrors(+ `scoreData.${index}.value`,+ );++ // Update the form with the new ID+ update(index, {+ ...score,+ scoreId: undefined,+ value: null,+ stringValue: undefined,+ });+ } finally {+ // Clean up pending delete tracking+ pendingDeletes.current.delete(+ score.scoreId,+ );+ }}}}>@@ -975,17 +1029,45 @@ export function AnnotateDrawerContent<Target extends ScoreTarget>({}onClick={async () => {if (score.scoreId) {+ // Record deletion timestamp+ deletionTimestamps.current.set(+ score.scoreId,+ Date.now(),+ );+setOptimisticScore({index,value: null,stringValue: null,+ scoreId: null,});- await deleteMutation.mutateAsync({- id: score.scoreId,- projectId,- });- capture("score:delete", analyticsData);- form.clearErrors(`scoreData.${index}.value`);++ // Track pending delete+ pendingDeletes.current.add(score.scoreId);++ try {+ await deleteMutation.mutateAsync({+ id: score.scoreId,+ projectId,+ });+ capture("score:delete", analyticsData);+ form.clearErrors(+ `scoreData.${index}.value`,+ );++ // Update the form with the new ID+ update(index, {+ ...score,+ scoreId: undefined,+ value: null,+ stringValue: undefined,+ });+ } finally {+ // Clean up pending delete tracking+ pendingDeletes.current.delete(+ score.scoreId,+ );+ }}}}>diff --git a/web/src/features/scores/hooks/useScoreMutations.ts b/web/src/features/scores/hooks/useScoreMutations.tsindex 4d622f877b..3ed5e350db 100644--- a/web/src/features/scores/hooks/useScoreMutations.ts+++ b/web/src/features/scores/hooks/useScoreMutations.ts@@ -11,16 +11,12 @@ import { isTraceScore } from "@/src/features/scores/lib/helpers";const onTraceScoreSettledUpsert =({- projectId,- traceId,utils,fields,update,isDrawerOpen,setShowSaving,}: {- projectId: string;- traceId: string;utils: ReturnType<typeof api.useUtils>;fields: FieldArrayWithId<AnnotateFormSchemaType, "scoreData", "id">[];update: UseFieldArrayUpdate<AnnotateFormSchemaType>;@@ -45,17 +41,9 @@ const onTraceScoreSettledUpsert =comment: comment ?? undefined,});- console.log("invalidating traces");-await Promise.all([utils.scores.invalidate(),- utils.traces.byIdWithObservationsAndScores.invalidate(- { projectId, traceId },- {- type: "all",- refetchType: "all",- },- ),+ utils.traces.byIdWithObservationsAndScores.invalidate(),utils.sessions.invalidate(),]);@@ -101,11 +89,13 @@ const onScoreSettledDelete =});}- await Promise.all([- utils.scores.invalidate(),- utils.traces.invalidate(),- utils.sessions.invalidate(),- ]);+ await Promise.all(+ [+ utils.scores.invalidate(),+ utils.traces.byIdWithObservationsAndScores.invalidate(),+ utils.sessions.invalidate(),+ ].filter(Boolean),+ );if (!isDrawerOpen) setShowSaving(false);};@@ -164,8 +154,6 @@ export function useScoreMutations(const onSettledUpsert = isTraceScore(scoreTarget)? onTraceScoreSettledUpsert({- projectId,- traceId: scoreTarget.traceId,utils,fields,update,@@ -200,7 +188,7 @@ export function useScoreMutations(});const deleteMutation = api.scores.deleteAnnotationScore.useMutation({- onSuccess: onSettledDelete,+ onSettled: onSettledDelete,});return {diff --git a/web/src/features/scores/hooks/useScoreValues.ts b/web/src/features/scores/hooks/useScoreValues.tsindex 9ee0305d65..e977304218 100644--- a/web/src/features/scores/hooks/useScoreValues.ts+++ b/web/src/features/scores/hooks/useScoreValues.ts@@ -17,6 +17,7 @@ export function useScoreValues({name?: string | null;dataType?: ScoreDataType | null;configId?: string | null;+ scoreId?: string | null;}>(getValues().scoreData, (state, updatedScore) => {const stateCopy = state.map((score, idx) =>@@ -25,6 +26,7 @@ export function useScoreValues({...score,value: updatedScore.value,stringValue: updatedScore.stringValue ?? undefined,+ scoreId: updatedScore.scoreId ?? undefined,}: score,);@@ -36,6 +38,7 @@ export function useScoreValues({configId: updatedScore.configId ?? undefined,value: updatedScore.value,stringValue: updatedScore.stringValue ?? undefined,+ scoreId: updatedScore.scoreId ?? undefined,};return [...stateCopy, newScore];}