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(aio): prevent trace search stack overflow from URL sync loop (#71088)

Radu Raicea1b1af86d5982
diff --git a/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts b/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
index ee4f669ced23..419e33f890fd 100644
--- a/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
+++ b/products/ai_observability/frontend/aiObservabilityTraceLogic.test.ts
@@ -92,6 +92,26 @@ describe('aiObservabilityTraceLogic', () => {
})
})
+ it('does not write the search query back to the URL when it originates from the URL', async () => {
+ // Regression: setSearchQuery's listener wrote the query back to the URL, which
+ // re-fired urlToAction, which called setSearchQuery again. For a query that the URL
+ // builder and the browser encode/read differently (e.g. one containing a space, like
+ // "[cite] "), the value-only guard never converged and the two recursed until the
+ // stack overflowed. A URL-sourced query must not trigger a write-back.
+ const replaceSpy = jest.spyOn(router.actions, 'replace')
+ const traceId = 'trace-with-search'
+ const traceUrl = combineUrl(urls.aiObservabilityTrace(traceId, { search: '[cite] ' }))
+
+ router.actions.push(addProjectIdIfMissing(traceUrl.url, MOCK_TEAM_ID))
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.traceId).toBe(traceId)
+ expect(logic.values.searchQuery).toContain('cite')
+ expect(replaceSpy).not.toHaveBeenCalled()
+
+ replaceSpy.mockRestore()
+ })
+
describe('messageShowStates reducer', () => {
it('has correct initial state', () => {
expect(logic.values.messageShowStates).toEqual({
diff --git a/products/ai_observability/frontend/aiObservabilityTraceLogic.ts b/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
index e86fdcb8ae82..1b32b781f2b5 100644
--- a/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
+++ b/products/ai_observability/frontend/aiObservabilityTraceLogic.ts
@@ -487,7 +487,7 @@ export const aiObservabilityTraceLogic = kea<aiObservabilityTraceLogicType>([
],
}),
- listeners(({ actions, values }) => ({
+ listeners(({ actions, values, cache }) => ({
initializeMessageStates: ({ inputCount, outputCount }) => {
// Apply display option when initializing
const displayOption = values.displayOption
@@ -504,8 +504,17 @@ export const aiObservabilityTraceLogic = kea<aiObservabilityTraceLogicType>([
actions.applySearchResults(inputStates, outputStates)
},
setSearchQuery: ({ searchQuery }) => {
+ // When the query came from the URL (via urlToAction), don't write it back.
+ // The URL builder and the router's decoder don't round-trip every string
+ // identically (e.g. queries containing a space), so the value-only comparison
+ // below can stay unequal forever and recurse until the stack overflows. This
+ // re-entrancy flag breaks the loop regardless of encoding.
+ if (cache.settingSearchFromUrl) {
+ cache.settingSearchFromUrl = false
+ return
+ }
+
// Only update URL if the search query actually changed
- // This prevents infinite loop when setSearchQuery is called from urlToAction
const currentUrl = window.location.search
const urlParams = new URLSearchParams(currentUrl)
const currentSearchInUrl = urlParams.get('search') || ''
@@ -564,7 +573,7 @@ export const aiObservabilityTraceLogic = kea<aiObservabilityTraceLogicType>([
},
})),
- urlToAction(({ actions }) => ({
+ urlToAction(({ actions, cache }) => ({
[urls.aiObservabilityTrace(':id')]: ({ id }, { event, timestamp, exception_ts, search, line, tab, msg }) => {
actions.setTraceId(id ?? '')
void addProductIntent({
@@ -587,7 +596,9 @@ export const aiObservabilityTraceLogic = kea<aiObservabilityTraceLogicType>([
parsedDate.add(EXCEPTION_LOOKUP_WINDOW_MINUTES, 'minutes').toISOString()
)
}
- // Set search from URL param if provided, otherwise clear it
+ // Set search from URL param if provided, otherwise clear it.
+ // Mark it as URL-driven so the listener doesn't write it straight back to the URL.
+ cache.settingSearchFromUrl = true
actions.setSearchQuery(search || '')
},
})),