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.

feat(posthog_ai): add staff toggle for run thread debug logs (#68950)

Georgiy Tarasov4acf276ebb71
diff --git a/products/posthog_ai/frontend/logics/debugLogsLogic.test.ts b/products/posthog_ai/frontend/logics/debugLogsLogic.test.ts
new file mode 100644
index 000000000000..ebd92a7fb900
--- /dev/null
+++ b/products/posthog_ai/frontend/logics/debugLogsLogic.test.ts
@@ -0,0 +1,68 @@
+import { MOCK_DEFAULT_USER } from 'lib/api.mock'
+
+import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
+import { userLogic } from 'scenes/userLogic'
+
+import { initKeaTests } from '~/test/init'
+import type { UserType } from '~/types'
+
+import { debugLogsLogic } from './debugLogsLogic'
+
+describe('debugLogsLogic', () => {
+ let logic: ReturnType<typeof debugLogsLogic.build>
+
+ function setup({ user, isDev }: { user: Partial<UserType>; isDev: boolean }): void {
+ userLogic.mount()
+ userLogic.actions.loadUserSuccess({
+ ...MOCK_DEFAULT_USER,
+ is_staff: false,
+ is_impersonated: false,
+ ...user,
+ } as UserType)
+ preflightLogic.mount()
+ preflightLogic.actions.loadPreflightSuccess({ is_debug: isDev } as any)
+ logic = debugLogsLogic()
+ logic.mount()
+ }
+
+ beforeEach(() => {
+ localStorage.clear()
+ initKeaTests()
+ })
+
+ afterEach(() => {
+ logic?.unmount()
+ })
+
+ it('defaults to showing debug logs for staff with no stored preference', () => {
+ setup({ user: { is_staff: true }, isDev: false })
+ expect(logic.values.debugLogsEnabled).toBe(true)
+ expect(logic.values.showDebugLogs).toBe(true)
+ })
+
+ it('offers no toggle to a staff user who is also impersonating, but still forces logs on', () => {
+ // The toggle would be a no-op during impersonation (logs are force-shown), so it must not render.
+ setup({ user: { is_staff: true, is_impersonated: true }, isDev: false })
+ expect(logic.values.canControlDebugLogs).toBe(false)
+ expect(logic.values.showDebugLogs).toBe(true)
+ })
+
+ it.each([
+ { name: 'staff, toggle on', user: { is_staff: true }, isDev: false, enabled: true, expected: true },
+ { name: 'staff, toggle off', user: { is_staff: true }, isDev: false, enabled: false, expected: false },
+ { name: 'local dev, toggle on', user: {}, isDev: true, enabled: true, expected: true },
+ { name: 'local dev, toggle off', user: {}, isDev: true, enabled: false, expected: false },
+ {
+ name: 'impersonation overrides toggle off',
+ user: { is_impersonated: true },
+ isDev: false,
+ enabled: false,
+ expected: true,
+ },
+ { name: 'non-staff, non-dev never sees', user: {}, isDev: false, enabled: true, expected: false },
+ ])('showDebugLogs — $name', ({ user, isDev, enabled, expected }) => {
+ setup({ user, isDev })
+ logic.actions.setDebugLogsEnabled(enabled)
+ expect(logic.values.showDebugLogs).toBe(expected)
+ })
+})
diff --git a/products/posthog_ai/frontend/logics/debugLogsLogic.ts b/products/posthog_ai/frontend/logics/debugLogsLogic.ts
new file mode 100644
index 000000000000..791a802f5e84
--- /dev/null
+++ b/products/posthog_ai/frontend/logics/debugLogsLogic.ts
@@ -0,0 +1,51 @@
+import { actions, connect, kea, path, reducers, selectors } from 'kea'
+
+import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
+import { userLogic } from 'scenes/userLogic'
+
+import type { debugLogsLogicType } from './debugLogsLogicType'
+
+/**
+ * Staff-only preference for whether `_posthog/console` debug rows surface in the run thread. Persisted
+ * to localStorage and shared across every run surface (singleton, unkeyed), enabled by default. An
+ * impersonated session always forces debug logs on, independent of the persisted toggle, so an engineer
+ * debugging a customer's session never has them silently hidden.
+ */
+export const debugLogsLogic = kea<debugLogsLogicType>([
+ path(['products', 'posthog_ai', 'frontend', 'logics', 'debugLogsLogic']),
+ connect(() => ({
+ values: [userLogic, ['user'], preflightLogic, ['isDev']],
+ })),
+ actions({
+ setDebugLogsEnabled: (enabled: boolean) => ({ enabled }),
+ }),
+ reducers({
+ debugLogsEnabled: [
+ true,
+ { persist: true, storageKey: 'posthog_ai.debugLogsEnabled' },
+ {
+ setDebugLogsEnabled: (_, { enabled }) => enabled,
+ },
+ ],
+ }),
+ selectors({
+ /**
+ * Who may see and toggle debug logs: staff or local dev, but never an impersonated session.
+ * Impersonation force-shows debug logs unconditionally (see `showDebugLogs`), so surfacing a
+ * toggle there would be a no-op that contradicts that invariant — exclude it here instead.
+ */
+ canControlDebugLogs: [
+ (s) => [s.user, s.isDev],
+ (user, isDev): boolean => !user?.is_impersonated && (!!user?.is_staff || !!isDev),
+ ],
+ /**
+ * Whether debug rows should actually render. Impersonation always shows them; otherwise staff/dev
+ * see them subject to the persisted toggle (on by default); everyone else never does.
+ */
+ showDebugLogs: [
+ (s) => [s.user, s.canControlDebugLogs, s.debugLogsEnabled],
+ (user, canControlDebugLogs, debugLogsEnabled): boolean =>
+ !!user?.is_impersonated || (canControlDebugLogs && debugLogsEnabled),
+ ],
+ }),
+])
diff --git a/products/posthog_ai/frontend/logics/runStreamLogic.ts b/products/posthog_ai/frontend/logics/runStreamLogic.ts
index cc4c4eead961..62265621d284 100644
--- a/products/posthog_ai/frontend/logics/runStreamLogic.ts
+++ b/products/posthog_ai/frontend/logics/runStreamLogic.ts
@@ -48,6 +48,7 @@ import {
isSessionUpdateUserMessage,
isTaskRunStateFrame,
} from '../types/wireTypes'
+import { debugLogsLogic } from './debugLogsLogic'
import type { runStreamLogicType } from './runStreamLogicType'
export type RunSseStatus = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed' | 'error'
@@ -1185,7 +1186,7 @@ export function foldLogToThread(entries: StoredEntry[], options: { isResumeRun:
* Whether a folded item renders any content. Empty priming thoughts and step-less progress rows fold
* into the thread but render nothing; drop them here so a virtualized consumer never reserves an empty,
* gap-padded row. Tool items are always paired with an invocation (see `upsertInvocationItem`), and
- * `debug` rows are gated separately by `showDebugItems`, so neither needs a content check here.
+ * `debug` rows are gated separately by `showDebugLogs`, so neither needs a content check here.
*/
function rendersThreadItemContent(item: ThreadItem): boolean {
switch (item.type) {
@@ -1229,6 +1230,8 @@ export const runStreamLogic = kea<runStreamLogicType>([
['isDev'],
userLogic,
['user'],
+ debugLogsLogic,
+ ['showDebugLogs'],
],
})),
actions({
@@ -1663,23 +1666,16 @@ export const runStreamLogic = kea<runStreamLogicType>([
(s) => [s.log, s.isBootstrapResumeRun],
(log, isResumeRun): FoldedThread => foldLogToThread(log.entries, { isResumeRun }),
],
- /**
- * Whether `_posthog/console` debug rows should surface in the thread. Derived from the current
- * user's staff/impersonation flag and dev environment, so debug items are filtered out of
- * `threadItems` for non-privileged users — never reaching the virtualizer.
- */
- showDebugItems: [
- (s) => [s.user, s.isDev],
- (user, isDev): boolean => !!user?.is_staff || !!user?.is_impersonated || !!isDev,
- ],
threadItems: [
- (s) => [s.foldedThread, s.showDebugItems],
- (foldedThread, showDebugItems): ThreadItem[] =>
+ (s) => [s.foldedThread, s.showDebugLogs],
+ (foldedThread, showDebugLogs): ThreadItem[] =>
// Filtering lives here, not in the renderer: a row the renderer would return `null` for
// (a content-less item, or a debug row a non-privileged user can't see) still reserves an
// empty, gap-padded slot in the virtualized thread. Drop them before they become rows.
+ // Debug rows are gated by `debugLogsLogic.showDebugLogs` (staff/dev toggle, force-on when
+ // impersonating).
foldedThread.threadItems.filter(
- (item: ThreadItem) => (item.type !== 'debug' || showDebugItems) && rendersThreadItemContent(item)
+ (item: ThreadItem) => (item.type !== 'debug' || showDebugLogs) && rendersThreadItemContent(item)
),
],
toolInvocations: [
diff --git a/products/posthog_ai/frontend/messages/DebugMessage.tsx b/products/posthog_ai/frontend/messages/DebugMessage.tsx
index 86ce2c9612b4..4e5a07d42147 100644
--- a/products/posthog_ai/frontend/messages/DebugMessage.tsx
+++ b/products/posthog_ai/frontend/messages/DebugMessage.tsx
@@ -17,8 +17,10 @@ const LEVEL_STYLES: Record<string, string> = {
}
/**
- * Staff-only inline console line rendered from `_posthog/console` wire frames. Not a chat bubble —
- * a muted, monospace debug row visually distinct from assistant failures (`AssistantFailureMessage`).
+ * Inline console line rendered from `_posthog/console` wire frames. Not a chat bubble — a muted,
+ * monospace debug row visually distinct from assistant failures (`AssistantFailureMessage`). Whether
+ * these rows surface at all is gated upstream by `debugLogsLogic.showDebugLogs` (staff/dev toggle,
+ * always on when impersonating).
*/
export const DebugMessage = memo(function DebugMessage({ text, level }: DebugMessageProps): JSX.Element {
const levelStyle = LEVEL_STYLES[level] ?? LEVEL_STYLES.info
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskDebugLogsMenu.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskDebugLogsMenu.tsx
new file mode 100644
index 000000000000..a78dd67b71cb
--- /dev/null
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskDebugLogsMenu.tsx
@@ -0,0 +1,31 @@
+import { useActions, useValues } from 'kea'
+
+import { SceneMenuBarCheckboxItem, SceneMenuBarMenu } from '~/layout/scenes/components/SceneMenuBar'
+
+import { debugLogsLogic } from '../../../logics/debugLogsLogic'
+
+/**
+ * Staff-only menu exposing the debug-logs toggle for the run thread. Renders nothing unless the current
+ * user may control debug logs (staff or local dev) — impersonated sessions force debug logs on and have
+ * no toggle. The checkmark reflects the persisted (localStorage) preference, on by default.
+ */
+export function TaskDebugLogsMenu(): JSX.Element | null {
+ const { canControlDebugLogs, debugLogsEnabled } = useValues(debugLogsLogic)
+ const { setDebugLogsEnabled } = useActions(debugLogsLogic)
+
+ if (!canControlDebugLogs) {
+ return null
+ }
+
+ return (
+ <SceneMenuBarMenu label="Staff only" dataAttr="task-menubar-staff">
+ <SceneMenuBarCheckboxItem
+ checked={debugLogsEnabled}
+ onCheckedChange={(checked) => setDebugLogsEnabled(!!checked)}
+ data-attr="task-menubar-show-debug-logs"
+ >
+ Show debug logs
+ </SceneMenuBarCheckboxItem>
+ </SceneMenuBarMenu>
+ )
+}
diff --git a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunSceneShell.tsx b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunSceneShell.tsx
index 5429ee5ba6d7..8dd984b9568b 100644
--- a/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunSceneShell.tsx
+++ b/products/posthog_ai/frontend/scenes/TaskTracker/components/TaskRunSceneShell.tsx
@@ -24,6 +24,7 @@ import {
} from '~/layout/scenes/SceneLayout'
import { Task, TaskRun } from '../../../types/taskTypes'
+import { TaskDebugLogsMenu } from './TaskDebugLogsMenu'
import { TaskPanelSkeleton, TaskRunMetadataSkeleton } from './taskDetailSkeletons'
import { TaskErrorBanner } from './TaskErrorBanner'
import { TaskRunMetadata } from './TaskRunMetadata'
@@ -77,6 +78,7 @@ export function TaskRunSceneShell({
Archive task
</SceneMenuBarItem>
</SceneMenuBarMenu>
+ <TaskDebugLogsMenu />
</SceneMenuBar>
)}
<ScenePanel>