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(llma): preserve prompt edits on publish version conflict (#67895)

Juraj Majerike82652f5797c
diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
index 22d2813a5cb4..8114b760c62d 100644
--- a/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.test.ts
@@ -1,8 +1,10 @@
import { expectLogic } from 'kea-test-utils'
+import api from '~/lib/api'
+import { ApiError } from '~/lib/api-error'
import { EventsQuery, NodeKind, TracesQuery } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
-import { PropertyFilterType, PropertyOperator } from '~/types'
+import { LLMPromptResolveResponse, PropertyFilterType, PropertyOperator } from '~/types'
import { PromptAnalyticsScope, PromptMode, llmPromptLogic } from './llmPromptLogic'
@@ -251,6 +253,42 @@ describe('llmPromptLogic', () => {
logic.unmount()
})
+ it('preserves form edits and advances the base version on a publish conflict', async () => {
+ const { versions, has_more, ...promptFields } = mockPrompt
+ const conflictingLatest = {
+ ...promptFields,
+ id: 'prompt-version-3',
+ prompt: 'Someone else edited this prompt.',
+ version: 3,
+ latest_version: 3,
+ version_count: 3,
+ }
+
+ jest.spyOn(api.llmPrompts, 'resolveByName')
+ .mockResolvedValueOnce({ prompt: promptFields, versions, has_more } as unknown as LLMPromptResolveResponse)
+ .mockResolvedValue({ prompt: conflictingLatest, versions, has_more } as unknown as LLMPromptResolveResponse)
+ jest.spyOn(api.llmPrompts, 'update').mockRejectedValue(
+ new ApiError('conflict', 409, undefined, { detail: 'The prompt changed since you opened it.' })
+ )
+
+ const logic = llmPromptLogic({ promptName: 'my-test-prompt' })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadPromptSuccess'])
+
+ logic.actions.setMode(PromptMode.Edit)
+ logic.actions.setPromptFormValues({ name: 'my-test-prompt', prompt: 'My in-progress edit.' })
+
+ logic.actions.submitPromptForm()
+ await expectLogic(logic).toDispatchActions(['submitPromptFormFailure'])
+
+ expect(logic.values.promptForm.prompt).toBe('My in-progress edit.')
+ expect(logic.values.publishConflict).toEqual({ latestVersion: 3 })
+ expect(logic.values.prompt).toMatchObject({ latest_version: 3 })
+ expect(logic.values.mode).toBe(PromptMode.Edit)
+
+ logic.unmount()
+ })
+
it('resets new prompt form values after unmount and remount', async () => {
const firstMount = llmPromptLogic({ promptName: 'new' })
firstMount.mount()
diff --git a/products/ai_observability/frontend/prompts/llmPromptLogic.ts b/products/ai_observability/frontend/prompts/llmPromptLogic.ts
index faf053a58b4d..5441ad4dbb75 100644
--- a/products/ai_observability/frontend/prompts/llmPromptLogic.ts
+++ b/products/ai_observability/frontend/prompts/llmPromptLogic.ts
@@ -85,7 +85,11 @@ const PROMPT_VERSIONS_LIMIT = 50
export const PROMPT_NAME_MAX_LENGTH = 255
const DEFAULT_PROMPT_ANALYTICS_DATE_FROM = '-1d'
const STALE_PROMPT_ERROR_MESSAGE =
- 'This prompt changed while you were editing it. Review the latest version and try again.'
+ 'This prompt changed while you were editing it. Your edits are preserved — review the latest version and publish again.'
+
+export interface PublishConflict {
+ latestVersion: number | null
+}
async function fetchResolvedPrompt(
promptName: string,
@@ -164,6 +168,7 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
toggleMarkdownRendering: true,
setCompareVersion: (compareVersion: number | null) => ({ compareVersion }),
toggleOutlineExpanded: true,
+ setPublishConflict: (publishConflict: PublishConflict | null) => ({ publishConflict }),
}),
reducers(({ props }) => ({
@@ -227,6 +232,14 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
toggleOutlineExpanded: (state) => !state,
},
],
+ publishConflict: [
+ null as PublishConflict | null,
+ {
+ setPublishConflict: (_, { publishConflict }) => publishConflict,
+ setMode: () => null,
+ loadPromptSuccess: () => null,
+ },
+ ],
})),
loaders(({ props }) => ({
@@ -335,11 +348,17 @@ export const llmPromptLogic = kea<llmPromptLogicType>([
}
if (error instanceof ApiError && error.status === 409) {
+ // Refresh the underlying prompt so base_version advances, but keep the
+ // user's in-progress edits in the form — never overwrite their work.
+ let latestVersion: number | null = null
try {
- await refreshLatestPromptState(props.promptName, actions)
+ const latestPrompt = await fetchResolvedPrompt(props.promptName)
+ actions.setPrompt(latestPrompt)
+ latestVersion = latestPrompt.latest_version ?? latestPrompt.version
} catch {}
- lemonToast.error(error.detail || STALE_PROMPT_ERROR_MESSAGE)
+ actions.setPublishConflict({ latestVersion })
+ lemonToast.error(STALE_PROMPT_ERROR_MESSAGE)
throw error
}
diff --git a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx b/products/ai_observability/frontend/prompts/promptSceneComponents.tsx
index 00ae029a6056..aa33a0079a24 100644
--- a/products/ai_observability/frontend/prompts/promptSceneComponents.tsx
+++ b/products/ai_observability/frontend/prompts/promptSceneComponents.tsx
@@ -478,11 +478,20 @@ export function PromptEditForm({
isHistoricalVersion: boolean
selectedVersion: number | null
}): JSX.Element {
- const { promptVariables, isNewPrompt, isRenderingMarkdown, promptForm } = useValues(llmPromptLogic)
+ const { promptVariables, isNewPrompt, isRenderingMarkdown, promptForm, publishConflict } = useValues(llmPromptLogic)
const { toggleMarkdownRendering } = useActions(llmPromptLogic)
return (
<div className="mt-4 max-w-3xl space-y-4">
+ {publishConflict ? (
+ <LemonBanner type="warning" data-attr="llma-prompt-publish-conflict-banner">
+ {publishConflict.latestVersion
+ ? `v${publishConflict.latestVersion} was published while you were editing.`
+ : 'This prompt changed while you were editing.'}{' '}
+ Your edits below are preserved — review them before publishing again.
+ </LemonBanner>
+ ) : null}
+
{isHistoricalVersion && selectedVersion ? (
<LemonBanner type="info">
You are publishing a new latest version from historical version v{selectedVersion}. The original