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(desktop): share product engineer guidance across agents (#87110)

Shy Alter2fc9f6eba93f
diff --git a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.test.ts b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.test.ts
index e748031a9cd0..bf5db4787fed 100644
--- a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.test.ts
+++ b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.test.ts
@@ -1590,14 +1590,13 @@ describe("CodexAppServerAgent", () => {
},
);
- it("flattens the host's {append} systemPrompt and dedupes it against developerInstructions", async () => {
+ it("dedupes a system prompt contained in developerInstructions", async () => {
const stub = makeStubRpc({ "thread/start": { thread: { id: "t" } } });
const { client } = makeFakeClient();
const agent = new CodexAppServerAgent(client, {
processOptions: {
binaryPath: "/x/codex",
- // The host pre-flattens into developerInstructions AND sends the raw {append} form.
- developerInstructions: "Be a careful engineer.",
+ developerInstructions: "Be a careful engineer.\n\nUse RTK.",
},
rpcFactory: stub.factory,
});
@@ -1608,11 +1607,10 @@ describe("CodexAppServerAgent", () => {
} as unknown as NewSessionRequest);
const threadStart = stub.requests.find((r) => r.method === "thread/start");
- // {append} is flattened (not "[object Object]") and, being identical, deduped to one copy.
expect(
(threadStart?.params as { developerInstructions?: string })
.developerInstructions,
- ).toBe("Be a careful engineer.");
+ ).toBe("Be a careful engineer.\n\nUse RTK.");
});
it("appends a distinct {append} systemPrompt to developerInstructions", async () => {
diff --git a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts
index 8775ffc214fc..50f354c99f39 100644
--- a/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts
+++ b/products/desktop/packages/agent/src/adapters/codex-app-server/codex-app-server-agent.ts
@@ -590,16 +590,10 @@ export class CodexAppServerAgent extends BaseAcpAgent {
this.config.setInitialMode(params.meta?.permissionMode);
// Codex doesn't attribute input tokens by source; the baseline seeds the resident floor + system prompt.
this.usage.setBaseline(buildBaseline(params.meta));
- // Flatten the {append} form (else "[object Object]") and dedupe identical parts
- // (the host pre-flattens into developerInstructions, so the prod prompt would duplicate).
- const developerInstructions = [
- ...new Set(
- [
- this.developerInstructions,
- flattenSystemPrompt(params.meta?.systemPrompt),
- ].filter((s): s is string => !!s),
- ),
- ].join("\n\n");
+ const developerInstructions = mergeDeveloperInstructions(
+ this.developerInstructions,
+ flattenSystemPrompt(params.meta?.systemPrompt),
+ );
this.threadSetup = { meta: params.meta, developerInstructions };
// Degrade gracefully: an unresolvable bundled local-tools script skips it with a
// warning rather than killing thread setup.
@@ -2409,3 +2403,15 @@ function flattenSystemPrompt(
}
return undefined;
}
+
+function mergeDeveloperInstructions(
+ developerInstructions: string | undefined,
+ systemPrompt: string | undefined,
+): string {
+ if (!developerInstructions) return systemPrompt ?? "";
+ if (!systemPrompt || developerInstructions.includes(systemPrompt)) {
+ return developerInstructions;
+ }
+ if (systemPrompt.includes(developerInstructions)) return systemPrompt;
+ return `${developerInstructions}\n\n${systemPrompt}`;
+}
diff --git a/products/desktop/packages/agent/src/server/agent-server.ts b/products/desktop/packages/agent/src/server/agent-server.ts
index 563934184bad..f7a3c4bf4282 100644
--- a/products/desktop/packages/agent/src/server/agent-server.ts
+++ b/products/desktop/packages/agent/src/server/agent-server.ts
@@ -35,6 +35,7 @@ import {
buildPosthogScopedPropertyHeaderLines,
buildPosthogScopedPropertyHeaderRecord,
} from "@posthog/shared/posthog-property-headers";
+import { prependProductEngineerPrompt } from "@posthog/shared/product-engineer-prompt";
import { appendRichOutputPrompt } from "@posthog/shared/rich-output-prompt";
import { unzipSync } from "fflate";
import { Hono } from "hono";
@@ -168,12 +169,19 @@ export function buildCloudSessionSystemPrompt(
cloudAppend: string,
userPrompt: ClaudeCodeConfig["systemPrompt"],
): string | { append: string } {
- if (typeof userPrompt === "string") {
- return appendRichOutputPrompt([userPrompt, cloudAppend].join("\n\n"));
- }
+ const prompt = [
+ typeof userPrompt === "string" ? userPrompt : userPrompt?.append,
+ cloudAppend,
+ ]
+ .filter(Boolean)
+ .join("\n\n");
+ const combinedPrompt = appendRichOutputPrompt(
+ prependProductEngineerPrompt(prompt),
+ );
- const prompt = [userPrompt?.append, cloudAppend].filter(Boolean).join("\n\n");
- return { append: appendRichOutputPrompt(prompt) };
+ return typeof userPrompt === "string"
+ ? combinedPrompt
+ : { append: combinedPrompt };
}
function sleep(ms: number): Promise<void> {
diff --git a/products/desktop/packages/agent/src/server/cloud-session-prompt.test.ts b/products/desktop/packages/agent/src/server/cloud-session-prompt.test.ts
index 59b762c76e2c..bb2816b9b27c 100644
--- a/products/desktop/packages/agent/src/server/cloud-session-prompt.test.ts
+++ b/products/desktop/packages/agent/src/server/cloud-session-prompt.test.ts
@@ -1,3 +1,4 @@
+import { PRODUCT_ENGINEER_PROMPT } from "@posthog/shared/product-engineer-prompt";
import { RICH_OUTPUT_TAGS_PROMPT } from "@posthog/shared/rich-output-prompt";
import { describe, expect, it } from "vitest";
import { buildCloudSessionSystemPrompt } from "./agent-server";
@@ -14,13 +15,17 @@ describe("buildCloudSessionSystemPrompt", () => {
append: "Use the canvas tools.",
},
],
- ])("includes rich-output instructions for %s", (_name, userPrompt) => {
+ ])("includes shared guidance for %s", (_name, userPrompt) => {
const prompt = buildCloudSessionSystemPrompt(
"Cloud task instructions.",
userPrompt,
);
const text = typeof prompt === "string" ? prompt : prompt.append;
+ expect(text).toContain(PRODUCT_ENGINEER_PROMPT);
expect(text).toContain(RICH_OUTPUT_TAGS_PROMPT);
+ expect(text.indexOf(PRODUCT_ENGINEER_PROMPT)).toBeLessThan(
+ text.indexOf("Cloud task instructions."),
+ );
});
});
diff --git a/products/desktop/packages/harness/src/extensions/product-engineer/extension.test.ts b/products/desktop/packages/harness/src/extensions/product-engineer/extension.test.ts
index 6c17c7fd9815..26f08966d0b8 100644
--- a/products/desktop/packages/harness/src/extensions/product-engineer/extension.test.ts
+++ b/products/desktop/packages/harness/src/extensions/product-engineer/extension.test.ts
@@ -1,13 +1,9 @@
-import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
+import { PRODUCT_ENGINEER_PROMPT } from "@posthog/shared/product-engineer-prompt";
import { describe, expect, it } from "vitest";
import { createProductEngineerExtension } from "./extension";
-const PRODUCT_ENGINEER_PROMPT = readFileSync(
- new URL("./prompts/product-engineer.md", import.meta.url),
- "utf8",
-).trim();
const UPSTREAM_SYSTEM_PROMPT = "Upstream pi instructions that may change.";
type BeforeAgentStart = (event: { systemPrompt: string }) => {
diff --git a/products/desktop/packages/harness/src/extensions/product-engineer/extension.ts b/products/desktop/packages/harness/src/extensions/product-engineer/extension.ts
index 1ad5fee712cd..b9521562ad9f 100644
--- a/products/desktop/packages/harness/src/extensions/product-engineer/extension.ts
+++ b/products/desktop/packages/harness/src/extensions/product-engineer/extension.ts
@@ -1,9 +1,10 @@
-import { existsSync, readFileSync } from "node:fs";
+import { existsSync } from "node:fs";
import { fileURLToPath } from "node:url";
import type {
ExtensionAPI,
ExtensionFactory,
} from "@earendil-works/pi-coding-agent";
+import { prependProductEngineerPrompt } from "@posthog/shared/product-engineer-prompt";
function resolveProductEngineerResource(relativePath: string): string {
const adjacentPath = fileURLToPath(new URL(relativePath, import.meta.url));
@@ -15,17 +16,6 @@ function resolveProductEngineerResource(relativePath: string): string {
);
}
-const PRODUCT_ENGINEER_PROMPT = readFileSync(
- resolveProductEngineerResource("prompts/product-engineer.md"),
- "utf8",
-).trim();
-
-function addProductEngineerPrompt(systemPrompt: string): string {
- return systemPrompt.startsWith(PRODUCT_ENGINEER_PROMPT)
- ? systemPrompt
- : `${PRODUCT_ENGINEER_PROMPT}\n\n${systemPrompt}`;
-}
-
export function createProductEngineerExtension(
instrumentationSkillsDirectory = resolveProductEngineerResource("skills"),
): ExtensionFactory {
@@ -36,7 +26,7 @@ export function createProductEngineerExtension(
: [],
}));
pi.on("before_agent_start", (event) => ({
- systemPrompt: addProductEngineerPrompt(event.systemPrompt),
+ systemPrompt: prependProductEngineerPrompt(event.systemPrompt),
}));
};
}
diff --git a/products/desktop/packages/harness/tsup.config.ts b/products/desktop/packages/harness/tsup.config.ts
index ed5508250d21..62e5bda34b04 100644
--- a/products/desktop/packages/harness/tsup.config.ts
+++ b/products/desktop/packages/harness/tsup.config.ts
@@ -181,11 +181,6 @@ export default defineConfig({
if (process.env.SKIP_PRODUCT_ENGINEER_SKILLS_DOWNLOAD !== "1") {
await copyOmnibusSkills("dist/extensions/product-engineer/skills");
}
- await cp(
- "src/extensions/product-engineer/prompts",
- "dist/extensions/product-engineer/prompts",
- { recursive: true },
- );
await cp(
"src/extensions/workflow/skills",
"dist/extensions/workflow/skills",
diff --git a/products/desktop/packages/shared/package.json b/products/desktop/packages/shared/package.json
index 68b45e43a3ca..a6f830637cd5 100644
--- a/products/desktop/packages/shared/package.json
+++ b/products/desktop/packages/shared/package.json
@@ -51,6 +51,10 @@
"./posthog-property-headers": {
"types": "./dist/posthog-property-headers.d.ts",
"import": "./dist/posthog-property-headers.js"
+ },
+ "./product-engineer-prompt": {
+ "types": "./dist/product-engineer-prompt.d.ts",
+ "import": "./dist/product-engineer-prompt.js"
}
},
"scripts": {
diff --git a/products/desktop/packages/shared/src/product-engineer-prompt.test.ts b/products/desktop/packages/shared/src/product-engineer-prompt.test.ts
new file mode 100644
index 000000000000..a421f87b9fcd
--- /dev/null
+++ b/products/desktop/packages/shared/src/product-engineer-prompt.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from "vitest";
+import {
+ PRODUCT_ENGINEER_PROMPT,
+ prependProductEngineerPrompt,
+} from "./product-engineer-prompt";
+
+const UPSTREAM_PROMPT = "Existing runtime guidance.";
+
+describe("prependProductEngineerPrompt", () => {
+ it.each([
+ ["an empty prompt", "", PRODUCT_ENGINEER_PROMPT],
+ [
+ "existing runtime guidance",
+ UPSTREAM_PROMPT,
+ `${PRODUCT_ENGINEER_PROMPT}\n\n${UPSTREAM_PROMPT}`,
+ ],
+ ])(
+ "prepends product engineering guidance to %s",
+ (_name, prompt, expected) => {
+ expect(prependProductEngineerPrompt(prompt)).toBe(expected);
+ },
+ );
+
+ it("does not duplicate guidance when a session rebuilds its prompt", () => {
+ const firstPrompt = prependProductEngineerPrompt(UPSTREAM_PROMPT);
+
+ expect(prependProductEngineerPrompt(firstPrompt)).toBe(firstPrompt);
+ });
+});
diff --git a/products/desktop/packages/harness/src/extensions/product-engineer/prompts/product-engineer.md b/products/desktop/packages/shared/src/product-engineer-prompt.ts
similarity index 82%
rename from products/desktop/packages/harness/src/extensions/product-engineer/prompts/product-engineer.md
rename to products/desktop/packages/shared/src/product-engineer-prompt.ts
index 29201a97c79b..756418cf1ea8 100644
--- a/products/desktop/packages/harness/src/extensions/product-engineer/prompts/product-engineer.md
+++ b/products/desktop/packages/shared/src/product-engineer-prompt.ts
@@ -1,4 +1,4 @@
-Operate as an expert product engineer. Use PostHog as the default platform for understanding users, observing quality, and shipping changes safely:
+export const PRODUCT_ENGINEER_PROMPT = `Operate as an expert product engineer. Use PostHog as the default platform for understanding users, observing quality, and shipping changes safely:
- Start from the user problem, desired experience, and product context. Understand why the work matters before deciding what to build.
- Use available evidence such as user feedback, product data, support signals, market context, and company strategy. Ask for missing context when it would change the decision.
- Exercise product judgment. Help decide both what to build and how to build it instead of treating the request as a fixed specification.
@@ -26,10 +26,19 @@ Choose the smallest set of PostHog products that solves the user problem. Know P
- Replay Vision: Use AI to turn session recordings into structured, queryable data.
Use PostHog throughout the product loop:
-- Before building, use `mcp` to inspect relevant PostHog project data, existing events, feature flags, errors, logs, traces, insights, and user feedback when they would improve the decision. Search before calling. Read or list before writing or creating, and reuse existing resources.
+- Before building, use \`mcp\` to inspect relevant PostHog project data, existing events, feature flags, errors, logs, traces, insights, and user feedback when they would improve the decision. Search before calling. Read or list before writing or creating, and reuse existing resources.
- Before adding instrumentation, inspect the existing PostHog SDK setup and invoke the matching bundled instrumentation skill. Do not install duplicate SDKs or create parallel initialization paths.
- Use product analytics for meaningful user actions and outcomes, not low-value implementation events. Never capture secrets, sensitive content, or unnecessary personal data.
- Use PostHog feature flags for uncertain or risky rollouts. Use PostHog error tracking, logs, and traces to make failures diagnosable. Use PostHog AI Observability for AI model calls.
- After shipping, use PostHog to verify adoption, outcomes, regressions, and rollout health. Let observed behavior drive the next iteration.
-Prefer customer impact and product quality over technical novelty. Treat code as one tool for creating useful, measurable outcomes.
+Prefer customer impact and product quality over technical novelty. Treat code as one tool for creating useful, measurable outcomes.`;
+
+export function prependProductEngineerPrompt(prompt: string): string {
+ if (prompt.includes(PRODUCT_ENGINEER_PROMPT)) {
+ return prompt;
+ }
+ return prompt
+ ? `${PRODUCT_ENGINEER_PROMPT}\n\n${prompt}`
+ : PRODUCT_ENGINEER_PROMPT;
+}
diff --git a/products/desktop/packages/shared/tsup.config.ts b/products/desktop/packages/shared/tsup.config.ts
index 422f9e434710..acf9af2e9e69 100644
--- a/products/desktop/packages/shared/tsup.config.ts
+++ b/products/desktop/packages/shared/tsup.config.ts
@@ -11,6 +11,7 @@ export default defineConfig({
"src/domain-types.ts",
"src/mcp-sandbox-proxy.ts",
"src/posthog-property-headers.ts",
+ "src/product-engineer-prompt.ts",
"src/quick-ask-shortcuts.ts",
"src/rich-output-prompt.ts",
"src/types.ts",
diff --git a/products/desktop/packages/workspace-server/src/services/agent/agent.test.ts b/products/desktop/packages/workspace-server/src/services/agent/agent.test.ts
index 7cb81a311961..73b8fe93a052 100644
--- a/products/desktop/packages/workspace-server/src/services/agent/agent.test.ts
+++ b/products/desktop/packages/workspace-server/src/services/agent/agent.test.ts
@@ -158,6 +158,7 @@ vi.mock("node:fs", async (importOriginal) => {
// --- Import after mocks ---
import { fetchGatewayModels } from "@posthog/agent/gateway-models";
+import { PRODUCT_ENGINEER_PROMPT } from "@posthog/shared/product-engineer-prompt";
import { RICH_OUTPUT_TAGS_PROMPT } from "@posthog/shared/rich-output-prompt";
import {
AgentService,
@@ -1040,10 +1041,14 @@ describe("AgentService", () => {
["default", undefined],
["overridden", "Generate a canvas."],
])(
- "uses the shared object-reference prompt for a %s session",
+ "uses the shared session guidance for a %s session",
(_name, systemPromptOverride) => {
- expect(buildChannelPrompt(systemPromptOverride)).toContain(
- RICH_OUTPUT_TAGS_PROMPT,
+ const prompt = buildChannelPrompt(systemPromptOverride);
+
+ expect(prompt).toContain(PRODUCT_ENGINEER_PROMPT);
+ expect(prompt).toContain(RICH_OUTPUT_TAGS_PROMPT);
+ expect(prompt.indexOf(PRODUCT_ENGINEER_PROMPT)).toBeLessThan(
+ prompt.indexOf(systemPromptOverride ?? "PostHog context:"),
);
},
);
diff --git a/products/desktop/packages/workspace-server/src/services/agent/agent.ts b/products/desktop/packages/workspace-server/src/services/agent/agent.ts
index 8c29492cbd2e..5b9280793aa3 100644
--- a/products/desktop/packages/workspace-server/src/services/agent/agent.ts
+++ b/products/desktop/packages/workspace-server/src/services/agent/agent.ts
@@ -75,6 +75,7 @@ import {
serializeError,
TypedEventEmitter,
} from "@posthog/shared";
+import { prependProductEngineerPrompt } from "@posthog/shared/product-engineer-prompt";
import { appendRichOutputPrompt } from "@posthog/shared/rich-output-prompt";
import { inject, injectable, preDestroy } from "inversify";
import { WORKSPACE_REPOSITORY } from "../../db/identifiers";
@@ -626,9 +627,13 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
): {
append: string;
} {
- // Overrides replace coding guidance, but rich tags must stay available in every agent response.
+ // Overrides replace task guidance, but product engineering and rich-output rules stay available.
if (systemPromptOverride) {
- return { append: appendRichOutputPrompt(systemPromptOverride) };
+ return {
+ append: appendRichOutputPrompt(
+ prependProductEngineerPrompt(systemPromptOverride),
+ ),
+ };
}
let prompt = `PostHog context: use project ${credentials.projectId} on ${credentials.apiHost}. When using PostHog MCP tools, operate only on this project.`;
@@ -701,7 +706,9 @@ If a repository is required, call \`list_repos\` to find it, then use \`clone_re
prompt += `\n\nThe user has granted you access to additional directories outside the working directory. You may read and edit files in these paths just like the working directory:\n<additional_directories>\n${dirs}\n</additional_directories>`;
}
- return { append: appendRichOutputPrompt(prompt) };
+ return {
+ append: appendRichOutputPrompt(prependProductEngineerPrompt(prompt)),
+ };
}
async startSession(params: StartSessionInput): Promise<SessionResponse> {