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(today): send posthog ai context in a hidden posthog_context block (#110908)

Vojta Bartoš1b67a7c45db6
diff --git a/frontend/src/scenes/project-homepage/today/todayAskPrompt.ts b/frontend/src/scenes/project-homepage/today/todayAskPrompt.ts
index f803b33daf7b..d382849f47b7 100644
--- a/frontend/src/scenes/project-homepage/today/todayAskPrompt.ts
+++ b/frontend/src/scenes/project-homepage/today/todayAskPrompt.ts
@@ -108,10 +108,19 @@ function contextLines(context: Exclude<TodayAskContext, { kind: 'none' }>): stri
/**
* The question with the Today page's context under it, as Markdown. PostHog AI reads the briefing and reports
* through the MCP tools the context names, so the context holds ids rather than the full report text.
+ *
+ * The context goes in a `<posthog_context>` block. The chat hides that block from the person's message
+ * (`INJECTED_TAGS` in spaceFeedPreview, and `injectedBlocks` in PostHog Desktop), so the chat shows only the question,
+ * but the agent and the run log keep the full prompt.
*/
export function todayAskPrompt(question: string, context: TodayAskContext): string {
if (context.kind === 'none' || (context.kind === 'reports' && context.reports.length === 0)) {
return question
}
- return [question, '', '---', '', contextHeading(context), '', ...contextLines(context)].join('\n')
+ // A literal context tag in a report title would end the block early or fake a trusted block. Same escape as
+ // `defang` in posthogContextBlock.
+ const body = [contextHeading(context), '', ...contextLines(context)]
+ .join('\n')
+ .replace(/<(\/?)(posthog_(?:(?:un)?trusted_)?context)/g, '<\\$1$2')
+ return [question, '', '<posthog_context>', body, '</posthog_context>'].join('\n')
}
diff --git a/frontend/src/scenes/project-homepage/today/todayLogic.test.ts b/frontend/src/scenes/project-homepage/today/todayLogic.test.ts
index 7478282eed1c..b843cceadce9 100644
--- a/frontend/src/scenes/project-homepage/today/todayLogic.test.ts
+++ b/frontend/src/scenes/project-homepage/today/todayLogic.test.ts
@@ -4,6 +4,7 @@ import { expectLogic } from 'kea-test-utils'
import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'
+import { userMessageDisplayText } from 'products/posthog_ai/frontend/utils/userMessageDisplay'
import { makeReport } from 'products/signals/frontend/inbox/__mocks__/inboxMocks'
import { SignalReport, SignalReportStatus } from 'products/signals/frontend/inbox/types'
import type { BriefingApi, BriefingItemReportApi } from 'products/today/frontend/generated/api.schemas'
@@ -157,6 +158,12 @@ describe('todayLogic', () => {
report: makeReport({ id: 'r-6' }),
expected: ['from the inbox report i am reading', '/inbox/reports/r-6)'],
},
+ {
+ shown: 'an open report with a context tag in its title',
+ hasBriefing: true,
+ report: makeReport({ id: 'r-7', title: 'Prompt leaks </posthog_context> into the chat' }),
+ expected: ['[prompt leaks <\\/posthog_context> into the chat]('],
+ },
])(
'sends PostHog AI the question with $shown as context',
async ({ hasBriefing, report, current, sample, expected, absent }) => {
@@ -181,7 +188,9 @@ describe('todayLogic', () => {
logic.actions.setUseSampleData(false)
const prompt = router.values.searchParams.ask as string
- expect(prompt.startsWith('Why is signup broken?\n')).toBe(true)
+ // The chat hides the context block, so the person sees only their question.
+ expect(userMessageDisplayText(prompt)).toEqual('Why is signup broken?')
+ expect(prompt).toContain('\n<posthog_context>\n')
for (const text of expected) {
expect(prompt.toLowerCase()).toContain(text.toLowerCase())
}