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): attribute local pi costs to tasks (#88425)

Adam Bowkercab874d0e111
diff --git a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
index 9e8ae66a562d..4a6dc4ef12cc 100644
--- a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
+++ b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.test.ts
@@ -95,7 +95,11 @@ describe("DesktopPiRpcClientFactory", () => {
).resolves.toBe(client);
expect(authProxy.start).toHaveBeenCalledWith(
getLlmGatewayUrl(getCloudUrlFromRegion("eu")),
- { "X-PostHog-Project-Id": "1" },
+ {
+ "x-posthog-property-task_id": "task-1",
+ "x-posthog-property-$ai_session_id": "task-1",
+ "X-PostHog-Project-Id": "1",
+ },
);
expect(authProxy.start).toHaveBeenCalledWith("https://eu.posthog.com");
expect(createPiRpcClient).toHaveBeenCalledWith({
diff --git a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
index f04a7f70aafd..1649597c881e 100644
--- a/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
+++ b/products/desktop/apps/code/src/main/platform-adapters/desktop-pi-rpc-client-factory.ts
@@ -8,7 +8,7 @@ import {
import { getLlmGatewayUrl } from "@posthog/agent/posthog-api";
import { ROOT_LOGGER, type RootLogger } from "@posthog/di/logger";
import { type CloudRegion, getCloudUrlFromRegion } from "@posthog/shared";
-import { buildPosthogProjectHeaderRecord } from "@posthog/shared/posthog-property-headers";
+import { buildPosthogScopedPropertyHeaderRecord } from "@posthog/shared/posthog-property-headers";
import { prepareContextWiki } from "@posthog/workspace-server/services/agent/context-wiki";
import {
AGENT_AUTH,
@@ -52,7 +52,7 @@ export class DesktopPiRpcClientFactory implements PiRpcClientFactory {
// Four independent round-trips: proxy URL, auth proxy, MCP config, wiki mount.
const [baseUrl, enrichmentApiUrl, mcpConfiguration, contextWikiPath] =
await Promise.all([
- this.getProxyUrl(credentials.region, projectId),
+ this.getProxyUrl(credentials.region, projectId, input.taskId),
this.authProxy.start(access.apiHost),
this.mcpServerSource.getMcpRuntimeConfiguration(),
this.mountContextWiki(projectId),
@@ -111,11 +111,18 @@ export class DesktopPiRpcClientFactory implements PiRpcClientFactory {
}
}
- private getProxyUrl(region: CloudRegion, projectId: number): Promise<string> {
+ private getProxyUrl(
+ region: CloudRegion,
+ projectId: number,
+ taskId: string,
+ ): Promise<string> {
const gatewayUrl = getLlmGatewayUrl(getCloudUrlFromRegion(region));
return this.authProxy.start(
gatewayUrl,
- buildPosthogProjectHeaderRecord(projectId),
+ buildPosthogScopedPropertyHeaderRecord(
+ { task_id: taskId, $ai_session_id: taskId },
+ projectId,
+ ),
);
}
}