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(posthog_ai): show why the server refused to start a run (#104844)

posthog[bot]c98f567e60eb
diff --git a/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts b/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts
index 952703ce3aa9..52768432f056 100644
--- a/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts
+++ b/products/posthog_ai/frontend/logics/runInteractionLogic.test.ts
@@ -24,6 +24,8 @@ import { runStreamLogic } from './runStreamLogic'
import { TaskDraftPersistence, taskDraftStorageKey } from './taskDraftPersistence'
import { toolStreamEventsLogic } from './toolStreamEventsLogic'
+const GENERIC_FAILURE = 'Failed to start a new run. Please try again.'
+
// Minimal kea stub for the shared sandbox stream logic — gives the test full control over the busy gate
// (`isThinking`) and `currentRunStatus`, and lets us fire `markTurnComplete` and observe `pushHumanMessage`
// without the real SSE machinery.
@@ -1036,7 +1038,7 @@ describe('runInteractionLogic', () => {
expect(logic.values.composerForm.draft).toBe('continue from here\n\nand check the tests')
expect(logic.values.startingRun).toBe(false)
expect(onRunStarted).not.toHaveBeenCalled()
- expect(lemonToast.error).toHaveBeenCalledWith('Failed to start a new run. Please try again.')
+ expect(lemonToast.error).toHaveBeenCalledWith(GENERIC_FAILURE)
}
)
@@ -1170,11 +1172,23 @@ describe('runInteractionLogic', () => {
})
test.each([
- [new Error('boom'), 'Failed to start a new run. Please try again.'],
+ [new Error('boom'), GENERIC_FAILURE],
[
new ApiError('starting', 503, undefined, { code: 'warm_run_activation_unavailable' }),
"Couldn't start this run yet. Please try again.",
],
+ [
+ new ApiError('starting', 400, undefined, {
+ detail: 'A resumed run must use its previous base branch. Omit branch to resume.',
+ }),
+ 'A resumed run must use its previous base branch. Omit branch to resume.',
+ ],
+ [
+ new ApiError('starting', 402, undefined, { error: 'Your organization is over its usage limit.' }),
+ 'Your organization is over its usage limit.',
+ ],
+ [new ApiError('starting', 500, undefined, { detail: 'A server error occurred.' }), GENERIC_FAILURE],
+ [new ApiError('starting', 503, undefined, null), GENERIC_FAILURE],
])('keeps the draft and unsent context when starting a run fails with %s', async (error, message) => {
let rejectSend!: (error: unknown) => void
;(tasksRunCreate as jest.Mock).mockReturnValueOnce(
diff --git a/products/posthog_ai/frontend/logics/runInteractionLogic.ts b/products/posthog_ai/frontend/logics/runInteractionLogic.ts
index 40a19831431f..1f6ba2c64a83 100644
--- a/products/posthog_ai/frontend/logics/runInteractionLogic.ts
+++ b/products/posthog_ai/frontend/logics/runInteractionLogic.ts
@@ -15,7 +15,7 @@ import {
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
-import { ApiError } from 'lib/api-error'
+import { ApiError, readableErrorMessage } from 'lib/api-error'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
import { projectLogic } from 'scenes/projectLogic'
import { aiConsentLogic } from 'scenes/settings/organization/aiConsentLogic'
@@ -127,6 +127,29 @@ function isClearCommand(content: string): boolean {
return /^\/clear(?:\s|$)/.test(content)
}
+const GENERIC_RUN_START_FAILURE = 'Failed to start a new run. Please try again.'
+
+/**
+ * The run-create endpoint refuses for many distinct reasons and words each one for a person, so the
+ * body says whether to retry, change branch, or start over where the generic string cannot.
+ *
+ * Only a refusal below 500 speaks for itself. A 5xx is a fault on our side or in the gateway, and
+ * its body is either empty or an internal message. The warm-run retry code is the one 5xx
+ * exception, because the backend words that one for the user.
+ */
+function runStartFailureMessage(error: unknown): string {
+ if (!(error instanceof ApiError)) {
+ return GENERIC_RUN_START_FAILURE
+ }
+ if (error.code === 'warm_run_activation_unavailable') {
+ return "Couldn't start this run yet. Please try again."
+ }
+ if (error.status === undefined || error.status >= 500) {
+ return GENERIC_RUN_START_FAILURE
+ }
+ return readableErrorMessage(error.data) ?? GENERIC_RUN_START_FAILURE
+}
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface runInteractionLogicValues {
dataProcessingAccepted: boolean // aiConsentLogic
@@ -1506,11 +1529,7 @@ export const runInteractionLogic = kea<runInteractionLogicType>([
optimisticStarted = false
}
actions.finishTaskDraftDelivery()
- lemonToast.error(
- error instanceof ApiError && error.code === 'warm_run_activation_unavailable'
- ? "Couldn't start this run yet. Please try again."
- : 'Failed to start a new run. Please try again.'
- )
+ lemonToast.error(runStartFailureMessage(error))
} finally {
disposables.dispose('optimistic-resume')
if (isCurrent()) {