: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tasks): link the task thread in task-created prs (#125947)
Dylan Martinb2dd7a8a1664
diff --git a/packages/agent/packages/agent/src/sdk/product.test.ts b/packages/agent/packages/agent/src/sdk/product.test.tsindex e6c67f77bbe0..791102c38c10 100644--- a/packages/agent/packages/agent/src/sdk/product.test.ts+++ b/packages/agent/packages/agent/src/sdk/product.test.ts@@ -97,4 +97,13 @@ describe("buildPullRequestFooter", () => {] as const)("formats the %s footer", (_name, options, footer, expected) => {expect(buildPullRequestFooter(launch(options), footer)).toBe(expected);});++ it("credits PostHog for a task from the web chat", () => {+ const webLaunch = { ...launch(), clientProvenance: "posthog_web" };+ const source = { label: "PostHog AI chat", url: "https://example.com/c" };++ expect(buildPullRequestFooter(webLaunch, { source })).toBe(+ "*Created with [PostHog](https://posthog.com?ref=pr) from a [PostHog AI chat](https://example.com/c)*",+ );+ });});diff --git a/packages/agent/packages/agent/src/sdk/product.ts b/packages/agent/packages/agent/src/sdk/product.tsindex bd2ba13146b6..2c5f71f4c14a 100644--- a/packages/agent/packages/agent/src/sdk/product.ts+++ b/packages/agent/packages/agent/src/sdk/product.ts@@ -49,6 +49,8 @@ export interface ProductLaunchConfiguration<readonly runId: string;/** Product that started the run. */readonly originProduct?: string;+ /** PostHog client that created the task. */+ readonly clientProvenance?: string;/** Product-specific launch data. */readonly launchOptions: Readonly<LaunchOptions>;/** Optional source that started the task. */@@ -139,7 +141,7 @@ export function buildPullRequestFooter("Created with [PostHog Desktop](https://posthog.com/desktop?ref=pr)";if (mobile) {createdWith = "Created with [PostHog Mobile](https://posthog.com?ref=pr)";- } else if (options.automated) {+ } else if (options.automated || launch.clientProvenance === "posthog_web") {createdWith = "Created with [PostHog](https://posthog.com?ref=pr)";}diff --git a/packages/agent/packages/agent/src/server/launch/run-launch.test.ts b/packages/agent/packages/agent/src/server/launch/run-launch.test.tsindex 74dde1fdb8fe..a7f48ccbec7e 100644--- a/packages/agent/packages/agent/src/server/launch/run-launch.test.ts+++ b/packages/agent/packages/agent/src/server/launch/run-launch.test.ts@@ -122,18 +122,59 @@ describe("run launch", () => {});describe("readProductLaunch", () => {- it("links the product source to the inbox report", () => {- const launch = readProductLaunch(- sources({- apiUrl: "https://us.posthog.com/",- task: createTask({ signal_report: "rep_1" }),+ it.each<[string, Task, string | undefined]>([+ [+ "the inbox report",+ createTask({ signal_report: "rep_1" }),+ "https://us.posthog.com/project/2/inbox/rep_1",+ ],+ [+ "the web chat for a web task",+ createTask({ client_provenance: "posthog_web" }),+ "https://us.posthog.com/project/2/ai?task=task-1",+ ],+ [+ "the web chat for a browser task without provenance",+ createTask({ origin_product: "posthog_ai" }),+ "https://us.posthog.com/project/2/ai?task=task-1",+ ],+ [+ "the desktop bridge for a PostHog AI task from desktop",+ createTask({+ origin_product: "posthog_ai",+ client_provenance: "posthog_desktop",+ }),+ "https://us.posthog.com/code/task/task-1",+ ],+ [+ "the web chat for a mobile task",+ createTask({+ origin_product: "posthog_ai",+ client_provenance: "posthog_mobile",}),+ "https://us.posthog.com/project/2/ai?task=task-1",+ ],+ [+ "the desktop bridge for a channel task",+ createTask({ client_provenance: "posthog_desktop", channel: "ch-1" }),+ "https://us.posthog.com/code/channel/ch-1/tasks/task-1",+ ],+ [+ "the desktop bridge for a desktop task",+ createTask({ client_provenance: "posthog_desktop" }),+ "https://us.posthog.com/code/task/task-1",+ ],+ [+ "nothing for an automated task",+ createTask({ origin_product: "review_hog" }),+ undefined,+ ],+ ])("links the product source to %s", (_, task, expected) => {+ const launch = readProductLaunch(+ sources({ apiUrl: "https://us.posthog.com/", task }),);- expect(launch.source).toEqual({- label: "inbox report",- url: "https://us.posthog.com/project/2/inbox/rep_1",- });+ expect(launch.source?.url).toBe(expected);});it("gives products a copy of the run state", () => {diff --git a/packages/agent/packages/agent/src/server/launch/run-launch.ts b/packages/agent/packages/agent/src/server/launch/run-launch.tsindex 87a142fe8cd3..983ff0df1710 100644--- a/packages/agent/packages/agent/src/server/launch/run-launch.ts+++ b/packages/agent/packages/agent/src/server/launch/run-launch.ts@@ -83,6 +83,7 @@ export function readProductLaunch(taskId: sources.payload.task_id,runId: sources.payload.run_id,originProduct: readInteractionOrigin(sources.env),+ clientProvenance: readClientProvenance(sources.task),launchOptions: structuredClone(sources.taskRun?.state ?? {}),source: readTaskSource(sources),};@@ -98,6 +99,14 @@ function readInteractionOrigin();}+// Browser session auth records no provenance, so a posthog_ai task without one comes from the web.+function readClientProvenance(task: Task | null): string | undefined {+ if (task?.client_provenance) {+ return task.client_provenance;+ }+ return task?.origin_product === "posthog_ai" ? "posthog_web" : undefined;+}+function readRepositories(task: Task | null): string[] {if (task?.repositories?.length) {return task.repositories;@@ -105,17 +114,41 @@ function readRepositories(task: Task | null): string[] {return task?.repository ? [task.repository] : [];}-// Web backlink to the inbox report that spawned this task, so the-// auto-generated PR can point back at it. Built from the same pieces as the-// report's `_posthogUrl`: <apiUrl>/project/<projectId>/inbox/<reportId>.+// Automated origins get no thread link, so sibling tasks keep one shared prompt cache.+const THREAD_ORIGINS = new Set(["user_created", "posthog_ai"]);++// GitHub drops custom URL schemes from links, so Desktop threads use the /code/... web bridge.function readTaskSource(sources: RunLaunchSources): TaskSource | undefined {- const signalReportId = sources.task?.signal_report;- if (!signalReportId) {+ const { task } = sources;+ if (!task) {return undefined;}const { apiUrl, projectId } = sources.config;- return {- label: "inbox report",- url: `${getProjectWebUrl(apiUrl, projectId)}/inbox/${signalReportId}`,- };+ const projectWebUrl = getProjectWebUrl(apiUrl, projectId);+ if (task.signal_report) {+ return {+ label: "inbox report",+ url: `${projectWebUrl}/inbox/${task.signal_report}`,+ };+ }+ if (!THREAD_ORIGINS.has(task.origin_product)) {+ return undefined;+ }++ const taskId = encodeURIComponent(sources.payload.task_id);+ const clientProvenance = readClientProvenance(task);+ if (+ clientProvenance === "posthog_web" ||+ clientProvenance === "posthog_mobile"+ ) {+ return {+ label: "PostHog AI chat",+ url: `${projectWebUrl}/ai?task=${taskId}`,+ };+ }+ const appUrl = apiUrl.replace(/\/+$/, "");+ const path = task.channel+ ? `code/channel/${encodeURIComponent(task.channel)}/tasks/${taskId}`+ : `code/task/${taskId}`;+ return { label: "PostHog Desktop task", url: `${appUrl}/${path}` };}diff --git a/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.test.ts b/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.test.tsindex 890f2ae14a38..12b7d958a368 100644--- a/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.test.ts+++ b/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.test.ts@@ -18,6 +18,9 @@ describe("buildTaskSystemPrompt", () => {expect(prompt).toContain("This is task task-123");expect(prompt).toContain("Generated-By: PostHog Desktop");expect(prompt).toContain("Task-Id: task-123");+ expect(prompt).toContain(+ "*Created with [PostHog Desktop](https://posthog.com/desktop?ref=pr) from a [PostHog Desktop task](https://us.posthog.com/code/task/task-123)*",+ );expect(prompt).toContain("Keep the patch small.");expect(prompt).toContain("<directory>/tmp/a&b</directory>");expect(prompt).toContain("<directory>/tmp/<shared></directory>");@@ -56,6 +59,7 @@ describe("buildTaskSystemPrompt", () => {expect(prompt).toContain("Generated-By: PostHog Desktop");expect(prompt).toContain("Task-Id: task-123");expect(prompt).not.toContain('git commit -m "$(cat');+ expect(prompt).not.toContain("/code/task/");expect(prompt).toContain("Use the existing pull request.");expect(prompt).toContain("## Keeping the task summary");expect(prompt).toContain("task_summary_update");diff --git a/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.ts b/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.tsindex e43ccca99749..ea5f4cdac5d3 100644--- a/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.ts+++ b/packages/agent/packages/harness/src/extensions/task-system-prompt/prompt.ts@@ -31,7 +31,13 @@ export function buildCompactionContinuationPrompt(): string {export function buildAttributionPrompt(taskId: string,environment: TaskContext["environment"],+ apiHost?: string,): string {+ // Local runs always come from Desktop, and GitHub drops custom URL schemes from links.+ const threadLink =+ environment === "local" && apiHost+ ? ` from a [PostHog Desktop task](${apiHost.replace(/\/+$/, "")}/code/task/${encodeURIComponent(taskId)})`+ : "";const commitInstructions =environment === "cloud"? `In cloud tasks, call \`git_signed_commit\` to create commits. It automatically adds these trailers:@@ -62,12 +68,15 @@ When creating new branches, prefix them with \`posthog/\` (e.g. \`posthog/fix-loWhen creating pull requests, add the following footer at the end of the PR description:\`\`\`----*Created with [PostHog Desktop](https://posthog.com/desktop?ref=pr)*+*Created with [PostHog Desktop](https://posthog.com/desktop?ref=pr)${threadLink}*\`\`\``;}-export function buildLocalAttributionPrompt(taskId: string): string {- return buildAttributionPrompt(taskId, "local");+export function buildLocalAttributionPrompt(+ taskId: string,+ apiHost?: string,+): string {+ return buildAttributionPrompt(taskId, "local", apiHost);}export function buildQuestionsPrompt(@@ -144,7 +153,11 @@ export function buildTaskSystemPrompt(];sections.push(- buildAttributionPrompt(context.taskId, context.environment),+ buildAttributionPrompt(+ context.taskId,+ context.environment,+ context.apiHost,+ ),buildQuestionsPrompt(capabilities.structuredInput === true),buildPullRequestLinksPrompt(),buildShellEfficiencyPrompt(),