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.

fix(desktop): scope agent generations to selected project (#84466)

Adam Bowker14a1e6a05339
diff --git a/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts b/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts
index 418b2c8d9e7b..8e0d939a8c73 100644
--- a/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts
+++ b/products/desktop/packages/agent/src/adapters/claude/session/options.test.ts
@@ -382,11 +382,11 @@ describe("buildSessionOptions", () => {
].join("\n"),
},
{
- name: "forwards POSTHOG_PROJECT_ID as the team_id attribution header",
+ name: "forwards POSTHOG_PROJECT_ID as the gateway project scope",
projectId: "42",
existingHeaders: undefined,
expected: [
- "x-posthog-property-team_id: 42",
+ "X-PostHog-Project-Id: 42",
"x-posthog-property-$ai_session_id: test-session",
"x-posthog-use-bedrock-fallback: true",
].join("\n"),
@@ -397,7 +397,7 @@ describe("buildSessionOptions", () => {
existingHeaders: "x-posthog-property-task_id: task-abc",
expected: [
"x-posthog-property-task_id: task-abc",
- "x-posthog-property-team_id: 42",
+ "X-PostHog-Project-Id: 42",
"x-posthog-property-$ai_session_id: test-session",
"x-posthog-use-bedrock-fallback: true",
].join("\n"),
diff --git a/products/desktop/packages/agent/src/adapters/claude/session/options.ts b/products/desktop/packages/agent/src/adapters/claude/session/options.ts
index 00a0da670a9d..b6f199d45e9b 100644
--- a/products/desktop/packages/agent/src/adapters/claude/session/options.ts
+++ b/products/desktop/packages/agent/src/adapters/claude/session/options.ts
@@ -12,7 +12,10 @@ import type {
SpawnedProcess,
SpawnOptions,
} from "@anthropic-ai/claude-agent-sdk";
-import { buildPosthogPropertyHeaderLines } from "@posthog/shared/posthog-property-headers";
+import {
+ buildPosthogProjectHeaderLines,
+ buildPosthogPropertyHeaderLines,
+} from "@posthog/shared/posthog-property-headers";
import type { FileEnrichmentDeps } from "../../../enrichment/file-enricher";
import { IS_ROOT } from "../../../utils/common";
import type { Logger } from "../../../utils/logger";
@@ -169,15 +172,11 @@ function buildEnvironment(
if (existingCustomHeaders) {
headerLines.push(existingCustomHeaders);
}
- // Attribute every captured $ai_generation event to the customer's team. The
- // gateway authenticates with a shared key, so without this the spend lands on
- // the key owner's team. The gateway lifts `x-posthog-property-*` headers onto
- // the event; both entrypoints export POSTHOG_PROJECT_ID before this runs
- // (workspace-server auth-adapter.ts, server/agent-server.ts). Mirrors django's
- // get_llm_client(team_id=...).
+ // Scope OAuth requests to the selected project so the gateway can authorize
+ // and derive billing attribution from it.
const projectId = gateway?.posthogProjectId ?? process.env.POSTHOG_PROJECT_ID;
if (projectId) {
- headerLines.push(buildPosthogPropertyHeaderLines({ team_id: projectId }));
+ headerLines.push(buildPosthogProjectHeaderLines(Number(projectId)));
}
if (sessionId) {
headerLines.push(
diff --git a/products/desktop/packages/agent/src/agent.test.ts b/products/desktop/packages/agent/src/agent.test.ts
index d25dab794a80..52c00838a942 100644
--- a/products/desktop/packages/agent/src/agent.test.ts
+++ b/products/desktop/packages/agent/src/agent.test.ts
@@ -67,4 +67,26 @@ describe("Agent", () => {
}),
);
});
+
+ it("scopes local Claude sessions to the selected project", async () => {
+ const agent = new Agent({
+ posthog: {
+ apiUrl: "https://us.posthog.com",
+ getApiKey: vi.fn().mockResolvedValue("token"),
+ projectId: 7,
+ },
+ skipLogPersistence: true,
+ });
+
+ await agent.run("task-1", "run-1", {
+ adapter: "claude",
+ repositoryPath: "/tmp/repo",
+ });
+
+ expect(createAcpConnectionMock).toHaveBeenCalledTimes(1);
+ const [[config]] = createAcpConnectionMock.mock.calls as unknown as [
+ [AcpConnectionConfig],
+ ];
+ expect(config.claudeGatewayEnv?.posthogProjectId).toBe("7");
+ });
});
diff --git a/products/desktop/packages/agent/src/agent.ts b/products/desktop/packages/agent/src/agent.ts
index 49324130f8d0..e63d56f3722b 100644
--- a/products/desktop/packages/agent/src/agent.ts
+++ b/products/desktop/packages/agent/src/agent.ts
@@ -123,6 +123,13 @@ export class Agent {
anthropicAuthToken: gatewayConfig.apiKey,
openaiBaseUrl: `${gatewayConfig.gatewayUrl}/v1`,
openaiApiKey: gatewayConfig.apiKey,
+ // Thread the selected project explicitly so the session scopes to it
+ // rather than the shared POSTHOG_PROJECT_ID global, which a
+ // concurrent session start can clobber.
+ posthogProjectId:
+ this.posthogApiConfig?.projectId != null
+ ? String(this.posthogApiConfig.projectId)
+ : undefined,
}
: undefined;