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(signals): tell inbox report runs to keep the report state honest (#95895)

Andrew Maguire8acee9f3b1be
diff --git a/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts b/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
index 109c2e7d7c7d..6c825e689347 100644
--- a/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
+++ b/products/signals/frontend/inbox/inboxTaskKickoffLogic.test.ts
@@ -1,62 +1,101 @@
import { makeReport } from './__mocks__/inboxMocks'
-import { buildDiscussReportPrompt } from './inboxTaskKickoffLogic'
+import { buildCreatePrReportPrompt, buildDiscussReportPrompt } from './inboxTaskKickoffLogic'
import { SignalReportStatus } from './types'
-describe('buildDiscussReportPrompt', () => {
- const url = 'https://app.posthog.com/project/1/inbox/report-1'
+describe('inboxTaskKickoffLogic', () => {
+ describe('buildDiscussReportPrompt', () => {
+ const url = 'https://app.posthog.com/project/1/inbox/report-1'
- it.each([SignalReportStatus.READY, SignalReportStatus.PENDING_INPUT])(
- 'tells the agent to carry out actions for a %s report',
- (status) => {
- const prompt = buildDiscussReportPrompt(
- makeReport({ status }),
- url,
- 'Create the alert the report recommends'
- )
- expect(prompt).toContain('carry the action out')
- expect(prompt).toContain(url)
- }
- )
+ it.each([SignalReportStatus.READY, SignalReportStatus.PENDING_INPUT])(
+ 'tells the agent to carry out actions for a %s report',
+ (status) => {
+ const prompt = buildDiscussReportPrompt(
+ makeReport({ status }),
+ url,
+ 'Create the alert the report recommends'
+ )
+ expect(prompt).toContain('carry the action out')
+ expect(prompt).toContain(url)
+ expect(prompt).toContain('inbox-reports-set-state')
+ // A discussion run can open a PR too, so it carries the Create PR prompt's guard: a
+ // resolve through the state API closes the report's open PR.
+ expect(prompt).toContain('would close the PR you just opened')
+ // The state API has no ownership precondition, so the prompt is the only thing keeping
+ // a discussion run from ending work another run holds.
+ expect(prompt).toContain('leave its state alone when somebody else holds it')
+ }
+ )
+
+ // Only judged, still-active reports may drive actions: pre-judgment statuses carry unjudged
+ // pipeline content, suppressed/failed reports carry the content the judge rejected, and a
+ // resolved report's persisted action suggestions would redo already-completed work.
+ it.each([
+ SignalReportStatus.POTENTIAL,
+ SignalReportStatus.CANDIDATE,
+ SignalReportStatus.IN_PROGRESS,
+ SignalReportStatus.RESOLVED,
+ SignalReportStatus.SUPPRESSED,
+ SignalReportStatus.FAILED,
+ SignalReportStatus.DELETED,
+ ])('pins the agent to answering for a %s report', (status) => {
+ const prompt = buildDiscussReportPrompt(makeReport({ status }), url, 'Carry out the recommendation')
+ expect(prompt).toContain('Answer this question')
+ expect(prompt).not.toContain('carry the action out')
+ // An answer-only run changes nothing about the report, so it is never told to touch the state.
+ expect(prompt).not.toContain('inbox-reports-set-state')
+ })
- // Only judged, still-active reports may drive actions: pre-judgment statuses carry unjudged
- // pipeline content, suppressed/failed reports carry the content the judge rejected, and a
- // resolved report's persisted action suggestions would redo already-completed work.
- it.each([
- SignalReportStatus.POTENTIAL,
- SignalReportStatus.CANDIDATE,
- SignalReportStatus.IN_PROGRESS,
- SignalReportStatus.RESOLVED,
- SignalReportStatus.SUPPRESSED,
- SignalReportStatus.FAILED,
- SignalReportStatus.DELETED,
- ])('pins the agent to answering for a %s report', (status) => {
- const prompt = buildDiscussReportPrompt(makeReport({ status }), url, 'Carry out the recommendation')
- expect(prompt).toContain('Answer this question')
- expect(prompt).not.toContain('carry the action out')
+ it.each([
+ // A fix is already in flight, so acting on the recommendations would duplicate it — the same
+ // reason autostart and Create PR eligibility exclude already-addressed reports.
+ ['an already-addressed report', makeReport({ status: SignalReportStatus.READY, already_addressed: true })],
+ // The actionability judge said there is no work to act on, so an action framing would invite
+ // acting anyway — the same judgment that hides Create PR.
+ [
+ 'a report judged not actionable',
+ makeReport({ status: SignalReportStatus.READY, actionability: 'not_actionable' }),
+ ],
+ // An implementation PR already exists for this report, so carrying a stored "implement the
+ // fix" suggestion out would open a second PR for the same work — the same field that hides
+ // Create PR.
+ [
+ 'a report with an implementation PR',
+ makeReport({
+ status: SignalReportStatus.READY,
+ implementation_pr_url: 'https://github.com/x/y/pull/1',
+ }),
+ ],
+ // null = the kickoff refetch could not confirm the report's current state; fail closed.
+ ['an unconfirmed report state', null],
+ ])('pins the agent to answering for %s', (_name, report) => {
+ const prompt = buildDiscussReportPrompt(report, url, 'Carry out the recommendation')
+ expect(prompt).toContain('Answer this question')
+ expect(prompt).not.toContain('carry the action out')
+ })
})
- it.each([
- // A fix is already in flight, so acting on the recommendations would duplicate it — the same
- // reason autostart and Create PR eligibility exclude already-addressed reports.
- ['an already-addressed report', makeReport({ status: SignalReportStatus.READY, already_addressed: true })],
- // The actionability judge said there is no work to act on, so an action framing would invite
- // acting anyway — the same judgment that hides Create PR.
- [
- 'a report judged not actionable',
- makeReport({ status: SignalReportStatus.READY, actionability: 'not_actionable' }),
- ],
- // An implementation PR already exists for this report, so carrying a stored "implement the
- // fix" suggestion out would open a second PR for the same work — the same field that hides
- // Create PR.
- [
- 'a report with an implementation PR',
- makeReport({ status: SignalReportStatus.READY, implementation_pr_url: 'https://github.com/x/y/pull/1' }),
- ],
- // null = the kickoff refetch could not confirm the report's current state; fail closed.
- ['an unconfirmed report state', null],
- ])('pins the agent to answering for %s', (_name, report) => {
- const prompt = buildDiscussReportPrompt(report, url, 'Carry out the recommendation')
- expect(prompt).toContain('Answer this question')
- expect(prompt).not.toContain('carry the action out')
+ describe('buildCreatePrReportPrompt', () => {
+ it('tells the agent how to leave the report state', () => {
+ const prompt = buildCreatePrReportPrompt(makeReport({ status: SignalReportStatus.READY }))
+ expect(prompt).toContain('open a PR')
+ expect(prompt).toContain('inbox-reports-set-state')
+ expect(prompt).toContain('fixed_outside_posthog')
+ expect(prompt).toContain('suppressed')
+ // Suppressing leaves the claim standing, so the run has to drop it itself.
+ expect(prompt).toContain('then release your claim')
+ // The claim is taken once, at task creation, so a rerun starts unclaimed.
+ expect(prompt).toContain('claim it again first')
+ // Resolving through the state API closes the report's open PR, so the run must not report
+ // the PR it just opened as a resolution.
+ expect(prompt).toContain('Do NOT set the state to resolved because you opened a PR')
+ })
+
+ it('keeps the user feedback after the state instructions', () => {
+ const prompt = buildCreatePrReportPrompt(
+ makeReport({ status: SignalReportStatus.READY }),
+ 'check the retries'
+ )
+ expect(prompt.indexOf('inbox-reports-set-state')).toBeLessThan(prompt.indexOf('check the retries'))
+ })
})
})
diff --git a/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts b/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
index c226eef2e1ea..eaefba7930d2 100644
--- a/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
+++ b/products/signals/frontend/inbox/inboxTaskKickoffLogic.ts
@@ -53,10 +53,28 @@ const CREATE_PR_RUNTIME: ClaudeRuntimeSelection = {
reasoning_effort: ReasoningEffortEnumApi.High,
}
-function buildCreatePrReportPrompt(report: SignalReport, feedback?: string): string {
+// The report's state is part of what a run owes the reader, and only the two ends of the happy
+// path are automatic: creating an implementation task claims the report server-side
+// (`record_implementation_task`), and a merged PR resolves it (`_apply_pr_report_state`). Every
+// other ending needs the agent, so spell the endings out. Resolving through the state API also
+// closes the report's open PR (`close_pr_when_report_dismissed`), which is why opening a PR must
+// not be reported as a resolution. Without this the report stays claimed and unresolved after a
+// run that found nothing to do, and the next reader cannot tell it from work still in flight.
+// The claim needs the same care from both ends: it is taken once, when the task is created, so a
+// rerun of a task that released it starts unclaimed, and suppressing a report leaves the claim
+// standing (only `claim_report` clears an actor), which would show a finished run as still working
+// if the report is ever restored.
+const REPORT_STATE_INSTRUCTIONS = `Keep the report's own state honest while you work, with the inbox MCP tools (\`inbox-reports-set-state\`, \`inbox-reports-claim\`):
+- Read the report before you start. This run took the report when its task was created, but a rerun of a run that released it starts unclaimed: claim it again first, so the work you are about to do is visible to everyone else.
+- Opening the PR is enough by itself. The PR is linked to the report for you, and merging it resolves the report. Do NOT set the state to resolved because you opened a PR: that closes the PR you just opened.
+- If the work is finished without a PR, set the state to resolved with the reason that fits (\`fixed_outside_posthog\`, \`pr_merged\`, or \`already_fixed\`) and a short note that says what you did.
+- If the report holds no work to do, set the state to suppressed with the reason that says why (\`report_unclear\`, \`analysis_wrong\`, \`wrong_repo\` with \`corrected_repository\`, \`wontfix_intentional\`, \`wontfix_irrelevant\`, or \`other\`) and a short note, then release your claim: suppressing does not release it for you.
+- If you stop for any other reason, release your claim on the report, so it does not look like work is still in flight.`
+
+export function buildCreatePrReportPrompt(report: SignalReport, feedback?: string): string {
const base = `Act on PostHog Inbox report "${report.title ?? report.id}" (id ${report.id}). Investigate the root cause using the report's contributing findings, implement the fix, and open a PR.${
report.summary ? `\n\nReport summary:\n${report.summary}` : ''
- }`
+ }\n\n${REPORT_STATE_INSTRUCTIONS}`
const trimmed = feedback?.trim()
if (!trimmed) {
return base
@@ -107,7 +125,14 @@ export function buildDiscussReportPrompt(report: SignalReport | null, reportUrl:
// Framed as question-or-action because a report's suggested prompts include next-step requests
// ("create the alert the report recommends"); "answer this question" would pin the agent to
// replying instead of acting.
- return `A user sent this about the PostHog Inbox report at ${reportUrl}. If it is a question, answer it; if it asks for action, carry the action out and summarize what you did:\n\n${question.trim()}`
+ // State hygiene rides along with the action framing only: a run that just answers a question
+ // has changed nothing about the report, so the only endings worth recording are an action that
+ // finishes the report or an exchange that shows it holds no work. A discussion run may open a
+ // PR of its own, so it needs the same do-not-resolve-on-an-open-PR rule the Create PR prompt
+ // carries. It also never claims the report (`record_report_task` claims for `implementation`
+ // only) and the state API has no ownership precondition, so it is told to keep its hands off a
+ // report somebody else is working — the check a discussion run can actually make.
+ return `A user sent this about the PostHog Inbox report at ${reportUrl}. If it is a question, answer it; if it asks for action, carry the action out and summarize what you did:\n\n${question.trim()}\n\nIf you carry an action out that finishes what the report asked for, record it on the report with the inbox MCP tools (\`inbox-reports-set-state\`): set the state to resolved with the reason \`fixed_outside_posthog\` and a short note that says what you did. Opening a pull request does not finish it — the PR is linked to the report and merging it resolves the report, so setting the state to resolved would close the PR you just opened. If the exchange shows the report holds no work to do, set the state to suppressed with the reason that says why and a short note. Before either, read the report again and leave its state alone when somebody else holds it or an implementation PR is already open on it: that work is not yours to end. Answering a question changes nothing about the report, so leave its state alone.`
}
// The per-report cap 429 carries code `signal_report_task_cap` with its message under `error`