Skip to content

: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.

fix(prompts): discard stale resolved-preview responses (#114040)

Juraj Majerike92253460f81
diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
index 91384a9f652b..1d61a6f046da 100644
--- a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
@@ -15,9 +15,10 @@ import {
llmPromptsNameLabelsDestroy,
llmPromptsNameLabelsUpdate,
llmPromptsNamePartialUpdate,
+ llmPromptsNameRetrieve,
llmPromptsResolveNameRetrieve,
} from '../generated/api'
-import type { LLMPromptApi, LLMPromptResolveResponseApi } from '../generated/api.schemas'
+import type { LLMPromptApi, LLMPromptPublicApi, LLMPromptResolveResponseApi } from '../generated/api.schemas'
import { PromptAnalyticsScope, PromptMode, llmPromptLogic } from './llmPromptLogic'
import type { ResolvedLLMPrompt } from './llmPromptLogic'
import { validatePromptLabelName } from './utils'
@@ -26,6 +27,7 @@ jest.mock('../generated/api', () => ({
llmPromptsNameLabelsUpdate: jest.fn(),
llmPromptsNameLabelsDestroy: jest.fn(),
llmPromptsNamePartialUpdate: jest.fn(),
+ llmPromptsNameRetrieve: jest.fn(),
llmPromptsResolveNameRetrieve: jest.fn(),
}))
@@ -33,6 +35,7 @@ const mockLabelsUpdate = llmPromptsNameLabelsUpdate as jest.MockedFunction<typeo
const mockLabelsDestroy = llmPromptsNameLabelsDestroy as jest.MockedFunction<typeof llmPromptsNameLabelsDestroy>
const mockPartialUpdate = llmPromptsNamePartialUpdate as jest.MockedFunction<typeof llmPromptsNamePartialUpdate>
const mockResolve = llmPromptsResolveNameRetrieve as jest.MockedFunction<typeof llmPromptsResolveNameRetrieve>
+const mockNameRetrieve = llmPromptsNameRetrieve as jest.MockedFunction<typeof llmPromptsNameRetrieve>
const mockPrompt = {
id: 'prompt-version-2',
@@ -328,6 +331,38 @@ describe('llmPromptLogic', () => {
logic.unmount()
})
+ it('discards a stale resolved-preview response once a newer load started', async () => {
+ const { versions, has_more, ...promptFields } = mockPrompt
+ mockResolve.mockResolvedValue({
+ prompt: promptFields,
+ versions,
+ has_more,
+ } as unknown as LLMPromptResolveResponseApi)
+ let resolveStale: (value: LLMPromptPublicApi) => void = () => {}
+ let resolveFresh: (value: LLMPromptPublicApi) => void = () => {}
+ mockNameRetrieve
+ .mockImplementationOnce(() => new Promise((resolve) => (resolveStale = resolve)))
+ .mockImplementationOnce(() => new Promise((resolve) => (resolveFresh = resolve)))
+
+ const logic = llmPromptLogic({ promptName: 'my-test-prompt' })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadPromptSuccess'])
+
+ logic.actions.toggleResolvedPreview() // show: first fetch starts
+ logic.actions.toggleResolvedPreview() // hide while it is in flight
+ logic.actions.toggleResolvedPreview() // show again: second fetch starts
+
+ resolveFresh({ prompt: 'fresh resolved' } as LLMPromptPublicApi)
+ await expectLogic(logic).toDispatchActions(['loadResolvedPreviewSuccess'])
+ expect(logic.values.resolvedPreview?.prompt).toBe('fresh resolved')
+
+ resolveStale({ prompt: 'stale resolved' } as LLMPromptPublicApi)
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.resolvedPreview?.prompt).toBe('fresh resolved')
+
+ logic.unmount()
+ })
+
it('drops prompt-scene params from the breadcrumb link back to the list', async () => {
router.actions.push('/prompt-management/prompts/existing-prompt', {
edit: true,
diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.ts
index ca49ca1c0135..cdcdff9e49af 100644
--- a/products/ai_observability/frontend/prompts/llmPromptLogic.ts
+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.ts
@@ -336,7 +336,7 @@ export interface llmPromptLogicActions {
prompt: ResolvedLLMPrompt
payload?: any
}
- loadResolvedPreview: () => any
+ loadResolvedPreview: (_: any) => any
loadResolvedPreviewFailure: (
error: string,
errorObject?: any
@@ -628,9 +628,13 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
// content (navigation, version switch, refresh, failure) begins with
// loadPrompt, so clearing on the trigger closes the whole class of
// stale-preview states instead of patching individual outcomes.
+ // Cleared on loadResolvedPreview too: a late response for a previous
+ // version may have refilled it, and it would show while the new fetch
+ // is in flight.
resolvedPreview: {
loadPrompt: () => null,
setMode: () => null,
+ loadResolvedPreview: () => null,
},
isShowingResolvedPreview: [
false,
@@ -758,15 +762,19 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
loaders(({ props }) => ({
resolvedPreview: {
__default: null as LLMPromptPublicApi | null,
- loadResolvedPreview: async () => {
+ loadResolvedPreview: async (_, breakpoint) => {
// Version from the router, like loadPrompt: values.prompt still holds
// the previous version while a back/forward navigation is loading.
const urlVersion = getSelectedVersionFromUrl()
- return await llmPromptsNameRetrieve(
+ const response = await llmPromptsNameRetrieve(
String(ApiConfig.getCurrentTeamId()),
props.promptName,
urlVersion !== undefined ? { version: urlVersion } : undefined
)
+ // A newer load started while this one was in flight: discard this
+ // response, or the older version's content would display as resolved.
+ breakpoint()
+ return response
},
},
})),
@@ -1308,7 +1316,7 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
},
toggleResolvedPreview: () => {
if (values.isShowingResolvedPreview) {
- actions.loadResolvedPreview()
+ actions.loadResolvedPreview(null)
}
},
loadResolvedPreviewFailure: ({ errorObject }) => {